Link to home
Start Free TrialLog in
Avatar of minnirok
minnirok

asked on

echo is printing two extra characters

Hi,

I have a simple php script:

    <?php
        // thePhpScript.php
        echo "hello";
    ?>

I am calling it from a javascript function vis a vie some ajax stuff like so:

    <script>
    function callScriptAJAX() {
        httpObj.open("GET", "thePhpScript.php", true);
        httpObj.onreadystatechange = handleHttpResponse;
        httpObj.send(null);
    }


And then I try to print the response like so:

    function handleHttpResponse() {

        if (httpObj.readyState == 4) {
            alert("[" + httpObj.responseText + "], length is: " + httpObj.responseText.length);
        }
    }    
    </script>

But the response printed in my message box is always:

    [
     hello], length is: 7

Why is my php script returning a carriage return and a white space before the word 'hello'!? It's driving me crazy. I thought it would print back perfectly [hello], length is 5.

Please let me know what I'm doing wrong, thanks!
Avatar of BogoJoker
BogoJoker

Hi minnirok,

Does the 'true' in  httpObj.open("GET", "thePhpScript.php", true); give it that new line character "\n" that would be the extra two characters in the string.

Joe P
Avatar of minnirok

ASKER

Hmm I don't think so, it's just a parameter specifying that I want the request to be asynchronus.
Well, it really looks like something is printing a new line.  I am not too familer with AJAX, I don't think its a problem on the php side.
I am not too sure its a carriage return.

Joe P
ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Haha gosh, yes I had one extra bit of comment junk at the top of the file - once I took it out it started working fine.

Thanks!