0

I am new to Ajax. In my Ajax I get the following error message :

Notice: Undefined index: address in C:\wamp\www\test\sample.php on line 4

I googled but I didn't get a solution for my specified issue.

Here is what I did.

HTML Form with Ajax (test1.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("mydiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","test2.php",true);
xmlhttp.send();
}
</script>


</head>

<body>
<form id="form1" name="form1" method="post" action="test2.php">
  <p>
    <label for="address"></label>
    <input type="text" name="address" id="address" onblur="loadXMLDoc()"  />
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
  <p><div id = 'mydiv'></div></p>
</form>
</body>
</html>

test2.php

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
echo "Your Address is ".$_POST['address'];
?>

</body>
</html>

I am sure it is very simple issue but I don't know how to solve it. Any help ?

klari
  • 25
  • 2
  • 5
  • what is the url you typed in the address bar?? "C:\wamp\www\test\sample.php" or "localhost/test/sample.php" – Chella Dec 15 '12 at 11:14
  • are you checking it on localhost or filesystem address kind of url? – Jai Dec 15 '12 at 13:11

2 Answers2

2

You are not sending any input with a name of address

I assume if you rename input name="mail"... to input name="address" some light will be shed on the situation.

As an alternative solution... Change:

<?php
echo "Your Address is ".$_POST['address'];
?>

to this

<?php
echo "Your Address is ".$_POST['mail'];
?>
Dale
  • 10,384
  • 21
  • 34
  • This is not the problem. $_POST['mail'] is the variable in my project but I forgot to change in my example code. Now I edited the sample code but the situation is same. I think the issue is something else – klari Dec 15 '12 at 11:43
  • If you aren't going to post the code with the problem but pseudo code instead we will never be able to help you – Dale Dec 15 '12 at 11:45
  • For my curiosity, when I submit the page, the value is passed to the test2.php, but the same not passed when I use Ajax. What may be the problem – klari Dec 15 '12 at 12:00
  • I surprise why one can't post sample code!. Why I post all of my codes for an issue in a small part of the code ?. – klari Dec 15 '12 at 20:21
2

I didn't see 'address' in your HTML code. You have one text box:

<input type="text" name="mail" id="mail" onblur="loadXMLDoc()"  />

But it name is 'mail', not 'address' You can do this:

<?php
echo "Your Address is ".$_POST['mail'];//instead of 'address'
?>
user0103
  • 1,246
  • 3
  • 18
  • 36
  • This is not the problem. $_POST['mail'] is the variable in my project but I forgot to change in my example code. Now I edited the sample code but the situation is same. I think the issue is something else – klari Dec 15 '12 at 11:41
  • For my curiosity, when I submit the page, the value is passed to the test2.php, but the same not passed when I use Ajax. What may be the problem – klari Dec 15 '12 at 12:00