动态创建单选按钮并分配选定的值 PHP
Posted
技术标签:
【中文标题】动态创建单选按钮并分配选定的值 PHP【英文标题】:Dynamically creating Radio Buttons and Assigning selected Values PHP 【发布时间】:2016-05-05 08:46:37 【问题描述】:您好,我对 php 很陌生。我正在尝试从 PHP 中的单选按钮获取选定的值,但我无法获取选定的值。我通过连接到我的数据库(mysql)填充了这些值,但我无法从单选按钮获取分配的值。它总是逃避 if 条件并说“未选择值”,我无法分配值并将其保存到我的数据库中
感谢您的帮助。
我的index.php如下`
<?php
session_start();
$_SESSION['timein']= time();
?>
<?php
include("config.php");
$conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
$query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
$select_salutation_type=mysqli_query($conn, $query_salutation_type);
while($row1 = mysqli_fetch_array($select_salutation_type))
echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];
?>
<html>
<body>
<form method="post" action="capture_data.php" >
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
`
我的 capture_data.php 如下
<?php
if(isset($_POST['submit']))
if(isset($_POST['salutation_description']))
$selected_val = $_POST['salutation_description'];
echo "You have selected :" .$selected_val;
else
echo 'No Value Selected';
?>
【问题讨论】:
单选按钮不在表单内。 【参考方案1】:表单仅发送介于<form>
和</form>
之间的输入。您正在呼应<form>
之前的单选按钮。
<?php
session_start();
$_SESSION['timein']= time();
?>
<html>
<body>
<form method="post" action="capture_data.php" >
<?php
include("config.php");
$conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
$query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
$select_salutation_type=mysqli_query($conn, $query_salutation_type);
while($row1 = mysqli_fetch_array($select_salutation_type))
echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];
?>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
【讨论】:
【参考方案2】:您需要使用<form>
标签内的单选按钮:
修改后的代码:
<?php
session_start();
$_SESSION['timein']= time();
?>
<html>
<body>
<form method="post" action="capture_data.php" >
<?php
include("config.php");
$conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
$query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
$select_salutation_type=mysqli_query($conn, $query_salutation_type);
while($row1 = mysqli_fetch_array($select_salutation_type))
echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];
?>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
【讨论】:
【参考方案3】:<?php
session_start();
$_SESSION['timein']= time();
?>
<?php
include("config.php");
$conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
$query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
$select_salutation_type=mysqli_query($conn, $query_salutation_type);
$radioHtml = "";
while($row1 = mysqli_fetch_array($select_salutation_type))
$radioHtml.= '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];
?>
<html>
<body>
<form method="post" action="capture_data.php" >
<?php echo $radioHtml; ?>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
在表单中保留单选按钮
【讨论】:
【参考方案4】:Hi Lad 尝试将 while 循环重新定位在 HTML 的 form 标记内。就像下面的例子。 :D
<?php
session_start();
$_SESSION['timein']= time();
?>
<?php
include("config.php");
$conn = mysqli_connect($dbHost, $dbuser, $dbpassword, $dbDatabase);
$query_salutation_type='SELECT salutation_description FROM tbl_salutation;';
$select_salutation_type=mysqli_query($conn, $query_salutation_type);
?>
<html>
<body>
<form method="post" action="capture_data.php" >
<?php
while($row1 = mysqli_fetch_array($select_salutation_type))
echo '<input type="radio" name="salutation_description" value="'.$row1[0].'"/>'.$row1[0];
?>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
【讨论】:
以上是关于动态创建单选按钮并分配选定的值 PHP的主要内容,如果未能解决你的问题,请参考以下文章