php - 在 PHP 中选中单选按钮时如何插入输入文本值的值?

标签 php html mysql

我创建了一个简短的问题表单,我想在选中单选按钮时插入输入文本的值。目前我只能插入单选按钮的值,不能插入输入文本的值。 我该怎么做?

HTML代码:-

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="Assets/index_style.css" />
    <link rel="stylesheet"  href="bootstrap/css/bootstrap.css" />
    <link href="https://fonts.googleapis.com/css?family=Comfortaa" rel="stylesheet">
    <script src="Assets/jquery-1.11.3-jquery.min.js"></script>

    <script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <script>
        //back button function
        function goBack() {
            window.history.back();
        }
    </script>


</head>
<body>

<div class="container">
    <div class="header clearfix">
        <nav>
            <ul class="nav nav-pills pull-right">
                <li role="presentation" class="active"><a href="settings.php">Settings</a></li>
                <li role="presentation"><a href="logout.php">Logout</a></li>
            </ul>
        </nav>
    </div> <!-- /container -->

    <div class="container">

        <button type="button" class="btn btn-success" onclick="window.location.href='admin_home.php'">
            <span class="glyphicon glyphicon-arrow-left"></span> Back to home
        </button><br>

        <h3 class="text-muted">Short Question Form</h3>

        <form action="admin_shortquestform.php" method="post">

            <table class="table">

                <tbody>
                <tr>
                    <td>Question:</td>
                    <td><input type="text" size="70" name="questiontext"></td>
                </tr>
                <tr>
                    <td>1. </td>
                    <td><input type="text" size="60" name="ans1"><input type="radio" name="radioans" value="1" /></td>
                </tr>
                <tr>
                    <td>2. </td>
                    <td><input type="text" size="60" name="ans2"><input type="radio" name="radioans" value="2" /></td>
                </tr>
                <tr>
                    <td>3. </td>
                    <td><input type="text" size="60" name="ans3"><input type="radio" name="radioans" value="3" /></td>
                </tr>
                <tr>
                    <td>4. </td>
                    <td><input type="text" size="60" name="ans4"><input type="radio" name="radioans" value="4" /></td>
                </tr>
                <tr>
                    <td align="center"><input type="submit" name="submit" value="Create"></td>
                </tr>
                </tbody>

            </table></form>
    </div>

    <footer class="footer">
        <p>&copy; 2016 FYP, Inc.</p>
    </footer>

</body>
</html>

PHP 代码:-

<?php

session_start();

require_once 'dbConn.php';

if(!empty($_POST{'submit'})) {


    $questiontext = $_POST['questiontext'];
    $anstext1 = $_POST['ans1'];
    $anstext2 = $_POST['ans2'];
    $anstext3 = $_POST['ans3'];
    $anstext4 = $_POST['ans4'];


    if(empty($questiontext) && empty($anstext1) && empty($anstext2) && empty($anstext3) && empty($anstext4)) {
        echo "Please enter data";
        echo "<script>setTimeout(\"location.href = 'admin_mcqform.php';\",2000);</script>";
        return false;

    }
    if(!isset($_POST['radioans']))
    {
        echo "Please enter data";
        echo "<script>setTimeout(\"location.href = 'admin_mcqform.php';\",2000);</script>";
        return false;
    }

    $radiobtn = $_POST['radioans'];
    try {


        //add the first record into question table
        $stmt1 = $conn->prepare("INSERT INTO `question`(question_text, question_type) VALUES(:questiontext, 'shortquestion')");
        $stmt1->bindParam(":questiontext",$questiontext);
        $stmt1->execute();

        //add the last id insert for question into option tbl question_id
        $questionID = $conn->lastInsertId();
        $answerID = $conn->lastInsertId();

        //add second record into option_tbl
        $stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer,question_id) VALUES(:ans,$questionID)");

        $stmt2->bindParam(":ans",$anstext1);
        $stmt2->execute();
        $stmt2->bindParam(":ans",$anstext2);
        $stmt2->execute();
        $stmt2->bindParam(":ans",$anstext3);
        $stmt2->execute();
        $stmt2->bindParam(":ans",$anstext4);
        $stmt2->execute();

        //add third record into answer table
        $stmt3 = $conn->prepare("INSERT INTO `answer`(answer_text,question_id) VALUES(:radioans,$answerID)");
        $stmt3->bindParam(":radioans",$radiobtn);
        $stmt3->execute();

        header('Location: '.$_SERVER['PHP_SELF']);
        exit;

    }
    catch (PDOException $e) {
        echo $e->getMessage();
    }

}

?>

最佳答案

它可以通过使用一点 java 脚本来实现。或者你可以使用 J查询(代码行会少)。我在这里使用 java 脚本...

    <!-- The id of input type=text  and Value of radio must be the same for each items/rows-->

  <form id="myForm" action="admin_shortquestform.php" method="POST">

    1.<input type="text" size="20" name="ans2" id="1"><input type="radio" name="radioans" value="1"/><br>
    2.<input type="text" size="20" name="ans3" id="2"><input type="radio" name="radioans" value="2" /><br>
    3.<input type="text" size="20" name="ans4" id= "3" ><input type="radio" name="radioans" value="3" /><br>



      <!-- selected ans is set to the below hidden input field in js function, and you can get it in php by using $_POST['selected_ans'] -->

      <input type="hidden" name="selected_ans" id="hidden_input" />


      <input type="button" onclick="myFunction()"  value="Submit">

    </form>

    <script>
    function myFunction() {

       var radios = document.getElementsByName('radioans');

    for (var i = 0, length = radios.length; i < length; i++) {
        if (radios[i].checked) {


            var selected_radio_value=radios[i].value;

            var selected_text_input=document.getElementById(selected_radio_value);

            var selected_text_input_value=selected_text_input.value;//selected textbox value

            var input_hidden=document.getElementById("hidden_input");

            input_hidden.value=selected_text_input_value+"";// setting selected textbox value to hidden input field




             document.getElementById("myForm").submit();// submitting the form


            break;
        }


    }
    }
    </script>

并且在您的表单操作 php 文件 (admin_shortquestform.php) 中:-

  1. 使用 if($_SERVER['REQUEST_METHOD'] === 'POST') 而不是 if(!empty($_POST{'submit'}))

  2. 您可以通过编写以下代码来获取选择的ans

    $selected_answer=$_POST['selected_ans'];

关于php - 在 PHP 中选中单选按钮时如何插入输入文本值的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41164039/

相关文章:

php mysql 回显排序

PHP-MySQL 查询优先显示一张表的行

html - 两个 div,一个在另一个上面,都具有 100% 的屏幕高度

html - 链接不适用于 div 和图像

php - PHP中两个日期之间所有月份和年份的列表

php - Laravel 4 中的公共(public)可用函数

javascript - 连续更改元素中的文本

c# - 循环 SQL 查询的正确方法?

android - 如何为 Android 创建安全的在线数据库

php - MYSQL 查询计算每小时耗时