Jump to content

Recommended Posts

4 minutes ago, piano0011 said:

will the above work

I have no idea. We only see small snippets of your code - you are the only one who can see the big picture.

 

5 minutes ago, piano0011 said:

it good practice to use a separate page for the javascript code?

My approch is the same as with php functions. If they are required in more than one script, put them in an included file. Same goes for js. If general purpose js functions for use in several places, use an external file. If only used in the current file then I just put them in <script type="text/javascript"> .. </script> tags in the head section of the page.

I can get my php form to process the timer result and insert the record into the database but when I add the header function, it keeps taking me to the header function instead of showing the timer. My guess is that because it is processing the php first, it is doing this. I just like to know if I can have a header function it this is the case?

 

<?php

include_once 'includes/dbh.php';
    $timer = 60;
    $time = $_POST['time'];   
    $user_uid = 'piano0011'; 

      
    
    $sql = "UPDATE primerlevel_tests
            SET time_achieved = ?
            WHERE user_uid = ?
           ";

    $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
          echo 'SQL error';
        } else {
          mysqli_stmt_bind_param($stmt, "ss", $time, $user_uid);
          mysqli_stmt_execute($stmt);
        }

        header("Location: update.php?countdown=success");
        exit();

        


?>
<style type="text/css">
    div.timer {
        text-align: center;
        margin: 15% auto;
    }
    #msg {
        color: red;
    }
</style>
<html>
<head>
<script type="text/javascript">
    var timer = <?php echo $timer ?>;    // 1 minute timer.... 
    var min = 0;
    var sec = 0;
    var active = 1;
    
    function startTimer() {
        if (active==0) return;
        
        if (timer == 0) timer = <?=$timer?> ;
        timer--;
        
        min = parseInt(timer/60);
        sec = parseInt(timer%60);
        
        
        if(timer < 1) {
            $timer = 0;
            min = 0;
            sec = 0;
            document.getElementById("msg").innerHTML = "You have to start again" ;
            document.getElementById("time").value = "00:00:00";
            active = 1;
            return;
        }
        else {
            if (min < 10) min = '0'+min;
            if (sec < 10) sec = '0'+sec;
            document.getElementById("msg").innerHTML = "" ;
            document.getElementById("time").value = "00:" + min.toString() + ":" + sec.toString();
        }

        setTimeout(function() {
        startTimer();
        }, 1000);
    }

    function stop() {
        if (min + sec > 0) {
            alert("Thank you for completing the test. You finished the test at: " +  min.toString() + ":" + sec.toString());
            document.form1.submit();  
            active = 0;
        }
    }

</script>
</head>
<body>
<div class="timer">
    <h1>Welcome to Timertrone.</h1>
    <br>
    <form name='form1' action='' method="post">
        Time left: <input type="text" name="time" id="time" value="">
    </form>
    <span id="msg"></span>
    </br> 
    <button name="start" onclick="startTimer()">Start</button>
    <button name="stop" onclick="stop()">Stop</button>

</div>
</body>
</html>

It only works if I get rid of the // header("Location": update.php?countdown=success");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.