javascript - Submitting form that has tinymce textarea, radio button and checkbox to database
Get the solution ↓↓↓My form has several textarea fields, radio button, checkbox and a select input. If I do not use TinyMCE on the textarea, I can easily submit the form using php. When I decided to use TinyMCE on one textareas, I brought in java script to process TinyMCE and to stop the page refreshing at submitting. Now when I submit, it seems only the last php line of code will be processed saying 'Please select at least one correct answer!'. Below are my codes.
<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 Script
$("#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 is the php page. Java script is on the same page with the form. Please what do i do to these codes to submit my form?
Answer
Solution:
To submit all the information, you must add a new input in your HTML code, it should be the last input inside your form. You should add an attribute to this input with the type submit. It would be like this,
<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>
I hope you found your answer.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function mysqli_connect()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.