动态创建单选按钮并分配选定的值PHP

时间:2016-01-28 05:42:11

标签: php radio-button

您好我是PHP新手。我试图从PHP中的单选按钮获取所选值但我无法获得所选值。我通过连接到我的数据库(MySQL)来填充值,但我无法从单选按钮获取指定的值。它总是逃脱if条件并说'#34; No Value Selected"我无法分配值并将其保存到我的数据库

感谢您的帮助。

我的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';
}
}
?>

index.php

4 个答案:

答案 0 :(得分:2)

表单仅发送<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>

答案 1 :(得分: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>

答案 2 :(得分:2)

<?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>

在表单中保留单选按钮

答案 3 :(得分:0)

Hi Lad尝试在HTML的表单标记内重新定位while循环。像下面的例子。 :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>