www.webdeveloper.com
Results 1 to 4 of 4

Thread: AJAX - passing values back from PHP scripts using echo()

  1. #1
    Join Date
    May 2007
    Posts
    44

    AJAX - passing values back from PHP scripts using echo()

    I have a wierd problem (wierd to me as a noobie that is)...

    I have an html script that uses Javascript to call a PHP script using GET. The PHP script returns a string using an echo() statement.

    a var response = http.responseText statement in javascript is used to get the returned value,

    and a document.getElementById('hw').innerHTML = response; statement puts the string value between <div> tags in the html page.

    Simple. However, I've found that if the PHP tries to either echo a string directly:

    echo "Hello World";

    or assign it to a variable first and then echo the variable:

    $x="Hello World"; echo "$x";

    nothing happens, even though I don't get any javascript or PHP errors.

    However, if the string is stored in a .txt file, and I retrieve it from the file, assign it to a variable, and then echo the variable, it works!

    The same thing happens with numbers.

    So whenever I want to assign a value to a variable and send it back, I have to store it in a file first, then retrieve it and echo it. There must be a better way. How do I just send the value stright back

  2. #2
    Join Date
    Feb 2007
    Posts
    22
    Without seeing any code I can't specify the exact problem but it's mostly likely to be with the PHP code. It would explain why the text file works.

    If there are no errors it might be that the code you want isn't being run. If it's in a function, is the function being called? If it's called through an if statement, is the condition being met?

    If you have any ifs in your PHP, add elses to them to try and catch any errors.

  3. #3
    Join Date
    May 2007
    Posts
    44
    Here's the PHP code I was experimenting with in its entirety. There are no if statements. Also, for what its worth there are no html tags around it.

    <?php

    $t=array();
    $r = "changedetect1.txt";
    $open = fopen($r, "r");
    $t=file($r);
    fclose ($open);

    $x=$t[0];
    $x=trim($x);
    echo "$x";

    //echo "hello world";

    //$y="hello world";
    //echo "$y";

    ?>

    changedetect1.txt has a single value or a string stored in it.

    echo "$x" works.

    However if I comment out the echo "$x" statement and un-comment any of the statements below it, nothing happens.

    If I just run the PHP program by itself, just by typing its URL into the browser, it works fine - all the echo statements work.

  4. #4
    Join Date
    Feb 2007
    Posts
    22
    Then it would appear to be the JavaScript at fault. Use Firebug (http://www.getfirebug.com/) with Firefox to find out the proper response of the Ajax call which should work fine but it may also pick up an error that may not have been picked up before.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles