Forum Moderators: open

Message Too Old, No Replies

PHP response extra new line char to AJAX request

PHP and AJAX

         

jayapalchandran

2:47 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



My PHP page responds to an AJAX request like the following

<?php
if( $_POST["username"] == "samy" )
echo "yes"
else
echo "no"
?>

in my javascript code i am checking the response text for yes or no like the following

if(ajax.responseText=="yes")
alert("Welcome samy");
else
alert("invalid username");

the problem is before the letter y in yes a new line is inserted...
why it is so... is there any method to stop it while sending the response back from the php page... it is fine to do manual removal or split of new line but i want to know why it is a case and what has to be done...?

Gibble

2:49 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is the <? on the very first line of your php file?

jayapalchandran

8:01 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



yes

eelixduppy

12:50 am on Sep 28, 2007 (gmt 0)



It doesn't really make sense from what you are showing. Make sure there is absolutely no whitespace in your php file that could be causing this. Also, there should be semicolons at the end of those echo statements.

Gibble

2:29 pm on Sep 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And for that matter, it could be written as:

<?php echo ($_POST["username"] == "samy")? "yes" : "no";?>

...I think I got the syntax correct, I don't have anything to test it here. But one line, no chance of whitespace. Could you perhaps post more code if what you have was just a sample?

vincevincevince

2:33 pm on Sep 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure there is such a character being output? Perhaps it is from your AJAX handling, or it is just a margin?

jayapalchandran

4:32 pm on Dec 25, 2008 (gmt 0)

10+ Year Member




System: The following message was spliced on to this thread from: http://www.webmasterworld.com/javascript/3814344.htm [webmasterworld.com] by coopster - 7:33 am on Dec. 26, 2008 (utc -6)


when i logged in my account to give a reply for this topic i couldn't see the reply button so i am posting it again... people can use it to solve theirs....

as the other replies said that there should not be any new line i removed all but still i have a long code where i have formatted for readability so the extra new line is been inserted when the response has been received using ajax...

what i didn was to do a ob_end_clean and then i use to give any echo statement. so if there has to be any new line for the response that will be cleared and the echoed values alone is been fetched by ajax...

if any one like to comment on this then it could be nice...

coopster

1:36 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sounds like it was exactly what these good folks figured, extra whitespace. Glad you got it sorted.
The reason you could not respond is because the thread went stagnant, it has been over a year since the last message.