I want to INSERT VALUES after user has clicked OK on the confirmation box. Now the confirmation box works well. The only problem I can see is that when the user clicks ‘OK’ I want the INSERT VALUES to happen on a seperate page (insertQuestion.php) but I do not want the form to be navigated to that page. I want the form to navigate the way it is doing which is either submit to its own page or submit to create_session2.php depending on the situation.
So how can I INSERT VALUES into the database without navigating the user to that page (insertquestion.php) after the user has clicked ‘OK’ in the confirmation box?
I have tried using Jquery to perform an AJAX request but this has failed (Code of Jquery is at the bottom)
below is the javascript code where the confirmation box appears and if confirmation is ‘OK’, it submits the form:
function showConfirm(){
var confirmMsg=confirm("Do you want to Proceed" + "\n" );
if (confirmMsg==true)
{
submitform();
}
}
function submitform()
{
var QandAO = document.getElementById("QandA");
QandAO.submit();
}
Below is the INSERT VALUES code (already connected to DB and is on insertQuestion.php page wile the values posted are from the previous page):
$insertquestion = array();
{
$insertquestion[] = "' ". mysql_real_escape_string( $_POST['id'] ) . "', ' ". mysql_real_escape_string( $_POST['qid'] ) . "'";
}
$questionsql = "INSERT INTO Question (SessionId, QuestionId)
VALUES (" . implode('), (', $insertquestion) . ")";
mysql_query($questionsql);
Now Below is the php and form tag where the values which are going to be inserted come from:
function insertQuestion(form) {
var $tbody = $('#qandatbl > tbody');
var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
var $qid = $("<td name='qid' class='qid'>" + qnum + "</td>");
//I am trying to insert the $qid above in the INSERT VALUES
}
</script>
<form id="QandA" action="<?php echo htmlentities($action); ?>" method="post" >
<h1>CREATING QUESTIONS AND ANSWERS: SESSION (<?php echo $_SESSION['id'] ?>)
//I am trying to insert the $_SESSION['id'] above in the INSERT VALUES
<table id="qandatbl" align="center">
<thead>
<tr>
<th class="qid">Question No</th>
</tr>
</thead>
<tbody>//the $qid would go here</tbody>
</table>
<p><input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" /></p>
</form>
In your insertQuestion.php, After executing your mySQL query, Return a response back to the client. Some thing like this
So in your post call, the data variable will get that value once the jQuery post call make a call to the inert.php file
Assuming your submit button id is
submitBtnIf you are calling the above javascript in a submit button click, you should do a
return falseat the end, otherwise it will go for the normal form submitting and you will not be able to notice that it did an ajax post.EDIT : As per the question update
Remove the onclick event of the submit button as you are already binding it in your javascipt
in your javascript (in button click), you are calling the val() function on the form. You will not get the form content by that. What are you trying to send to the insertQuestion.php page, Is that a textbox value ? then read that specifically like this
Assuming you have a text box with an id “txtAnswer” like this in your page
If you want to send the entire form , you may use serialize function
http://api.jquery.com/serialize/
Use firebug to see what data you are sending in the $.post call(net tab) and if there is any script errors in the page (console tab)