eng_coo Posted September 17, 2006 Share Posted September 17, 2006 hi,im pretty new to php but need to learn it for a big project. At the moment i have a page which displays a form. the user clicks on the item(s) they want and then it displays the item(s) that they clicked from an array. However i want to store what they select so that when select another item from the checkboxes it just sticks the value on the end of the previous ones (at the moment it seems to overwrite wats already in there) is this possible? heres my code - <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" form method = "POST"> <h5> Pick which phones you like - </h5> <input type = "checkbox" name = "phones[]" value="nokia"/> Nokia <br /> <input type = "checkbox" name = "phones[]" value="sony" /> Sony Erricsson <br /> <input type = "checkbox" name = "phones[]" value="motorola" /> Motorola <br /> <input type = "checkbox" name = "phones[]" value="seimens" /> seimens <br /> <input type = "checkbox" name = "phones[]" value="poo" /> Poo <br /> <input type = "submit" name = "pressed" value = "Select"><br><br><?if (isset($_POST['pressed'])) { echo"You have seleted the following phones :"; echo"<br>"; foreach($_POST['phones'] as $phone) array_push($yours, $phone); echo "$phone<br />"; } print_r($yours);As you can see i loop through the array and use array_push to add the values selected onto the end of $yours array. however when u select some more phones it overwrites the ones in the $yours array and i want it to add them to the end. im probably going bout it the wrong way.any help greatly appreciatedcheersjohn Quote Link to comment Share on other sites More sharing options...
Barand Posted September 17, 2006 Share Posted September 17, 2006 Two ways come to mind1 ) Store selected phones in session variable[code]<?phpsession_start();if (isset($_POST['pressed'])) { if (isset($_SESSION['yours'])) { $yours = $_SESSION['yours'] ; } else { $yours = array(); } echo"<br/>"; foreach($_POST['phones'] as $phone) { if (!in_array($phone,$yours)) { $yours[] = $phone; } } // save in session var $_SESSION['yours'] = $yours; echo "You have selected the following phones :<br/>"; foreach ($yours as $phone) echo "$phone<br/>"; print_r($yours);}?><form action = "<?php echo $_SERVER['PHP_SELF']; ?>" form method = "POST"> <h5> Pick which phones you like - </h5> <input type = "checkbox" name = "phones[]" value="nokia"/> Nokia <input type = "checkbox" name = "phones[]" value="sony" /> Sony Erricsson <input type = "checkbox" name = "phones[]" value="motorola" /> Motorola <input type = "checkbox" name = "phones[]" value="seimens" /> seimens <input type = "checkbox" name = "phones[]" value="poo" /> Poo <input type = "submit" name = "pressed" value = "Select"></form>[/code]Method 2 makes it easy to remove from selection also2 ) Store in the checkbox settings[code]<?phpif (isset($_POST['pressed'])) { $yours = $_POST['phones']; echo "You have selected the following phones :<br/>"; foreach ($yours as $phone) echo "$phone<br/>"; print_r($yours);}else $yours = array(); ?><form action = "<?php echo $_SERVER['PHP_SELF']; ?>" form method = "POST"> <h5> Pick which phones you like - </h5> <?php $types = array( 'nokia' => 'Nokia', 'sony' => 'Sony Erricsson', 'motorola' => 'Motorola', 'seimens' => 'Seimens', 'poo' => 'Poo' ); foreach ($types as $k => $v) { $chk = in_array($k, $yours) ? 'checked' : ''; echo "<input type='checkbox' name='phones[]' value='$k' $chk /> $v"; } ?> <input type = "submit" name = "pressed" value = "Select"></form>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.