-1

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

pah
  • 4,700
  • 6
  • 28
  • 37
COp
  • 25
  • 2
  • 12

2 Answers2

0

One way to do it is this way:

switch (strtolower($_SERVER['REQUEST_METHOD'])) {
    case 'post':
        $postdata = file_get_contents('php://input'));
        //Do Something For Post
        break;
    case 'get':
        $getdata = file_get_contents('php://input'));
        //Do Something For Get
        break;
}
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Aaron
  • 1,208
  • 1
  • 9
  • 21
  • fyI: switch/case requires "break". edit: ah you added it now – Funk Forty Niner Jul 25 '16 at 19:22
  • I am sure there far more less complicated ways.If i had enough reputation i would downvote too – COp Jul 25 '16 at 19:24
  • @COp "I am sure there far more less complicated ways." So you would downvote based on your assumption that something exists? – Aaron Jul 25 '16 at 19:24
  • It is not assumption. – COp Jul 25 '16 at 19:25
  • Sure.I did not mean to offend you in any. @Aaron – COp Jul 25 '16 at 19:28
  • @COp I'm not offended, I just want to believe what you are saying. – Aaron Jul 25 '16 at 19:29
  • 1
    The less complicated way, since the AJAX request is made as `type: 'POST'` is to read the `$_POST` array. In addition, the addition of `file_get_contents()` adds an unnecessary layer of complexity to a simple AJAX request. – Jay Blanchard Jul 25 '16 at 19:30
  • @JayBlanchard That's not less complicated: For example, If the content-type is `application/json` the `$_POST` method will not work because the `$_POST` wrapper does not know how to handle it. Less complicated is subjective here. The original poster gets a pass here, but if he wishes to send JSON, your method will not work. – Aaron Jul 25 '16 at 19:32
  • Wait....whut? You can't put JSON in a `$_POST` array variable? Golly, I'm going to have to go back and change hundreds of AJAX requests. – Jay Blanchard Jul 25 '16 at 19:34
  • It is a simple AJAX request using the POST method. How else do you expect to send form variables to a PHP page for processing? That is what the OP is asking for. – Jay Blanchard Jul 25 '16 at 19:37
  • @JayBlanchard What you're saying is true, but the OP doesn't need JQuery for that. – Aaron Jul 25 '16 at 19:43
  • No, but he has chosen to use it. – Jay Blanchard Jul 25 '16 at 19:43
  • @JayBlanchard And That's why my answer is also valid. – Aaron Jul 25 '16 at 19:44
  • Well, it would be valid if it contained an explanation of why you use the code. – Jay Blanchard Jul 25 '16 at 19:45
0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119