I have a form which sends data to php via ajax , very simple. But when I submit the form the data is not being sent across.
Javascript:
$(document).ready(function(e) { $('#update').submit(function(event) { event.preventDefault(); var fd = new FormData($('#update')[0]); alert(fd); $.ajax({ url:'func/update.php?id=<?php echo md5($result['id']);?>', processData: false, contentType: false, data: fd, type:'POST', dataType:"JSON", success:function(json){ } }); }); }); PHP:
if(empty($_POST)){ echo 'No post data'; }else{ echo 'data sent'; } print_r($_POST); The php is left very basic to see what is going on. I would normally attach my data fom the individual field.
data:{a:a,b:b,c:c}... and so on.
HTML:
<form id="updateStaff"> Profile Picture<br /><img id="pp_img_display" src="../images/<?php echo $result['pp'];?>" width="100px" /><br /><small>Upload a new image</small><br/><input onchange="uploadImage()" type="file" id="file" /><input type="hidden" id="pp_img" value="<?php echo $result['pp']; ?>" /><br/><small>upload .jpg .png .jpeg only</small><br /><div class="form_err" id="image_err"></div> Name: <select id="prefix" required="required"> <option><?php echo $result['prefix']; ?></option> <?php $pre = array('Mr','Mrs','Miss','Ms'); for($x = 0; $x < count($pre); $x++){ if($pre[$x] != $result['prefix']){ echo '<option>'.$pre[$x].'</option>'; } }?> </select> <input type="text" id="f_name" placeholder="First Name" value="<?php echo $result['f_name'];?>" required="required" /> <input type="text" id="l_name" placeholder="Last Name" value="<?php echo $result['l_name'];?>" required="required" /><br/> Job Title: <input type="text" id="job" required="required" placeholder="Job Tilte" value="<?php echo $result['job_title']; ?>" /> <br/> Years of Experience: <input type="number" id="xp" required="required" placeholder="1" value="<?php echo (int)$result['xp'];?>" /> <p><input type="submit" value="Update" /> <input type="button" value="Cancel" onclick="history.go(-1);"/></p> </form> I would have thought that formData would be a shorter way of doing this.
url:'func/update.php?id=<?php echo md5($result["id"]);?>',escape your string properlydata: JSON.stringify(fd)?