I made a simple application that have a combobox which is loaded with database value and a textfield which should display text related to combo box selection. Name Number number1 1234 number2 2345 number2 5678 number3 2212
So combo box will have values number1, number2, number3. when user selects number1, i have to load textfield value with 1234 and if number2 is selected 2345,5678.
Below is my code which retrieves from database ... but problem is the value is loaded only when i press enter key instead of clicking submit button...
<head>
<title>Sample Numbers</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body >
<?php
$dbname = 'sample_db';
$db_user = 'xxx';
$db_pass = 'xxx';
$host = 'localhost';
$conn = mysql_connect($host, $db_user, $db_pass);
mysql_select_db($dbname);
$query = "select distinct Name from numbers";
$result = mysql_query($query, $conn) or die(mysql_error());
?>
<center>
<form name=callsubm>
<table>
<?php
if ($result) {
?>
<tr>
<td>Group Name:</td>
<td><select name="Name" id="Name" onchange="onComboChange();">
<?php
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['Name'] . '">' . $row['Name'] . '</option>';
}
}
?>
</select>
<?php
if (isset($_GET['Name'])) {
$array = array();
$query = "select Number from numbers where Name='" . $_GET['Name'] . "'";
$result = mysql_query($query, $conn) or die(mysql_error());
$i = 0;
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row['Number'];
开发者_开发技巧 $i++;
}
}
$total_numbers = implode(',', $array);
}
?>
<script type="text/javascript">
function showValues() {
var a=new Array();
<?php
for ($i = 0; $i < count($array); $i++) {
echo "a[$i]='" . $array[$i] . "';\n";
}
?>
alert(a.join());
document.getElementById("inTxt").value=a.join();
}
</script>
</td>
</tr>
<tr>
<td>Numbers :</td>
<td ><input name=inTxt id=inTxt type="text" size="15"></td>
<td><input type="button" id="callBtn" name="callBtn" value="submit" onclick="showValues()" ></td>
</tr>
</table>
</form>
</center>
<script type="text/javascript">
function onComboChange() {
groupName=document.getElementById("Name").value;
alert("Selected Group:" + groupName);
}
</script>
</body>
How to solve this?
Thanks in advance
If you need a submit button, but it doesn't have to be a button
, use an <input type="submit">
.
Thus, change your html to:
<input type="submit" id="callBtn" name="callBtn" value="submit" onclick="showValues()" >
(You can omit the value="submit"
if you don't query that value in php...)
When to use a button:
If you want the text being displayed on the submit button to be different to the value of the submit, you use a button:
<button type="submit" id="callBtn" name="callBtn" onclick="showValues()"
value="valueToBeSubmitted">textToBeDisplayed</button>
Change the type to submit on your submit button, rather than using button as the type:
<input type="submit" id="callBtn" name="callBtn" value="submit" onclick="showValues()" >
精彩评论