提交具有tinymce textarea,单选按钮和复选框到数据库的表单

时间:2017-08-23 10:52:21

标签: javascript php jquery forms

我的表单有几个textarea字段,单选按钮,复选框和一个选择输入。如果我不在textarea上使用TinyMCE,我可以使用php轻松提交表单。当我决定在一个textareas上使用TinyMCE时,我引入了java脚本来处理TinyMCE并在提交时停止刷新页面。现在,当我提交时,似乎只会处理最后一行php代码,说“请选择至少一个正确的答案!”#39;。   以下是我的代码。

<form id="myform2" method="post" action="insert_question_refresh.php">
    <p style="color:#333">Question</p>
    <textarea class="tinymce" name="texteditor" id="texteditor"></textarea>

    <p style="color:#333" style="margin-top:30px !important">Answers</p>

    A<input type="checkbox"name="get_value[]" value="A"><span class="ans_option"> This answer option is correct</span>
    <textarea name="aaa" class="form-control" style="width:60% !important; height:200px !important"></textarea>

    B <input type="checkbox" name="get_value[]" value="B"><span class="ans_option"> This answer option is correct</span>
    <textarea name="bbb" class="form-control" style="width:60% !important; height:200px !important"></textarea>

    C <input type="checkbox"name="get_value[]" value="C"><span class="ans_option"> This answer option is correct</span>
    <textarea name="ccc" class="form-control" style="width:60% !important; height:200px !important"></textarea>

    D<input type="checkbox" name="get_value[]" value="D"><span class="ans_option"> This answer option is correct</span>
    <textarea name="ddd" class="form-control" style="width:60% !important; height:200px !important"></textarea>

    <label for="student_id" class="control-label" style="color:#333" >Course Title</label>
    <select name="category" class="form-control" required>
        <option>one</option>
        <option>two</option>
        <option>three</option>
    </select>

    <p style="color:#333" style="margin-top:23px !important">Randomize answer?</p>
    <input name="random" type="radio" value="no" /> <span style="color:#333; font-size:14px">No</span><br>
    <input name="random" type="radio" value="yes" /> <span style="color:#333; font-size:14px">Yes</span>

    <button id="sub" style="color:#fff; padding:5px 66px" class="btn btn-success" ><b>Save</b></button>

</form>

PHP

<?php
    include("includes/db.php");
    if(!empty($_POST["get_value"])){
    foreach($_POST["get_value"] as $checkbox){
    }
    $question = $_POST['texteditor'];
    $a = $_POST['aaa'];
    $b = $_POST['bbb'];
    $c = $_POST['ccc'];
    $d = $_POST['ddd'];
    $random = $_POST['random'];
    $category = $_POST['category'];

    $insert_question = "insert into questions (question,checkbox,ansa,ansb,ansc,ansd,random,category) values ('$question','$checkbox','$a','$b','$c','$d','$random','$category')";

    $run_question = mysqli_query($con, $insert_question);
    if($insert_question){
    echo "Your message was successfully delivered";
    }
    else {
     echo "failed";
    }
    }

    else{
    echo "<script>alert('Please select at least one correct answer!')</script>";
    }

?>

Java脚本

 $("#sub").click( function() {

 $.post( $("#myform2").attr("action"), $("#myform2 :input").serializeArray(), function(info){ $("#result").html(info); } );
clearInput();
 });

 $("#myform2").submit( function() {
    return false;
 });

 function clearInput() {
     $("#myform2 :input").each( function() {
         $(this).val('');
     });
 }

insert_question_refresh.php是php页面。 Java脚本与表单位于同一页面上。请如何处理这些代码以提交表格?

1 个答案:

答案 0 :(得分:0)

要提交所有信息,您必须在HTML代码中添加新输入,它应该是表单中的最后一个输入。您应该使用提交类型向此输入添加属性。就像这样,

<p style="color:#333" style="margin-top:23px !important">Randomize answer?</p>
    <input name="random" type="radio" value="no" /> <span style="color:#333; font-size:14px">No</span><br>
    <input name="random" type="radio" value="yes" /> <span style="color:#333; font-size:14px">Yes</span>

    <button id="sub" style="color:#fff; padding:5px 66px" class="btn btn-success" ><b>Save</b></button>

<input type="submit" value="Submit"/>

</form>

我希望你找到答案。