...

View Full Version : mysql dropdown fill input fields in php with javascript



UD2006
04-07-2009, 12:27 PM
Hope I posted this in the right forum.

Here's my problem.

I have a mysql database (name) which helds 3 fields, id, naam, email. In php I've made the connection and create a selection menu and with javascript I was able to display, (when I selected for example "Bas" in the selection menu), bas his name into a first input text field. Now a couple of things I still want but can't seem to get it right.

First: I also want to display "bas" his email address in a second input text field. (the database contains more names, but this an example).

Second: The selection menu starts with the first name in the database, but can I make it for example "Select name" as beginning.

Here is the code I have for now, please help.


<script>
function setVal()
{
document.getElementById('naam').value = cArray[document.getElementById('name').selectedIndex];
}
var cArray = new Array();
</script>
<!-- Contact info -->
<table border="0" width="100%">
<tr>
<td style="text-align:right" width="150"><?php echo $hesklang['select_name']; ?>: <font class="important">*</font></td>
<td width="80%">
<?php
require(HESK_PATH . 'inc/database.inc.php');
hesk_dbConnect();
$sql = 'SELECT naam,email FROM `'.$hesk_settings['db_pfix'].'names`';
$result = hesk_dbQuery($sql) or die ('Error in query: $query. ' . mysql_error());

echo "<select id=\"name\" name=\"name\" onChange=\"javascript:setVal();\">";

// keeps getting the next row until there are no more to get
$Cnt=0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a dropdown
echo "<option value='$row[naam]'>$row[naam]</option>";
//Adding to Array
echo "<script>cArray[$Cnt]='$row[naam]';</script>";
$Cnt++;
}
echo "</select>";
?>
</td>
</tr>
<tr>
<br>
<td style="text-align:right" width="150"></td>
<td width="80%"><input type="text" name="naam" id="naam" size="40" maxlength="30" value="<?php echo stripslashes(hesk_input($_SESSION['c_name']));?>" /></td>
</tr>
<tr>
<td style="text-align:right" width="150"></td>
<td width="80%"><input type="text" name="email" id="email" size="40" maxlength="50" value="<?php echo stripslashes(hesk_input($_SESSION['c_email']));?>" /></td>
</tr>
</table>

UD2006
04-08-2009, 09:41 AM
I've solved the problem, but running into a small other problem.

This is the code I use now:


<script>
function setVal()
{
document.getElementById('naam').value = cArray[document.getElementById('name').selectedIndex];
document.getElementById('email').value = dArray[document.getElementById('name').selectedIndex];
}
var cArray = new Array();
var dArray = new Array();
</script>
<!-- Contact info -->
<table border="0" width="100%">
<tr>
<td style="text-align:right" width="150"><?php echo $hesklang['select_name']; ?>: <font class="important">*</font></td>
<td width="80%">
<?php
require(HESK_PATH . 'inc/database.inc.php');
hesk_dbConnect();
$sql = 'SELECT naam,email FROM `'.$hesk_settings['db_pfix'].'names`';
$result = hesk_dbQuery($sql) or die ('Error in query: $query. ' . mysql_error());

echo "<select id=\"name\" name=\"name\" onChange=\"javascript:setVal();\">";
echo "<option value='select name'>Select name</option>

// keeps getting the next row until there are no more to get
$Cnt=0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a dropdown
echo "<option value='$row[naam]'>$row[naam]</option>";
//Adding to Array
echo "<script>cArray[$Cnt]='$row[naam]';</script>";
echo "<script>dArray[$Cnt]='$row[email]';</script>";
$Cnt++;
}
echo "</select>";
?>


I want in the select option a standard word, for example: select name.
But when I add a line of option just after the first select (bold text), that line takes the first record of the database, but it should be a blank line (nothing linked to it, just a reminder to select a name).

Can some one please help me with this, need some help soon.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum