1

I'm trying to send a simple string to the database. In order to see if I'm half way there, I'm checking to see if $_POST has anything set -- but it doesn't.

Here is my simple form, on an index.php:

<form action="" method="POST">
<input type="text" name="add_item" id="add_item">
<input type="button" value="submit" id="button">
</form>

And here is the AJAX from that form, where the success is triggered:

var add_item;
        $('#button').click(function(e){
            e.preventDefault();
            add_item = $('#add_item').val();
            console.log(add_item);

            $.ajax({
                type: 'POST',
                url: 'sendData.php',
                data: 'send me!',
                success: function(data){
                    alert('lol');
                }
            });
        });

And on the sendData.php, but there is nothing in the $_POST global:

var_dump($_POST); // returns 0
if (isset($_POST['add_item'])):
    $add_item = $_POST['add_item'];

    $submit = new Connection();
    $submit->connect_db();
    $submit->add_to_DB($add_item);

endif;

As you can see, I'm trying to send it to the database; but before that I'm trying to see if anything is in the $_POST global -- but it returns 0.

Would anyone know why? I'm new to this and I can't find any concrete tutorials explaining simply.

1
  • What would you expect to get in $_POST when you send send me!? Commented Mar 30, 2014 at 18:22

1 Answer 1

2

Try setting data to something like "testvar=sendme" or simply use a PlainObject

PHP seems to be expecting key-value-pairs for $_POST to work.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 . However, correcting the code work might be helpful to OP.
Yes, still getting used to StackOverflow. Since OP for whatever reason was only interested in seeing if there was any POST data at all, I didn't bother to fix all the other flaws and focused on his question.
Thanks, but 'testvar=sendme' still has nothing in the post global. Is this something I could see by refreshing the sendData.php page, or is this something behind the scenes? For example, if I wanted to retrieve 'name' => 'something', what would I send from the AJAX?
either data: "name=something" or data: {"name":"something"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.