• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

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!
0
minnirok
Asked:
minnirok
  • 2
  • 2
1 Solution
 
BogoJokerCommented:
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
0
 
minnirokAuthor Commented:
Hmm I don't think so, it's just a parameter specifying that I want the request to be asynchronus.
0
 
BogoJokerCommented:
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
0
 
ravenplCommented:
   <?php
        // thePhpScript.php
        echo "hello";
    ?>
does the <?php
is in the very first line of the script?
0
 
minnirokAuthor Commented:
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!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now