2

I want to compare two values (both are simple strings) on ajax success. Value1 is echo from php, value2 is javascript variable. But when I try something like:

$.ajax({
    type: "POST",
    url: proccessPage,
    data: dataString,
    dataType: "text",
    success:function(result){

    alert (result);

    }
});

I will get message "[object Object]". When i try change alert (result); for

var response = $(result);
document.getElementById("div1").innerHTML = result;

then div with id "div1" has proper value. I know I can make the div hidden and use value from it, but is there some way to work with result directly? Or somehow convert result to string? Code in php:

if (isset($_POST['submit']) && $_POST['submit']=="true")
echo "string";

Thanks.

2
  • What is the format of your result? Commented Oct 31, 2012 at 16:02
  • What are you echoing from php ? Commented Oct 31, 2012 at 16:02

4 Answers 4

3

In your script, change the datatype to html.

dataType: "text",

to

dataType: "html",
Sign up to request clarification or add additional context in comments.

Comments

0

The dataType of text is perfectly ok. Make certain that your PHP script is setting the mime type for the return to text/plain.

PHP Code: header('Content-type: text/plain');

jQuery will process the return according to what the server says it is. The dataType field is only a hint. http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

If all else fails use something like Firefox with Firebug. It will give you the ability to place a break pint within your success closure and then you may inspect the value of your result variable.

Comments

0

I am having success by creating a json response from the PHP function, and loading it with whatever I need. Comparing known values from the Json Object handles your [object Object] return issue by giving you known object properties- declared with JSON:

$response = json_encode( array( 'success' => true , 'value1' => $my_result,);
header( "Content-Type: application/json" );
echo $response;
exit;

I use the success bool to ensure that $my_result is a successful response, because AJAX may execute properly, but this allows me to specifically check valid value1

Now, back in your $.ajax:

...
success: function(result){
  if(result['success']) { 
    document.getElementById("removeme").innerHTML = (result['value1'] == value2)? value1 : "Uh-oh. Something went wrong!";
  }
}
...

Or you can put whatever is appropriate in your success function body, whatever you need to compare or do. I just gave 1 example to show a complete implementation.

Comments

-1

The first A in AJAX means asynchronous, this means that your request will be executed as normal but the callback only gets executed when the requests gets a response. the script doesn't pause waiting for the response.

with that said, if you want to use information from an AJAX request you must use it in its callback, or it will not be available.

something like this:

$.ajax({
    type: "POST",
    url: proccessPage,
    data: dataString,
    dataType: "text",
    success:function(result){

        document.getElementById("removeme").innerHTML = result; 

    }
});

Comments

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.