...

View Full Version : Ajax/Php auto fill form



Phil_n
09-13-2010, 01:47 PM
Hi, I originally posted this in the php section but then as my script has evolved and i was told i needed ajax I have the php side fully working and the whole thing working in IE but not Firefox.

Im trying to achieve a form in which the user will use a select box and the choice will determine what some text fields are filled with allowing the user to still edit the textfield sif necersary.

I have the script fully working in IE as stated but not in Firefox. Below is my code:

Form Page


<html>
<head>
<script type="text/javascript">
function showSupplier(str)
{
if (str=="")
{
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","getsupplier.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onChange="showSupplier(this.value)">
<option value="">Select a person:</option>
<option value="1">Supplier1</option>
<option value="2">Supplier2</option>
<option value="3">Supplier3</option>
<option value="4">Supplier4</option>
</select>
</form>
<br />
<div id="txtHint"><b>Supplier info here</b></div>

</body>
</html>


getsupplier.php:

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', '', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("", $con);

$sql="SELECT * FROM suppliers WHERE Supplier_ID = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Supplier</th>
<th>Contact</th>
<th>Telephone</th>
<th>Email</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Supplier'] . "</td>";
echo "<td>" . $row['Contact'] . "</td>";
echo "<td>" . $row['Telephone'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

Spudhead
09-13-2010, 02:58 PM
I think you need:

onChange="showSupplier(this.options[this.selectedIndex].value)"

Phil_n
09-13-2010, 03:31 PM
Thanks for your help.

I have just tried your suggestion but unfortunately it is the same result. Works in IE but not Firefox.

Reading around I believe it has something to do with the following part:

<script type="text/javascript">
function showSupplier(str)
{
if (str=="")
{
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","getsupplier.php?q="+str,true);
xmlhttp.send();
}
</script>

Spudhead
09-13-2010, 04:13 PM
Ok. I know this might sound like a stupid question - the fact that you're fetching a PHP page at least suggests otherwise - but you're not running this off a local machine are you? It'll need to be on a server to return a HTTP status code.

Phil_n
09-13-2010, 07:14 PM
no im not running this on my local machine it is on the server.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum