php - Variable count is not set in array -
i want $id
increase 1 every time form submitted. should appended array $users.
why not working?
<?php $users = array(); $id = 0; if(isset($_post["submit"])){ $id = $id + 1; $users[] = $id; } echo "<pre>"; print_r($users); echo "</pre>"; ?> <form action="random.php"> buy ticket <input type="submit" name="submit"> </form>
this because once php code stops executing value of $id
, $users
gone forever. http , php stateless. once page processed gone , never existed. if want persist state need use persistent data store sessions or database.
<?php session_start(); if(isset($_post["submit"])){ if (!isset($_session['users'])) { $_session['users'] = 0 } $_session['users']++; } echo "<pre>"; print_r($_session['users']); echo "</pre>"; ?> <form action="random.php" method="post"> buy ticket <input type="submit" name="submit"> </form>
n.b.: forms defaults when method isn't defined, therefore needs method="post"
since working post variables.
Comments
Post a Comment