php-检查单选按钮然后将值传递给mysql查询

时间:2014-04-23 16:27:11

标签: php html

每一天都很美好,我正在尝试检查是否点击了一个单选按钮,并且想要点击单选按钮的值来传递一个变量,我将使用该变量来比较数据库中的记录与相同的值它。并显示列表框上的所有记录? 但是当我尝试运行此代码时,就会发生这种情况

<td><input type="radio" name="1stChoice" value="TESDA" ></td><br>

<td align = "center">

<select name="course1">

<?php
include('dbconnection.php');
if(isset($_POST['1stChoice'])) {
if($_POST['1stChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['1stChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);

while($row=mysql_fetch_array($mysql_select))
{

?>
<option><?php $row['Program']; ?></option></td>
</select>
<?php } ?>
</tr>
<td width="20%">2nd choice:</td>
<td><input type="radio" name="2ndChoice" value="CHED" ></td>
<td><input type="radio" name="2ndChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course2">
<?php
include('dbconnection.php');
if(isset($_POST['2ndChoice'])) {
if($_POST['2ndChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['2ndChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);

while($row=mysql_fetch_array($mysql_select))
{

?>
<option><?php $row['Program']; ?></option></td>
</select>
<?php } ?>

2 个答案:

答案 0 :(得分:1)

我测试了您的代码,据我所见,您没有回复<?php $row['Program']; ?>应该读作<?php echo $row['Program']; ?>

这适用于您的<option>代码。

我也没有注意到任何表单标记<form></form>,因此如果您目前没有使用它们,则需要添加它们。

使用提交按钮也很有用。虽然我不确定你是否在代码中使用JS / jQuery。

<input type="submit" name="submit" value="Submit">


以下是我用来测试它的内容,以及一些补充/修改:

(我添加了表单标签,提交按钮和<select>标签的回显)

<form action="" method="post">
<td><input type="radio" name="1stChoice" value="TESDA" ></td><br>

<input type="submit" name="submit" value="Submit">

<td align = "center">

<select name="course1">

<?php
include('dbconnection.php');
if(isset($_POST['1stChoice'])) {
if($_POST['1stChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['1stChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);

while($row=mysql_fetch_array($mysql_select))
{

?>
<option><?php echo $row['Program']; ?></option></td>
</select>
<?php } ?>
</tr>
<td width="20%">2nd choice:</td>
<td><input type="radio" name="2ndChoice" value="CHED" ></td>
<td><input type="radio" name="2ndChoice" value="TESDA" ></td><br>
<td align = "center">
<select name="course2">
<?php
include('dbconnection.php');
if(isset($_POST['2ndChoice'])) {
if($_POST['2ndChoice'] == 'TESDA') {
$choose='TESDA';
} elseif($_POST['2ndChoice'] == 'CHED') {
$choose='CHED';
}
}
$mysql_select=mysql_query("select * from courses where Institution = '$choose' ",$mysql);

while($row=mysql_fetch_array($mysql_select))
{

?>
<option><?php echo $row['Program']; ?></option></td>
</select>
</form>
<?php } ?>

答案 1 :(得分:0)

这不是php工作。如果您的HTML好,您的浏览器将检查选中的单选按钮。看到!在您的代码中,名称必须以字母或_(下划线)开头。 因此,请将所有name='2ndChoice'替换为

   <td><input type="radio" name="ck_2ndChoice" value="CHED" ></td>
   <td><input type="radio" name="ck_2ndChoice" value="TESDA" ></td><br>

以及所有name='1stChoice' name='ck_1stChoice'