tags:

views:

137

answers:

1

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>
+2  A: 

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
}
?>
karim79
@jasondavis - you've got $_REQUEST['comment'] in your PHP but you're sending 'message'!
karim79
thanks for the typo fix, also the array_key_exists I have never used that for, it does look nice though, unfortunately none of this fixed the problem, well obviously they fixed a problem but it still is broke I should say
jasondavis
@jasondavis - if (isset($_REQUEST['comment'])) { } means that in your JS you need var dataString = 'comment='+ message; and NOT var dataString = 'message='+ message;
karim79
Yeah I fixed that too
jasondavis
@jasondavis - try this: var dataString = 'comment=blahblah'; This is driving me nuts!
karim79
yeah me too, I starting to think I will never get it working. and I tried that but nothing happened
jasondavis
@jasondavis, just do what I said bellow.
Cleiton
OK I just set this var dataString = 'comment=test'; and in processing.php I put some code to write to a text file, and it wrote "test" to the file, so it is getting sent, this is crazy
jasondavis
I got it working now, not sure exactly what it was still, thanks for your help
jasondavis