COp COp - 4 months ago 9
PHP Question

How to receive AJAX type post with PHP

I have got this straightforward HTML form

<form enctype="multipart/form-data" class="Mesform">
<textarea maxlength="400" type="text" placeholder="Your message" class="MessageInp"></textarea>
<div class="attach">
<input type="file" id="chatfil" accept="image/*">
<label for="chatfil">
<img src="../img/camera.png" class="addphc">
</label>
</form>


and this jquery

$("body").delegate('.MessageInp','keydown',function(e) {
if (e.which==13 ) {
$(".Mesform").submit();
}
});


That's how i submit my form

$(".Mesform").submit(function(){
var val=$(this).children('textarea').val();
var who=$(".headChat").text();
var formData = new FormData($(this)[0]);
alert(formData);
if (val!="") {
$.ajax({
url: '../files/ajax.php',
type: 'POST',
data:formData,
success: function (data) {
alert(data)
},
cache: false,
contentType: false,
processData: false
});
}
return false;
});


But I do not know how to receive this AJAX call with PHP

Answer

In order for the form elements to show up in any request array, such as $_POST they will need name attributes (name="<something>"). For example:

<form enctype="multipart/form-data" class="Mesform">
    <textarea name="description" maxlength="400" placeholder="Your message" class="MessageInp"></textarea>
    <div class="attach">
    <input name="chatfil" type="file" id="chatfil" accept="image/*">
    <label for="chatfil">
        <img src="../img/camera.png" class="addphc">
    </label>
</form>

Plus as stated, <textarea> does not use a type.