...

View Full Version : How can I add php to my JavaScript so the submit button uses both php and java?



connollyc4
03-01-2010, 10:15 PM
Right now the code has a submit button that has a timer of 1 minute to submit using JavaScript.. Once you hit submit a JavaScript pop up message either tells you that your answer is correct or incorrect.. What I need help with is adding php to that same submit button so the data can eventually get stored into a database.. I am able to do the database part just not the php part.. I would be able to do it if it was all php but using both JavaScript and php is hard for me to figure out. Please help if you have an idea here is the code..

I need help making this timed quiz so when you hit submit it goes into a mysql database. right now it just tells you if you answered correctly or not

Here is the code:

what color is the grass?

<input type = "text" id = "answer">
<input type = "button" id = "time" onclick="answer()">

<script type = "text/javascript">
var n = 60; // modify this for number of seconds to answer
document.getElementById( 'time').value = "Answer in " + n + " seconds";
var i = setInterval('count()' , 1000);
var tooLate;

function count() {
tooLate = 0;
n--;
if(n >=0) {
document.getElementById( 'time').value = "Answer in " + n + " seconds";
}
else {
clearInterval(i);
alert("Too late!");
document.getElementById('answer').value = "";
tooLate = 1;
}
}

function answer() {

var correct = "green"; // This is the correct answer
if (tooLate == 0) {
if(document.getElementById( 'answer').value == correct) {
clearInterval(i);
alert("Right Answer with " + n + " seconds remaining");
}
else{
clearInterval(i);
alert("Incorrect! The answer was " + correct);
}
}
}
</script>

skywalker2208
03-01-2010, 10:36 PM
Have javascript submit the form after the alert statement letting the user know if the answer is right or wrong. Take a look at

http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml

The other thing you could do is use ajax to submit the data to a php script and have the script enter the info into the database.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum