I’m having trouble with this… Hoping to auto fill the text input boxes in my form upon selection of the username from the dropdown. Users array comes from a mysql db. Not sure I have the javascript written correctly. First is my array which will just get called from db as $users. Thanks for looking.
<?php
$users=array
(
"User" => array
(
"0" => array
(
"id" => "6",
"username" => "bsmith",
"full_name" => "Buddy Smith"
),
"1" => array
(
"id" => "2",
"username" => "lsmith",
"full_name" => "Libbie Smith"
),
"2" => array
(
"id" => "4",
"username" => "asmith",
"full_name" => "Andy Smith"
)
)
)
?>
then the javascript:
<script type="text/javascript">
var ids = new Array();
var use = new Array();
var ful = new Array();
<?php
foreach($users as $key=>$value) {
echo "ids[" . $key . "] = '" . $value['User']['id'] . "';\n";
echo "use[" . $key . "] = '" . $value['User']['username'] . "';\n";
echo "ful[" . $key . "] = '" . $value['User']['full_name'] . "';\n";
}
?>
function Choice() {
x = document.getElementById("users");
x.value = y.options[y.selectedIndex].text;
document.getElementById("ids") = ids[y.selectedIndex];
document.getElementById("use") = use[y.selectedIndex];
document.getElementById("ful") = ful[y.selectedIndex];
}
}
</script>
then my html:
<form name="form1" method="post" action="">
<select name="users" onChange='Choice();'><option> </option>
<?php
foreach ($users as $key=>$value) {
echo '<option value="'.$key.'">'.$value['User']['username'].'</option>';
}
?>
</select>
<p><input type="text" id="ids" name="id" ></p>
<p><input type="text" id="ful" name="full_name" ></p>
</form>
I took some liberties, and don’t really understand PHP so I made up those parts. Anyway, here’s what I got:
http://jsfiddle.net/zunrk/