0

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>

1 Answer 1

2

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
}
?>
Sign up to request clarification or add additional context in comments.

8 Comments

@jasondavis - you've got $_REQUEST['comment'] in your PHP but you're sending 'message'!
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 - if (isset($_REQUEST['comment'])) { } means that in your JS you need var dataString = 'comment='+ message; and NOT var dataString = 'message='+ message;
@jasondavis - try this: var dataString = 'comment=blahblah'; This is driving me nuts!
yeah me too, I starting to think I will never get it working. and I tried that but nothing happened
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.