php - how to display data with ajax in diffrent dom elements? -
i want display data ajax function receive php file different html elements.
function getdetails(x) { if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("fname").value=xmlhttp.responsetext; } }
use jquery this:
$.ajax({ type:'get', url:'url/to/the/file.php', }.done(function( msg ) { $('#id-of-element').html(msg); $('.class-of-other-element').html(msg); // element $('.class-of-another-element').html(msg); // element });
explanation:
type: // type of http request file, post url: 'url/to/the/file.php' // provide file , path file communicate .done (function(msg)) { // when ajax request completed (if), assign variable 'msg' contents of return. $('#id-of-element').html(msg); // assign return data html element of choice.
edit:
if you're expecting 2 variables, in php file return them this:
echo $var1."/".$var2 // '/' symbol never expect returned in either variable.
then in ajax:
$.ajax({ type:'get', url:'url/to/the/file.php', }.done(function( msg ) { var content = msg; var varsplit = msg.split('/'); //where '/' symbol chose use in stage above. var var1 = varsplit[0]; var var2 = varsplit[1]; $('.first-element').html(var1); $('.second-element').html(var2); });
Comments
Post a Comment