/    Sign up×
Community /Pin to ProfileBookmark

mysql dropdown fill input fields in php with javascript

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.

[CODE]
<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>
[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@ud2008authorApr 08.2009 — I've solved the problem, but running into a small other problem.

This is the code I use now:
[CODE]
<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();">";
[B]echo "<option value='select name'>Select name</option>[/B]

// 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>";
?>
[/CODE]


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.
×

Success!

Help @ud2008 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...