javascript - Pulling value from rows, using Ajax and PHP -
i created small script, have learned ajax + javascript far. running in chrome btw, xmlhttprequest();
should working
php,
$conn = new pdo('mysql:host=localhost; dbname=test', 'root', '') $stmt = $conn->query("select text ajax"); foreach($stmt $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
tbl name ajax
have rows id
& text
, in text
has 3 filled-in rows, ajax code not showing me anything. not echoing @ all, let alone update instantly, when adding texts in rows. doing wrong?
out.php
<?php $conn = new pdo('mysql:host=localhost; dbname=test', 'root', '') $stmt = $conn->query("select text 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 server in responsetext //data contains array of json objects of data for(i=0;i<data.length;i++) { console.log(data[i].text); //.text variable based on mysql field } } } request.send(); console.log(request); </script>
first need make php script outputs data straight out nothing else (as in out.php section above) , have javascript script in whole other page (as in test.html section) , give request.open
call url out.php in example above http://mysite.com/out.php
, replace actual url out.php file, , since on localhost http://localhost/portal/test/out.php
Comments
Post a Comment