user466534
user466534

Reputation:

list names of USA by php &ajax

i am trying to write program,which ask user to enter some letters or strings and by this string,list these cities ,for which this string or letter is prefix i have following html file

<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","USA.php="+str,true);
xmlhttp.send();
}
</script>

</head>
<body>
<h3>entering  any city names which you like  </h3>
<form action="USA.php" method="post" >
CITY :<input type="text" name="city" value=" " onkeyup="showHint(this.value)"></br>
<input type="submit" value="Submit"/>
</form>

<p>Suggestions: <span id="txtHint"></span></p> 
</body>
</html>

and also USA.php file

<html>
<body>
<?php
$c=$_GET['city'];
$d=strlen($c);

$filename=file("UScities.txt");
$out=" ";
foreach ($filename as $line)
{

if (strtolower($c)==substr($line,0,$d)){
if($out=="")
{
$out=$line;
}
else
{
$out=$out. ' , ' . $line;
}
}
}
echo $out

?>
</body>
</html>

but when i run it in firefox,it gave me following error

Notice: Undefined index: city in C:\xampp\htdocs\united\USA.php on line 4

but i could not understand this error, why is city undefined index? EDITED:

html code
<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET", "USA.php?city="+str, true);
xmlhttp.send();
}
</script>

</head>
<body>
<h3>entering  any city names which you like  </h3>
<form action="USA.php" method="post" >
CITY :<input type="text" name="city" value=" " onkeyup="showHint(this.value)"></br>
<input type="submit" value="Submit"/>
</form>

<p>Suggestions: <span id="txtHint"></span></p> 
</body>
</html>

php file

<html>
<body>
<?php
$c=$_POST['city'];
$d=strlen($c);

$filename=file("UScities.txt");
$out=" ";
foreach ($filename as $line)
{

if (strtolower($c)==substr($line,0,$d)){
if($out=="")
{
$out=$line;
}
else
{
$out=$out. ' , ' . $line;
}
}
}
echo $out

?>
</body>
</html>

Upvotes: 0

Views: 98

Answers (2)

sephoy08
sephoy08

Reputation: 1114

Here try this one:

xmlhttp.open("GET","USA.php?city="+str,true);

Ok, this is the problem. you request for hint on USA.php and you submit your form also in USA.php. Yet on requesting for list of hint, you used

xmlhttp.open("GET","USA.php?city="+str,true);

however you form used post method

<form action="USA.php" method="post" >

the problem is on USA.php you used post

$c=$_POST['city'];

I recommend you change the ff.

$c=$_POST['city']; => $c=$_GET['city'];

<form action="USA.php" method="post" > => <form action="USA.php" method="get" >


Last edit: Change your condition into this my friend. and your all set!

if (strtolower($c)==strtolower(substr($line,0,$d)))

Upvotes: 2

Sirko
Sirko

Reputation: 74086

You query string in the JavaScript is wrong. Change the following line:

xmlhttp.open("GET", "USA.php?city="+str, true);

Upvotes: 2

Related Questions