简体   繁体   中英

submit a php form and a javascript function with 1 button

I'm new in the web developement currently I'm working on a quiz in php, which I wanted to add a timer in javascript to give points based on rapid response. but now when I send the remaining time of the timer, I can't submit the php form with the answers, I tried with "document. forms. quiz. value" but this way even if the answers are not selected, they take it always as correct. this is my currently script :

       <html>
       <head>



   <script type="text/javascript">
    function inviare() {
            var resto = secondi_totali;
            window.location = "risultato.php?res=" + secondi_totali;
            }
    </head>
    <body>
     <?php
     <form method='post' action='javascript:inviare()' name='quiz'id='1'>
      <ol>
                <li>
                    <h3>Cosa e java ?</h3>

                    <div>
                    <input type='radio' name='risposta3' value'c'/>
                    <label for='risposta3'>c) una cavolata </label>
                    </div>

                    <div>
                    <input type='radio' name='risposta1' id='risposta1' value='a'/>
                    <label for='risposta1'>a) un cafe  </label>
                    </div>

                    <div>
                    <input type='radio' name='risposta2' value='b'/>
                    <label for='risposta2'>b) un linguaggio </label>
                    </div>

                    <input type='submit' name='invia'/>  


                </li>

            </ol>
     ?>
    </body>
    </html>

i tried too with onSubmit but it doesn't work. can i do something to send the answers and the remaining time at the same time ?

Your code has many issues, but just answering why it is not submitting: using window.location will navigate your page, not submit the form. you have to use something like this

http://www.w3schools.com/jsref/met_form_submit.asp

with some fixes, your code will look like this and POST to your risultato.php where you can actually record the data using your PHP code. you don't need any PHP for this form

<html>
       <head>
   <script type="text/javascript">
   var startDate = new Date();
       function inviare() {
            var diff = (new Date().getTime() - startDate.getTime()) / 1000;
            document.getElementById("lapsedtime").value = diff;
            document.getElementById("1").submit();
       }
    </script>
    </head>
    <body>

     <form method='post' action='risultato.php' name='quiz' id='1'>
      <ol>
                <li>
                    <h3>Cosa e java ?</h3>

                    <div>
                    <input type='radio' name='risposta3' value='c'/>
                    <label for='risposta3'>c) una cavolata </label>
                    </div>

                    <div>
                    <input type='radio' name='risposta1' id='risposta1' value='a'/>
                    <label for='risposta1'>a) un cafe  </label>
                    </div>

                    <div>
                    <input type='radio' name='risposta2' value='b'/>
                    <label for='risposta2'>b) un linguaggio </label>
                    </div>
                    <input type="hidden" name="lapsedtime" id="lapsedtime" />
                    <input type='button' name='invia' value="submit" onclick="javascript:inviare()"/>  


                </li>

            </ol>
     </form>
    </body>
    </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM