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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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