0

My first question so here it goes.

My goal is to have a simple form post and if everything checks out proceed.

$.post("anotherpage.php", $("#form").serialize(),function(data){

if(data == 'sent'){
alert("True:  " + data);
}    else{
alert("False:  " + data);
}

$("#somediv").text(data);

     });   

here is 'anotherpage.php'

<?php

echo 'sent'; 

?>

Here is my problem, '#somediv' shows the word 'sent' but my function fails every time. Also when it does fail when it alerts it looks like this. "False: s..." It makes me think I have some problem up the stream that is effecting this but I don't know what.

6
  • 1
    Try console.log(data) with Firebug or Chrome to see what's actually in data. Commented Jul 26, 2011 at 14:04
  • Is "False: s..." literally what you are seeing? Commented Jul 26, 2011 at 14:05
  • You should also ensure that you don't have any whitespace after the word "sent" coming from the server. Try console.log(data.length) and check that it prints "4" Commented Jul 26, 2011 at 14:07
  • It is what i am seeing but i believe the entire word is there but it only shows "s...". if i change it to "1234" in return i see "1...". Commented Jul 26, 2011 at 14:07
  • just tried console.log(data) and console.log(data.length) results: sent and 6??? This is the echo string copied. echo 'sent'; Commented Jul 26, 2011 at 14:12

2 Answers 2

1

When trying to be explicit with your HTTP response, it's easy to accidentally return extra whitespace, especially at the end of your PHP block (carriage returns after the closing angle-bracket being the usual suspects).

Some tips:

  1. Make sure there is no whitespace before the start of your php block.
  2. Explicitly end the response with exit() on the last code line of your php block:

e.g.

  <?php

  echo 'sent';
  exit(); 

  ?>

There are a few cool functions in PHP regarding output control. You can buffer the output, clear it, store it or flush it. See here for more information.

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

3 Comments

Also, if you leave the closing ?> tag off then you don't ever have to worry about invisible whitespace after it.
True, but it sends a shiver down my pedantic spine ;) My code soul requires closure!
The Zend coding standard states that, when a file contains only PHP, you should leave the closing tag off. I think it is a cleaner solution than exit().
0

here is 'anotherpage.php'

<?php

echo "var status='sent';"; 

?>



 $.post("anotherpage.php", $("#form").serialize(),function(data){
alert(date);    
eval(date); // its make as perfect js var string;
    alert(date);
if(status == 'sent'){
    alert("True:  " + status );
    }    else{
    alert("False:  " + status );
    }

    $("#somediv").text(status );

         });  

2 Comments

Make sure that u access path is correct??? e.i anotherpage.php or http:\domain\anotherpage.php
First of all: eval is evil. It's better to return JSON, and use jQuery to parse it.

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.