php - Add Web form input to an Array -
i cannot figure out how go keeping data saved on $myarray after hitting save. everytime that, replaces other input.
i need able save user enters , save in text file. keep saving data on array , update file well.
any suggestions?
<!doctype> <html> <body> <form action="list.php" method="post"> name: <input type="text" name="name[]" /><br /> email: <input type="text" name="name[]" /><br /> <input type="submit" name="save" value="submit" /><br /> <?php $myarray = array(); if (isset($_post['save'])) { array_push($myarray,$_post['name']); } print_r($myarray); ?> </body> </html>
every time user hits submit, there new request sent script. variable $myarray
created on first line of php code, , empty array.
variables not persist across requests. suggest use either cookie, or session variable this.
example of latter here:
<?php session_start(); // has done before data sent client session_regenerate_id(); // practice prevent session hijacking ?> <!doctype> <html> <body> <form action="list.php" method="post"> name: <input type="text" name="name[]" /><br /> email: <input type="text" name="name[]" /><br /> <input type="submit" name="save" value="submit" /><br /> <?php if( !isset( $_session['myarray'] ) || !is_array( $_session['myarray'] ) ) { $_session['myarray'] = array(); } if (isset($_post['save'])) { $_session['myarray'][] = $_post['name']; } print_r($_session['myarray']); // here can write contents of $_session['myarray'] file. ?> </body> </html>
Comments
Post a Comment