game_over_new
game_over_new

Reputation: 5

redirect checked checkbox with php and make it work

How can i make it work with php code this code, the code is about if you checked will redirect to another website but i want to create checked option with url: https://example.com/index.php?checked1

First Line of code

if (isset($_GET['checked1'])) {
    $showcheck = 'checked';
}

And javascript code below both codes are in one file index.php

<input <?=$showcheck?> type="checkbox" onclick="handleClick(this)">Redirect me after 30s<br>
<script>
  let handleClick = (ele) => {
    if (ele.checked) {
      redirectTime = setTimeout(() => {
        window.location = "/menu.php"
      }, 30000)
    } else if (!ele.checked && typeof redirectTime !== 'undefined') {
      clearTimeout(redirectTime);
    }
  }
</script>

with php is not starting redirecting after 30s

Upvotes: 0

Views: 142

Answers (2)

AnKing
AnKing

Reputation: 2174

try this:

<?
if (isset($_GET['checked1'])) {
    $showcheck = 'document.getElementById("test").click();';
}
?>

<input type="checkbox" id="test" onclick="handleClick(this)">Redirect me after 30s<br>
<script>
  let handleClick = (ele) => {
    if (ele.checked) {
      redirectTime = setTimeout(() => {
        window.location = "/menu.php"
      }, 30000)
    } else if (!ele.checked && typeof redirectTime !== 'undefined') {
      clearTimeout(redirectTime);
    }
  }

  <?=$showcheck?>  
</script>

Upvotes: -1

Quentin
Quentin

Reputation: 943548

You've said window.location = "/", which navigates to /, not to /index.php?checked1=anything.

Upvotes: 2

Related Questions