douli1872 2018-09-05 14:22
浏览 84
已采纳

我该如何解析数据库中的单选按钮选择

Hello I am newbie and I would like to ask how can I parse the value that the user makes from the radio button to database. I tried to do it but when I go to database I don't see the value, only the text on. What am I doing wrong? can you please help me? Any suggestions are welcome. I have this code:

<?php
/*
Template Name: Form
*/
?>
<?php global $pc_theme_object; /* Reference theme framework class */ ?>
<?php get_header(); ?>

<form action="" id="postjob" method="post">
    <fieldset>
        <legend>blalala;</legend>
<input type="hidden" name="question1" value="blalala;"/>
        <div>
            <input type="radio" id="Α. Ίδια" name="answer1" checked />
            <label for="Α. Ίδια">Α. Ίδια</label>
        </div>

        <div>
            <input type="radio" id="Β. Διαφορετικές" name="answer1" />
            <label for="Β. Διαφορετικές">Β. Διαφορετικές</label>
        </div>

        <div>
            <input type="radio" id="Γ. Δεν γνωρίζω" name="answer1" />
            <label for="Γ. Δεν γνωρίζω">Γ. Δεν γνωρίζω</label>
        </div>


    </fieldset>
    
    
    
     <fieldset>
        <legend>rororor</legend>

<input type="hidden" name="question2" value="rororor"/>

        <div>
            <input type="radio" id="Α. Ναι" name="answer2" checked />
            <label for="Α. Ναι">Α. Ναι</label>
        </div>

        <div>
            <input type="radio" id="Β. Όχι" name="answer2" />
            <label for="Β. Όχι">Β. Όχι</label>
        </div>

         <div>
            <input type="radio" id="Γ. Δεν γνωρίζω" name="answer1" />
            <label for="Γ. Δεν γνωρίζω">Γ. Δεν γνωρίζω</label>
        </div>


         
    </fieldset>
     <button type="submit" name="submit">Submit</button>
</form>

<?php
$servername ="localhost";
$username = "username";
$password = "password";


// Create connection
$conn = mysqli_connect($servername, $username, $password, $db_name);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


$question1 = $_POST['question1'];
$answer1 = $_POST['answer1'];
$question2= $_POST['question2'];
$answer2 = $_POST['answer2'];

 global $wpdb;

if(isset($_POST['submit'])){
        $wpdb->insert( 'survey_test', array( 'question1' =>
        $_POST['question1'], 'answer1' => $_POST['answer1'], 'question2' =>
        $_POST['question2'], 'answer2' => $_POST['answer2'] ),
        array( '%s', '%s', '%s', '%s' ) );
    }
echo "Connected successfully";
?>

<?php get_footer(); ?>

</div>
  • 写回答

2条回答 默认 最新

  • dpno17028 2018-09-05 14:31
    关注

    Try adding value attributes to all your checkboxes inputs

    Instead of this

    <input type="radio" id="Α. Ίδια" name="answer1" checked />
    

    Use this

    <input type="radio" id="Α. Ίδια" name="answer1" value="Α. Ίδια" checked />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?