JasonDavis
JasonDavis

Reputation: 48933

jquery not sending my form data

The code below does not return any errors and I can make it return data from process.php however on process.php I am checking for "message" like this:

<?PHP
if (isset($_REQUEST['message'])) {
    //return a json string
}
?>

Here is my jquery code below, dataString shows "message=WHATEVER I TYPE IN THE TEXTAREA" when I use alert (dataString); but it act like it is not being sent to the processing.php script, I am at a dead end right now

If i go to www.url.com/processing.php?message=whatever then the php script shows what you would expect.

Also it seem the ajax part is working because it will return a response to the script if I have the php script output something without wrapping it in my if statement

What could the problem be?

<script>
var dataString = 'comment='+ message;
//alert (dataString);

// send message to PHP for processing!
$.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    dataType: "json",
    success: function (data) {
    // do stuff here
    }

});
</script>

Upvotes: 0

Views: 536

Answers (1)

karim79
karim79

Reputation: 342635

EDIT: The problem is here:

var dataString = 'message=' + message;

Should be:

var dataString = 'comment=' + message;

Try:

<?php
if (isset($_POST['comment'])) {
    //return a json string
}
?>

Also make sure that the "name" attribute is exactly "comment":

var dataString = 'comment=' + message;

Note that it's prettier to use array_key_exists for checking whether or not something is set in one of the superglobals:

<?php
if (array_key_exists("comment",$_POST)) {
    //return a json string
}
?>

Upvotes: 2

Related Questions