www.webdeveloper.com
Results 1 to 3 of 3

Thread: form submit and redirect question

  1. #1
    Join Date
    Jul 2012
    Posts
    1

    Question form submit and redirect question

    i'm trying to put together a form that takes the answer to a simple multiplication question and then uses javascript to verify the answer. i want the page to redirect to a new one if the answer is correct, but i also want the form to submit when the viewer hits enter.

    when i write the form using input type="submit" the view can hit enter and get a response, but the page won't redirect because the answer is added to the url

    when i write the form using input type="button" the page will redirect, but the viewer has to click on the button to get a response.

    can i have the best of both worlds? here is my code using "button" below.

    thanks!

    <html>

    <head>
    <title></title>
    <SCRIPT LANGUAGE="JavaScript">

    function multv() {
    if (document.mult.answer.value == 21) {
    alert("You are correct!")
    window.location.href = "http://www.yahoo.com";
    }

    else if (document.mult.answer.value > 21) {
    alert("Too high!");
    }
    else if (document.mult.answer.value < 21) {
    alert("Too low!");
    }
    else {
    alert("Try again!");
    }
    };



    </SCRIPT>
    </head>

    <body>
    <center>
    <h1>7 x 3</h1>
    <P>
    <form name="mult">


    <input type="number" name="answer" style="font-size:40px; width:75px; height:50px" tabindex="0" height="100" width="30"/>
    <br>
    <input type="button" value="answer" onClick="multv()" />
    </form>
    </center>

    </body>
    </html>

  2. #2
    Join Date
    Jun 2012
    Location
    Dark Side of the Moon
    Posts
    70
    My guess would be that if the answer is correct you want to move on to a new question. So why would not put in a button that verifies the question, as in "Check Answer", and then a "Next Question" button for when the user is ready to move to the next. Each would call a separate function, so in the end you get to stay on the page for verification, and next for when the user is ready to move on.

    Also if you don't put an input element into a form, there is not form to submit, so no processing. Then the onclick becomes you way to handle flow, and nothing gets added to the end of the url.

  3. #3
    Join Date
    Mar 2012
    Posts
    1,643
    If you want to use a form it would make more sense (to me) to use PHP rather than Javascript.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles