Reputation: 5
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
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
Reputation: 943548
You've said window.location = "/"
, which navigates to /
, not to /index.php?checked1=anything
.
Upvotes: 2