Jump to content

Ajax POST method


arunpatal

Recommended Posts

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.....

Link to comment
https://forums.phpfreaks.com/topic/283865-ajax-post-method/
Share on other sites

<?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?

Link to comment
https://forums.phpfreaks.com/topic/283865-ajax-post-method/#findComment-1458139
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/283865-ajax-post-method/#findComment-1458174
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.