Forum Moderators: open
<script type="text/javascript">
function loadUser(name,email) {
document.getElementById("name").value=name;
document.getElementById("email").value=email;
}
</script>
<form action="action.php" method="POST">
<select name="person">
<?php
$result=mysql_query("SELECT * FROM persons");
while($row=mysql_fetch_array($result)) {
$personName=$row['name'];
$personEmail=$row['email'];
echo "<option value='$personName' onClick=\"loadUser('$personName','$personEmail');\">$personName</option>";
}
?>
</select>
<input type="text" id="name" name="name" placeholder="name" />
<input type="email" id="email" name="email" placeholder="email" />
</form>
<script type="text/javascript">
window.onload=function() { attachBehaviors(); };
//
function attachBehaviors() {
document.getElementById('person').onchange=function() {
loadUser(this.options[this.selectedIndex].value); // <-- check this, may be incorrect
}
}
function loadUser(optionvalue) {
// Always set a default
if (optionvalue=='') {
// You could alert here but it may just be annoying
//alert('Please select a value');
return; // Do nothing if blank
}
opts = optionvalue.split(':');
var name = opts[0];
var email = opts[1];
document.getElementById('name').value=name;
document.getElementById('email').value=email;
}
</script>
<form action="action.php" method="POST">
<select name="person" id="person">
<option value="">Select</option>
<?php
$result=mysql_query("SELECT * FROM persons");
while($row=mysql_fetch_array($result)) {
$personName=$row['name'];
$personEmail=$row['email'];
echo "<option value=\"$personName:$personEmail\">$personName</option>\n";
}
?>
</select>
<input type="text" id="name" name="name" placeholder="name" />
<input type="email" id="email" name="email" placeholder="email" />
</form>