如何使用PHP接收AJAX类型的帖子

时间:2016-07-25 19:19:12

标签: php ajax

我有这个简单的HTML表单

<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>

和这个jquery

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

这就是我提交表格的方式

$(".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;                   
});

但我不知道如何用PHP接收这个AJAX调用

2 个答案:

答案 0 :(得分:1)

为了使表单元素显示在任何请求数组中,例如$_POST,它们将需要名称属性(name="<something>")。例如:

<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>

如上所述,<textarea>不使用类型。

答案 1 :(得分:0)

这样做的一种方法是:

<script>

supportchatinterval = 5000;

$(document).ready(function(){
    checkForChangesSupport();
    setTimeout(checkForChangesSupport, supportchatinterval);
});

function checkForChangesSupport() {
   $.get("getCountSupport.php", function(response) {
      if(response == 1) {
         refreshTableSupport();
      }
setTimeout(checkForChangesSupport, supportchatinterval)
   });
}

function refreshTableSupport(){
    $('#tableHolderSupport').load('getTableSupport.php');
}

</script>

<script type="text/javascript">

function onBlur(){
document.body.className = 'blurred';
$.get("afk.php?afk=1");
supportchatinterval = null;
clearInterval(supportchatinterval);
};

function onFocus() {
document.body.className = 'focused';
$.get("afk.php?afk=0");
    supportchatinterval = 5000;
refreshTableSupport();
}

</script>