$ _POST返回空

Trying to practice ajax and PHP a little, I can't figure out why whenever I send something, it returns empty or undefined index.

PHP:

<?php

    if (!empty($_POST['message'])) {
        echo 'works';
    } else {
       echo 'empty';
    }

?>

returns empty.

JS:

$('form').submit(function(){

    var meth = $(this).attr('action');

    var msg = $('.msg');
    $.post(meth, function(data) {
        $('.messages').append(data);
    }) ;

    return false; 
}); 

HTML:

<form class="send" action="process.php" method="post">
    <textarea type="text" name="message" class="msg" value="blah"></textarea>
    <input type="submit" name="submit" class="submit">
</form>

If I just do 'echo 'test'; in the process.php file, it works; undefined index if otherwise.

What am I doing wrong?

Try putting the value of the text area between the <textarea></textarea> tags, like so:

<textarea type="text" name="message" class="msg">blah</textarea>

Textarea functions differently than a typical <input /> field.

I see you've edited the syntax errors, so you can ignore this section:

Well of the top of my head I see two syntax errors, one in php:

<?php


  if (!empty($_POST['message'])) {
    echo 'works';
  } else [ // <- This should be a {
     echo 'empty';
  }

?>

and one in JS:

$('form').submit(function(){

      var meth = $(this).attr('action');

      var msg = $('.msg');
      $.post(meth, function(data) {

        $('.messages').append(data);

      }) // <- Should be a ; here

      return false; 
    });

Also let me be the first to point out that you should indent your code properly. It's hard to see the depth of your scope otherwise, and just makes your code plain unreadable.

empty is the wrong function to be using to check if an index on an array is set. That's what's generating your "undefined index" warnings. Use isset or array_key_exists instead:

if (!isset($_POST['message'])) {
// or
if (!array_key_exists('message', $_POST)) {

The empty function just checks if the variable contains a value that evaluates to empty, such as an empty string, an empty array, a NULL etc. It doesn't check if a variable is set.


Additionally, you don't set the value of a textarea by populating the value attribute. That won't work. You set the value by placing it between the open and close tags like this:

<textarea class="msg">My message</textarea>

Then you fetch the text (in jQuery) by requesting the value

var my_message = $('textarea.msg').val()

Since you aren't using default behavior, you have to pass along the names and values of form fields yourself for PHP to receive any of them:

// ...
$.post(meth, $(this).serialize(), function(data) {
// ...

You may also consider using array_key_exists to avoid undefined errors:

if (array_key_exists('message', $_POST)) {
    # ...
}

The best way to test this, is to echo back the sent data:

<?php
  if (isset($_POST['message'])) {
    echo $_POST['message'];
  }
?>

If you get the action property with prop() it will usually get you the absolute link, which is better IMO.

$('form').on('submit', function(e){
    e.preventDefault();
    var meth = $(this).prop('action'),
        Mymsg = $('.msg').text(); //assuming the element contains text
    $.post(meth, {message: Mymsg}, function(data) {
        $('.messages').append(data);
    });
});

To send the entire form use serialize, was going to add that, but see Jonathan's answer instead.