arunpatal Posted November 13, 2013 Share Posted November 13, 2013 Hi, I am fetching username.... This is PHP Code for fetching.. <?php include("ajax.php"); include("connection.php"); $sql = mysql_query("select * from $demo") or die (mysql_error()); while ($row = mysql_fetch_array($sql)){ echo '<input id="name" value="'.$row["username"].'" size="50" type="text" />'; echo '<input type="submit" value="Add Category" onClick="javascript:test_function();"><br>'; } ?> <div id="status"> </div> Now i am passing username value to ajax by clicking submit..... Here is Ajax Code <script language="JavaScript" type="text/javascript"> function test_function(){ var hr = new XMLHttpRequest(); var url = "php.php"; if (document.getElementById("name")){ var ca = document.getElementById("name").value; var vars = "name="+ca; } hr.open("POST", url, true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if(hr.readyState == 4 && hr.status == 200) { var return_data = hr.responseText; document.getElementById("status").innerHTML = return_data;}} hr.send(vars); document.getElementById("status").innerHTML = "<img class='loading' src='load.gif' alt='loading...' />";} </script> Now am echoing the passed value <?php echo $_POST["name"]; ?> But the problem is that the script is echoing alway first row username..... Quote Link to comment https://forums.phpfreaks.com/topic/283865-ajax-post-method/ Share on other sites More sharing options...
dalecosp Posted November 13, 2013 Share Posted November 13, 2013 <?php echo $_POST["name"]; ?> But the problem is that the script is echoing alway first row username..... Isn't that because you asked it to? $sql = mysql_query("select * from $demo") or die (mysql_error()); Perhaps I don't understand the issue. Incidentally, isn't "XMLHttpRequest" only available in FireFox and Chrome? Are you not supporting MS browsers? Quote Link to comment https://forums.phpfreaks.com/topic/283865-ajax-post-method/#findComment-1458139 Share on other sites More sharing options...
Irate Posted November 13, 2013 Share Posted November 13, 2013 As .josh stated in an announcement in here, use jQuery for Ajax requests. It is A LOT easier than re-inventing the same wheel over and over again. Quote Link to comment https://forums.phpfreaks.com/topic/283865-ajax-post-method/#findComment-1458146 Share on other sites More sharing options...
kicken Posted November 14, 2013 Share Posted November 14, 2013 Incidentally, isn't "XMLHttpRequest" only available in FireFox and Chrome? Are you not supporting MS browsers?[/color] XMLHttpRequest is supported in IE7+. It's still advisable to use something like jQuery though to simplify things and smooth out any other compatibility issues. Quote Link to comment https://forums.phpfreaks.com/topic/283865-ajax-post-method/#findComment-1458174 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.