...

View Full Version : problem in retrieving data from mysql into specified fields on webpage



sindhump83
10-27-2009, 07:02 AM
Hi,

Here's wat im trying to do. I have a list box populated by mysql database. On select of a particular element i want all the details of that in their specified text fields or text areas on the web page so that the user can modify or update the details and submit.

The code till now is as follows:
modify species page:


<html>
<head>
<script type="text/javascript">
var xmlhttp;

function showMod(str)
{
xmlhttp=GetXmlHttpObject();

if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}

var url="getdspe.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}

}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}

if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
</script>
<title>Add Species information</title>
</head>
<frameset rows="40%,60%">

<frame src="frame_a.htm">


<form method="post" action="http://...">

<font size=3><b><i>Species : &nbsp;&nbsp;</i></b></font><select name="sname" onchange="showMod(this.value)">
<option>-- Select One --</option>
<?php

$con = mysql_connect("localhost","user", "pwd") or die(mysql_error());

mysql_select_db("dbname") or die ( "Couldn't open : ".mysql_error() );

$query = "SELECT * FROM species";

$result = mysql_query($query) or die(mysql_error());

for($i=0;$i<mysql_num_rows($result);$i++)

{

$row=mysql_fetch_assoc($result);

echo '<option >'.$row[sname].'</option>';

}

?>

</select>
<br />

<br><div id="txtHint"><b>Species info will be displayed here.</b></div>

<center><input type="reset" name="submit" value="Reset">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Submit"></center>
<center></center>
</form>
</frameset>
</html>


And the getdspe.php file is as follows




<html>

<head>

<title>asdf</title>

<script type="text/javascript">



</script>

</head>

<body>

<form method="post" action="http://......">

<?php



$q=$_POST["q"];



$con = mysql_connect('localhost','user','pwd');



if (!$con)

{

die('Could not connect: ' . mysql_error());

}



mysql_select_db("dbname", $con);

$sql = "SELECT * FROM species WHERE sname = '".$q."'";



$result = mysql_query($sql);



while ($row=mysql_fetch_array($result))

{
$sname=$row["sname"];

$synonyms=$row["synonyms"];

$distribution=$row["distribution"];

$author=$row["author"];

$Extralimital=$row["Extralimital"];

$variety=$row["variety"];

$vauthor=$row["vauthor"];

$form=$row["form"];

$fauthor=$row["fauthor"];



}


echo "<table border=\"0\" cellpadding=\"6\" cellspacing=\"6\">";

echo "<tr>";

echo "<td><font size=3><b><i>Species :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"sname\" value=\"$row[sname]\" /></td>";



echo "<td><font size=3><b><i>Author :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"author\" value=\"$author\" value=\"$row[author]\" /></td>";

echo "</tr>";



echo "<tr>";

echo "<td><font size=3><b><i>Variety :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"variety\" value=\"$row[variety]\" /></td>";



echo "<td><font size=3><b><i>Variety Author :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"vauthor\" value=\"$row[vauthor]\" /></td>";

echo "</tr>";



echo "<tr>";

echo "<td><font size=3><b><i>Form :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"form\" value=\"$row[form]\" /></td>";



echo "<td><font size=3><b><i>Form Author :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"fauthor\" value=\"$row[fauthor]\" /></td>";

echo "</tr>";



echo "<tr>";

echo "<td><font size=3><b><i>Synonyms :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"synonyms\" value=\"$row[synonyms]\" /></td>";



echo "<td><font size=3><b><i>Extralimital :</i></b></font></td>";

echo "<td><input type=\"text\" name=\"Extralimital\" value=\"$row[Extralimital]\" /></td>";

echo "</tr>";



echo "<tr>";

echo "<td><font size=3><b><i>Distribution :</i></b></font></td>";

echo "<td><textarea name=\"distribution\" rows=\"5\" cols=\"25\" value=\"$row[distribution]\" > </textarea><br /> </td>";

echo "</tr>";



echo "</table>";




mysql_close($con);

?>

</form>

</body>

</html>





Pls help me out..... I know my mistake is in the retrieval part.
Help any one...:(

Thanks

abduraooft
10-27-2009, 08:05 AM
To begin with, I'd recommend to use a basic form with a submit button(without any ajax) in the first page, and submit run the query from the target page based on the value from the drop down.

SB65
10-27-2009, 02:32 PM
Isn't this the problem:


$q=$_POST["q"];

Aren't you using GET instead of POST from Ajax - so this should be:


$q=$_GET["q"];

Phil Jackson
10-27-2009, 02:36 PM
xmlhttp.open("GET",url,true);
looks like it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum