alex alex - 22 days ago 7
Ajax Question

Ajax image upload failure, Script doesn't get the php isset function

I've been following along with some ajax uploading tutorial and it was working properly.

Here it's how i done,
i created a form in html like this.



<form id="submit_form" action="php-script/test_lates_statusbx-script.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Select Image</label>
<input type="file" name="ui-is-status_is_photo_fl" id="image_file" />
<textarea name="status_is_text_ara"></textarea>
<span class="help-block">Allowed File Type - jpg, jpeg, png, gif</span>
</div>
<input type="submit" name="is_status_forum_btn" class="btn btn-info" value="Upload" />
</form>
<div id="image_preview">
</div>





and here its my ajax code,



$(document).ready(function(){
$('#submit_form').on('submit', function(e){
e.preventDefault();
$.ajax({
url:"php-script/test_lates_statusbx-script.php",
method:"POST",
data:new FormData(this),
contentType:false,
//cache:false,
processData:false,
success:function(data)
{
$('#image_preview').html(data);
$('#image_file').val('');
}
})
});

});





and my php looks like this,

if(isset($_POST['is_status_forum_btn'])){
echo $fileactuname = basename($_FILES['ui-is-status_is_photo_fl']['name']);
echo $textareastatus = htmlspecialchars($_POST['status_is_text_ara']);
}


Problem: When i click the submit buttons it doesnt execute my code. But if i echo something outside of the isset function will does.Where am i wrong ?

Answer

A submit button is only a successful control if it is used to submit the form.

You are:

  1. Using the submit button to submit the form
  2. Preventing the default behaviour of the submit event so the form is not submitted
  3. Collecting the data from the form with JavaScript
  4. Making an HTTP request with that data

Since (due to step 2) the submit button is no longer being used to submit the form, it isn't included in the object you create with FormData().

Test for the presence of a different piece of data that you are sending.

e.g.

if(isset($_FILES['ui-is-status_is_photo_fl'])) 
Comments