Jump to content

checkboxes and storing values in arrays


eng_coo

Recommended Posts

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

Link to comment
Share on other sites

Two ways come to mind

1 ) Store selected phones in session variable
[code]
<?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>
[/code]

Method 2 makes it easy to remove from selection also
2 ) Store in the checkbox settings
[code]
<?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>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.