Jump to content


Photo

checkboxes and storing values in arrays


  • Please log in to reply
1 reply to this topic

#1 eng_coo

eng_coo
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 17 September 2006 - 01:42 AM

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 appreciated

cheers

john



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 17 September 2006 - 09:36 AM

Two ways come to mind

1 ) Store selected phones in session variable
<?php
session_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>

Method 2 makes it easy to remove from selection also
2 ) Store in the checkbox settings
<?php

if (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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users