anonymous new
anonymous new

Reputation: 13

timer based on the user input PHP

I tried to create a timer based on the user input for how long the timer will go

here is my code for user input userinput.html

<form method="post" action="update.php">
<input type="number" name="inputnumber" />
<input type="submit" value="OK" />  
</form>

here is my code using javascript and php update.php

<script type="text/javascript">
function countdown(secs, elem){
        var element = document.getElementById(elem);
        element.innerHTML = "Please wait for "+secs+" seconds";
        secs--;
        var timer = setTimeout('countdown('+secs+',"'+elem+'")', 1000);
}
</script>
</head>
<body>
<div id="status"></div>
<script type="text/javascript">
countdown("<?php $test = $_POST['inputnumber'];?>","status");
</script>

</body>

I'm trying to pass the user input using php to javascript which is from this line

<script type="text/javascript">
countdown("<?php $test = $_POST['inputnumber'];?>","status");
</script>

I want the timer start based on the user input

but the result is the timer always start from 0

is my code wrong to passing the value from php to javascript ??

anyone know how to pass the value??

thanks

Upvotes: 1

Views: 1017

Answers (1)

Nathan Edwards
Nathan Edwards

Reputation: 311

You need to echo the post parameter, otherwise it won't print for your JavaScript to use

countdown("<?php echo $_POST['inputnumber'];?>","status");

Or shorthand

countdown("<?=$_POST['inputnumber'];?>","status");

Upvotes: 1

Related Questions