good day to all! I'm kinda new at coding using php.. and I have a problem with the expected output of my code below.. For example, at first run.. I will input 'A' on the textfield named 'plate' when I press the button Park, the output would be: Array ([0] => A [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) then when I input 'B' then pressed 'Park'.... the output is like this.... Array ([0] => B [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) I was kinda expecting the output to be like this.. Array ([0] => A [1] =>B [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) The problem is that the value that I will input is always saved at the FIRST index of my array everytime I press Park.. I was expecting that when I pressed 'PARK', the value that I will input will be saved to the next index and not overwrite the value of the first index of the array.. our prof has clearly specified that we should not use session when solving this machine problem...is it possible without using sessions? any ideas? I would really appreciate it. thank you! (^_^) here's my code.. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> </head> <body> <?php $arr= array("","","","","","","","","",""); ?> <form method="POST" action="index.php"> Plate #: <input name="plate" type="text" ><br/><br/> <input name="park" type="submit" value="PARK"> </form> <?php if(isset($_POST['park'])) { $plate= $_POST["plate"]; $flag=0; for($i=0; $i<10;$i++){ if(($arr[$i]=="") && ($flag==0)){ $arr[$i]= $plate; $flag=1; } } print_r($arr); } ?> </body> </html> Code (text):
Here is the solution to your problem if you are using a session can solve your problem because store the array in session variable because after every time submit button is pressed the array gets over written. so <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <?php session_start(); ?> <title>Untitled Document</title> </head> <body> <?php //this will start a session if(!isset($_SESSION['var_arr'])) $_SESSION['var_arr']=""; //following code will assing array stored in session to varaible and for first time execution array is not there so the is_array is used. $arr=$_SESSION['var_arr']; if(!is_array($arr)) $arr= array("","","","","","","","","",""); ?> <form method="POST" action="index.php"> Plate #: <input name="plate" type="text" ><br/><br/> <input name="park" type="submit" value="PARK"> </form> <?php if(isset($_POST['park'])) { $plate= $_POST["plate"]; $flag=0; for($i=0; $i<10;$i++){ if(($arr[$i]=="") && ($flag==0)){ $arr[$i]= $plate; $flag=1; } } $_SESSION['arr_var']=$arr; print_r($arr); } ?> </body> </html> see this solution wll work for u. php_chess www.techiquest.blogspot.com
That's a good point, you will need a session to retain the values from post to post, or you will need to output the content to a hidden form field within the page. The session storage is always a more secure option. If you are not required to have a fixed length to your array, you can always use array_push() to append a new value. You have to create the array first, then you can add elements to it like this: <?php $my_array = new Array(); if(isset($_POST['Park'])) { array_push($my_array, $_POST['plate']); } ?> Code (markup): If you want more information on array_push, check out the PHP docs. Good Luck!