繁体   English   中英

如何插入单选按钮的检查值,该值是来自PHP中另一个表的数组

[英]how to insert the checked value of radio button which is array from another table in php

我创建了一个测试表单,试图将来自性别表(包含:(1)男性(2)女性)的单选按钮值发送到另一个名为ajebaje MySQL表中。 我目前遇到问题。 下面的代码只是一个测试,我希望单选按钮提交该值,但不是。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title> ajebaje </title>
    </head>
    <body>
        <form name="form1" action= "<?php echo $_SERVER["PHP_SELF"]; ?>"  method="post"> 
             <table width="20%" border="0" cellspacing="1" cellpadding="3">
                <tr>
                    <td><h4> Student's Gender </h4></td>
                    <td>
                        <?php
                            $con=mysqli_connect("localhost", "root", "ew6wLoLOro", "result");
                            $sql=mysqli_query($con, "select gender_sl_no, gender_name from gender")                
                            while($row=mysqli_fetch_array($sql))
                            {
                                 echo '<table>
                                           <input type="radio" name="sexbd" checked="checked"/>'.$row['gender_name'].'           
                                       </table>';
                            }
                        ?>
                   </td>
               </tr>  
               <tr>
                   <td> </td>
                   <td><input type ="submit" name="submit"/> </td>
               </tr>
           </table>
       </form>
       <?php
           $con=mysqli_connect("localhost", "root", "ew6wLoLOro", "result");
           $sexbd = $_POST['sexbd'];
           if(isset($_POST['submit']))
           {    
               echo $que="Insert into ajebaje VALUES(default,'$sexbd' )";
               echo " ";
               echo "Your Data Inserted";
               $result = mysqli_query($con,$que);
           }
       ?>
   </body>
</html>

默认情况下,它应该在表单中发送sexbd: on

为了从表单发送值,需要在输入标签中定义value属性。 像这样:

echo '<table>
          <input type="radio" name="sexbd" checked="checked" value='.$row['gender_name'].'/>'.$row['gender_name'].'
      </table>';

呈现的表单将如下所示:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title> ajebaje </title>
    </head>
    <body>
        <form name="form1" action= "/test"  method="post"> 
             <table width="20%" border="0" cellspacing="1" cellpadding="3">
                <tr>
                    <td><h4> Student's Gender </h4></td>
                    <td>
                        <table>
                            <input type="radio" name="sexbd" checked="checked" value="Male"/> Male          
                        </table>
                        <table>
                            <input type="radio" name="sexbd" checked="checked" value="Female"/> Female          
                       </table>
                   </td>
               </tr>  
               <tr>
                   <td> </td>
                   <td><input type ="submit" name="submit"/> </td>
               </tr>
           </table>
       </form>
   </body>
</html>

现在,提交的值将为sexbd: Malesexbd: Female

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM