0

I created this small script, from what I have learned about Ajax + Javascript so far. I am running this in Chrome btw, so XMLHttpRequest(); should be working

PHP,

    $conn = new PDO('mysql:host=localhost; dbname=test', 'root', '')

    $stmt = $conn->query("SELECT text FROM ajax");
    foreach($stmt as $each){

?>

Ajax,

<script type='text/javascript'>

    var request = new XMLHttpRequest();
    request.open('GET', <?php echo $each['text']; ?>, false);
    request.send();
    console.log(request);

</script>
<?php } ?>

Now, in database test where tbl name is ajax I have rows id & text, in which text has three filled-in rows, but the ajax code is not showing me anything. It is not echoing anything at all, let alone update instantly, when I am adding texts in my rows. What am I doing wrong?

3
  • 1
    I would recommend you to use jQuery for ajax requests.. Commented May 9, 2013 at 15:07
  • I would love to @Dremp but, I have no clue of jQuery Commented May 9, 2013 at 15:07
  • that code.. can get changed into $.get('<?php echo $each['text']; ?>', function(data) { /* console.log(data); */}); Commented May 13, 2013 at 16:54

2 Answers 2

1

out.php

<?php
     $conn = new PDO('mysql:host=localhost; dbname=test', 'root', '')

     $stmt = $conn->query("SELECT text FROM ajax");
     echo json_encode($stmt);
?>

test.html

<script>
    var request = new XMLHttpRequest();
    request.open('GET', "http://mysite.com/out.php", false);
    request.onreadystatechange=function()
    {
        if (request.readyState==4 && request.status==200)
        {
             var data = JSON.stringify(request.responseText); //The data from the server will be in responseText
             //data now contains an array of JSON objects of your data
             for(i=0;i<data.length;i++) {
                 console.log(data[i].text);   //.text is a variable based on your MYSQL field
             }
        }
    }
    request.send();
    console.log(request);
</script>

First you need to make a php script that outputs the data straight out nothing else (as in the out.php section above) and then have the javascript script in a whole other page (as in the test.html section) and give the request.open call the url to out.php in the example above http://mysite.com/out.php, but replace that with your actual url to the out.php file, and since you are on localhost something like http://localhost/Portal/Test/out.php

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

8 Comments

I tried your code, although I undestood some of the mistakes I've made, but I did not get the script to work. Nothing is being desplayed: btw: I replaced all my ajax script with mine.
I just reedited my answer a moment ago i had xmlhttp.readyState==4 && xmlhttp.status==200 when it needed to be request.readyState==4 && request.status==200 to match your variable name, did you copy the older version?
And is your text field a url or just text? if it is just text that wont work, the ajax call needs a url to where the data it is trying to retrieve is being outputted.
It is pure text, so how do I assing the url? Sorry, for the noob question. Just show me that one, and I'll stop bothering you
I am on localhost btw: localhost/Portal/Test/ajax.php
|
0

First I would remove the foreach loop. Change it to the following:

echo json_encode($stmt);

That will give your AJAX request variable a JSON encoded object to work with. Currently your result variable from your AJAX request is only displaying the results within the console.

Maybe try changing console.log(request) to the follwing:

alert(JSON.stringify(request));

2 Comments

Changed it, but How do I manupilate the the object inside the ajax code?
JSON objects are the equivalent of an associative array in PHP. In other words instead of using a numerical value for the values key, a string can be used. Research JS & JSON objects as there are tons of examples of looping through the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.