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

时间:2016-12-15 12:19:57

标签: 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();
    }

}

?>

1 个答案:

答案 0 :(得分:0)

可以通过使用一些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 [&#39; REQUEST_METHOD&#39;] ===&#39; POST&#39;)而不是if(!empty($ _ POST {&#39; submit&#39;}))

  2. 您可以通过编写以下代码

    来获取所选的ans

    $ selected_answer = $ _ POST [&#39; selected_ans&#39;];