Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    New Jersey USA
    Posts
    81
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Question How can I add php to my JavaScript so the submit button uses both php and java?

    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>

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    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/java...m-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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •