I had been having probs last week getting this to work and no one could really seem to help me figure it out.. I got it working finally and wanna share it cause its pretty usefull and I havn't been able to find any examples of it anywhere.. So save yourself some time and grab this code....
Description: What this does is take info from a mysqldb and store it in javascript arrays, then you can dynamically update form feilds without refreshing the page..
Sorry the formatting didn't copy right..
Code:
<?php
mysql_connect (localhost, root, "");
mysql_select_db (Test_D😎;
$res = mysql_query ("SELECT * FROM T");
?>
<head>
<script language="JavaScript" type="text/javascript">
<!--
<?PHP
$x = "0";
while ($a = mysql_fetch_array($res))
{
$name = $a["Name"];
$age = $a["Age"];
$gen = $a["gender"];
echo "$x = new Array('$name','$age','$gen');
";
$x++;
}
?>
function fill(val) {
val2 = eval(val);
var id = val2;
var gen = id[2];
var age = id[1];
window.document.test.age.value = age;
window.document.test.gender.value = gen; }
//-->
</script>
</head>
<body>
<form name=test>
<select name=drp onChange="fill(window.document.test.drp.options.value)";>
<option value= ></option>
<?PHP
$res = mysql_query ("SELECT * FROM T");
$x = "0";
while ($a = mysql_fetch_array($res))
{
$name = $a["Name"];
$age = $a["Age"];
$gen = $a["gender"];
echo "<option value=_$x>$name</option>
";
$x++;
}
?>
</select>
<input type=text name=age value=>
<input type=text name=gender value=>
</form>
</body>
Heres the html output:
<head>
<script language="JavaScript" type="text/javascript">
<!--
0 = new Array('Chris','19','Male');
1 = new Array('Matt','30','Female');
2 = new Array('Jim','90','Male');
3 = new Array('Bob','46','Female');
function fill(val) {
val2 = eval(val);
var id = val2;
var gen = id[2];
var age = id[1];
window.document.test.age.value = age;
window.document.test.gender.value = gen; }
//-->
</script>
</head>
<body>
<form name=test>
<select name=drp onChange="fill(window.document.test.drp.options.value)";>
<option value= ></option>
<option value=0>Chris</option>
<option value=1>Matt</option>
<option value=2>Jim</option>
<option value=_3>Bob</option>
</select>
<input type=text name=age value=>
<input type=text name=gender value=>
</form>
</body>