php、html 组合框和文本字段
我制作了一个简单的应用程序,其中有一个加载了数据库值的组合框和一个应显示与组合框选择相关的文本的文本字段。 姓名 号码 号码1 1234 号码2 2345 号码2 5678 number3 2212
所以组合框将有值 number1、number2、number3。当用户选择 number1 时,我必须加载文本字段值 1234,如果选择 number2 2345,5678。
下面是我从数据库检索的代码...但问题是仅当我按 Enter 键而不是单击提交按钮时才加载该值...
<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>
如何解决这个问题?
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要提交按钮,但不一定是
按钮
,请使用。
因此,将您的 html 更改为:(
如果您不在 php 中查询该值,则可以省略
value="submit"
...)何时使用按钮:
如果您希望提交按钮上显示的文本与提交的值不同,请使用按钮:
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:
(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:
更改要在提交按钮上提交的类型,而不是使用按钮作为类型:
Change the type to submit on your submit button, rather than using button as the type: