3

I have this function and I want when it is the time equal to 0.I want to make my page change from index.php to gameover.html.

function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element
        .find('div')
        .animate({ width: progressBarWidth }, 500)
        .html(timeleft + " seconds to go");
    if(timeleft > 0) {
        setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }
};

progress(180, 180, $('#progressBar'));

I thought,to that if(timeleft==0) {//i use much things but i didn't managed } .

the php code I have on the index.php so I will be able to connect with my database

session_start();
include("s/connect.php");

if($_SERVER['REQUEST_METHOD'] == "POST")
{
    
    //something was posted
    $user_name = $_POST['user_name'];
    $password = $_POST['password'];
    if(!empty($user_name) && !empty($password) && !is_numeric($user_name))
    {
        
    $user_id = random_num(20);  
        $query = "insert into users ( user_id,user_name,password) values ('$user_id','$user_name','$password')";
    
    mysqli_query($mysqli, $query);
    header("Location: gameover.html");
    die;
    }
    else{
        echo "Please enter some valid information!";
    }
    
}
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Dec 23 '20 at 02:36

1 Answers1

1

Try this (if timeleft not >0, do a redirection)

<script>
function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element
        .find('div')
        .animate({ width: progressBarWidth }, 500)
        .html(timeleft + " seconds to go");
    if(timeleft > 0) {
        setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }else{
    window.location.href='gameover.html';
    }
};

progress(180, 180, $('#progressBar'));


</script>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29