php - confusion about basic AJAX code -
<script> try { function xmldo() { var xmlhttp; xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { document.getelementbyid("para").innerhtml = xmlhttp.responsetext; } } var url = "http:\\127.0.0.1\ajax.php"; xmlhttp.open("get", url, true); xmlhttp.send(); } } catch (err) { document.write(err.message); } </script> <p id="para">hey message change</p> <br> <button type="button" onclick="xmldo()">click me</button>
this code webpage want change content of #para.innerhtml respnse in php file ajax.php
<?php $response="hey text changed"; echo $response; ?>
i using wamp placed ajax.php in www folder , set location of file on server 127.0.0.1/ajax.php [url] on pressing button text @ para placeholder not getting changed. new ajax must missing on points. plz me them.
change slashes in url.
you have: http:\\127.0.0.1\ajax.php
correct way is: http://127.0.0.1/ajax.php
i suggest using jquery perform ajax requests - it's simpler , write less code!
i've added example below written in jquery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#button_id").click(function(){ $.ajax({ url: 'http://127.0.0.1/ajax.php', cache: false, type: 'get', datatype: 'html', error: function (e){ console.log(e); }, success: function (response){ $("#para").empty().append(response); } }); }); }); </script> <p id="para">hey message change</p><br> <button type="button" id="button_id">click me</button>
Comments
Post a Comment