piano0011 Posted July 20, 2018 Author Share Posted July 20, 2018 Ok.. i will take a look and also, will the above work and is it good practice to use a separate page for the javascript code? As in <script src="">? Quote Link to comment https://forums.phpfreaks.com/topic/307521-buttons-not-aligning-at-the-center/page/2/#findComment-1559890 Share on other sites More sharing options...
Barand Posted July 20, 2018 Share Posted July 20, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307521-buttons-not-aligning-at-the-center/page/2/#findComment-1559893 Share on other sites More sharing options...
piano0011 Posted July 21, 2018 Author Share Posted July 21, 2018 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"); Quote Link to comment https://forums.phpfreaks.com/topic/307521-buttons-not-aligning-at-the-center/page/2/#findComment-1559916 Share on other sites More sharing options...
Barand Posted July 21, 2018 Share Posted July 21, 2018 For the benefit of those who cannot see your screen (ie everyone but you) can you descriibe what happens when it "works" and what happens when it "doesn't work" Quote Link to comment https://forums.phpfreaks.com/topic/307521-buttons-not-aligning-at-the-center/page/2/#findComment-1559920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.