Jump to content

Checkbox value(s) into array then into $_SESSION


Hyperjase

Recommended Posts

Hi all,

 

I have a set of checkboxes which I need to be able to send into a session, which can be picked up later but exploded (it could be a possible maximum 7 options, may be only be 1).

 

I've been experimenting with foreach, but I can't get that into a session to be able to reuse, only $_POST which can't be carried over different pages.  Also, once I have them inside a session, how do I print what's been selected (rather than having to pick out manually).  IE - so if six have been selected, it prints those six options, or if it's two then it prints just those two choices.

 

Thanks!

 

Jason

Link to comment
Share on other sites

what exactly are you having trouble with?

it should look something like this,

 

foreach($_POST['tag_name'] as $key => $option) {
      //add filtering etc here
      $_SESSION[$key] = $option;
}

 

of course, you will probably want to name your sessions something other than the key, but you get the idea. do you want the options to output on the screen asynchronously or not?

Link to comment
Share on other sites

Fantastic, that looks much easier than what I've been working with.

 

My biggest problem is that it's a list of toppings - it will need to be listed out at some point, preferably with a number by the side of it, also it will need to be inserted into a database.

 

Sorry, I know exactly what I need to achieve just having problems trying to word it right!

 

Thanks!

Link to comment
Share on other sites

Assuming the checkboxes are configured as an array in the form, you don't need to loop them to put them into the $_SESSION array. Just assign the incoming $_POST subarray to the $_SESSION array directly.

 

$_SESSION['toppings'] = $_POST['toppings'];

 

For your last question, you can add the commas and ampersand in a few different ways. Here's one, based on the array being structured as shown above, and assuming the names of the toppings are in the value= attributes of the checkboxes.

 

$last = array_pop($_SESSION['toppings']);
$toppings = implode(', ', $_SESSION['toppings']) . ' & ' $last;
echo $toppings;

Link to comment
Share on other sites

In the form the name is listed as topping[] - so as far as I know that takes them in to an array ... ?  Here's a line of that code:

<input name="topping[]" type="checkbox" value="Vanilla" checked>Vanilla<br />

 

Re the last code you posted, I pasted it into my code, inside the foreach and it spat an error out, this is what I ended up with code wise:

if(!empty($_POST['topping'])){
foreach($_POST['topping'] as $topping => $toppingarray) {
      $last = array_pop($_SESSION['topping']);
	$topping = implode(', ', $_SESSION['topping']) . ' & ' $last;
	echo $topping; 
    }
}

 

I couldn't see anything obvious but then again I missed a letter c from a variable and spent an hour headscratching!!

 

Thanks for your help,

 

Jason

Link to comment
Share on other sites

if you are going that route,

 

if(!empty($_POST['topping'])){
      $_SESSION['topping'] = $_POST['topping'];
      $last = array_pop($_SESSION['topping']);
	$topping = implode(', ', $_SESSION['topping']) . ' & ' $last;
	echo $topping; 
}

Link to comment
Share on other sites

This thows up an error:

 

$topping = implode(', ', $_SESSION['topping']) . ' & ' $last;

 

This is what I get :

Parse error: syntax error, unexpected T_VARIABLE in /creator.php on line 125

 

I can't see anything obvious!

 

Also what happens when there's only 1 result?

 

Thanks, Jason

Link to comment
Share on other sites

Missing a concatenation operator before $last.

 

' & ' . $last;

 

Surely you can figure out how to handle the value if there are none, or one selected by using count, no?

 

Thanks, works a charm, didn't spot that to be honest!

 

I will figure the count() out, I'm still on my php learning curve, maybe experimenting with bits I've never tried before but learning quickly!

 

Thanks again,

 

Jason

Link to comment
Share on other sites

Sorted!

 

Figured it out with count() - is this correct format wise? This does work perfectly as I need it to:

if (count($_SESSION['topping']) == 1) { echo " cupcakes with the following topping: ".array_pop($_SESSION['topping']); }
else if(!empty($_POST['topping'])){
echo " cupcakes with the following toppings: ";
      $last = array_pop($_SESSION['topping']);
$topping = implode(', ', $_SESSION['topping']) . ' & ' .$last;
echo $topping; 
}
echo "<br />";
if (count($_SESSION['swtopping']) == 1) { echo "You selected the following sweetie topping: ".array_pop($_SESSION['swtopping']); }
else if(!empty($_POST['swtopping'])){
echo "You selected the following sweetie toppings: ";
      $last1 = array_pop($_SESSION['swtopping']);
$swtopping = implode(', ', $_SESSION['swtopping']) . ' & ' .$last1;
echo $swtopping; 
} else { echo "You didn't select any toppings, is this correct?  If not please <a href='javascript:history.go(-1)'>go back</a> and choose some!"; }

 

Which brings me to a new question.  At the very bottom, if you haven't select a certain type, it asks if you want to change something.  Whenever I go back using the browser it asks if I want to resend the information .... and what that link it also does the same.  With link to ?step=2 it will go back but nothing loads on the screen (as in the form).  What can I do to get around this?

 

Cheers

 

Jason

Link to comment
Share on other sites

I'm having an odd issue.

 

I'm putting all the info in a database, and want to put the array from topping into there - only problem is it always inserts data minus one selected option.  Sounds odd but I have no idea whats going on, been trying many things but can't figure out whats going on.

 

Here's the code:

if (count($_SESSION['topping']) > 1) {
$last1 = array_pop($_SESSION['topping']);
$topping = implode(', ', $_SESSION['topping']) . ' & ' .$last1;
} else { $topping = $_SESSION['topping']; }

My view on this is if it's more than 1 it adds commas and ampersand as required.  If it's only 1 then it only inserts the one entry in the array within $_SESSION['topping'] but I can't get it to insert the lone entry in the array.  Even when there's three, only two show in the database!

 

Please help!!

 

Thanks and Happy New Year!!

 

Jason

Link to comment
Share on other sites

Thanks for the prompt reply, however this doesn't work for what I need it to do - I need to take all of the variables within the array - but using this code prior to the mysql query it drops one from the end of the array, if there's none, it inserts no data:

<?php
if (count($_SESSION['topping']) > 1) {
$last1 = array_pop($_SESSION['topping']);
$topping = implode(', ', $_SESSION['topping']) . ' & ' .$last1;
} else { $topping = $_SESSION['topping']; }
?>

 

I tried to make it so more than one, it used comma and ampersand, just one it shows just the one (after the else).

 

Thanks again,

 

Jason

Link to comment
Share on other sites

Right ... sorry to keep replying to myself but I've figured out the problem:

<?php 
if (count($_SESSION['topping']) == 1) { echo " cupcakes with the following topping: ".array_pop($_SESSION['topping']); }
else {
echo " cupcakes with the following toppings: ";
$last = array_pop($_SESSION['topping']);
$topping = implode(', ', $_SESSION['topping']) . ' & ' .$last;
echo $topping; 
}
$_SESSION['topping2'] = $topping;
echo "<br />";
if (count($_SESSION['swtopping']) == 1) { echo "You selected the following sweetie topping: ".array_pop($_SESSION['swtopping']); }
else if(count($_SESSION['swtopping']) > 1){
echo "You selected the following sweetie toppings: ";
      $last1 = array_pop($_SESSION['swtopping']);
$swtopping = implode(', ', $_SESSION['swtopping']) . ' & ' .$last1;
echo $swtopping; 
} else { echo "You didn't select any toppings, is this correct?  If not please <a href='?step=2'>go back</a> and choose some!"; }
$_SESSION['swtopping2'] = $swtopping;
?>

 

When two check boxes are selected, the data is correctly copied into the database (I've pushed everything into new sessions (topping2 an topping2) which are the ones in the mysql query.

 

Now both times it checks for zero or one results within the array, these seem to be the issue.  Within those, the array doesn't seem to be changed to the new session.  Nothing appears within the database.

 

Anyone suggest where I'm going wrong on this?

 

Thanks!

 

Jason

Link to comment
Share on other sites

why so many variables for toppings?

wouldnt it be easier to add in attributes to the toppings?

I see this as a lot of extra coding, but it's your code.

 

I think you may need a new perspective on what your trying to accomplish.

 

You never assign topping, when u have 1

array_pop($_SESSION['topping'])

to

($topping=array_pop($_SESSION['topping']))

Link to comment
Share on other sites

I've been experimenting with the code, this, as it is, works fine for multiple check boxes. I know that $topping is not defined that's why I posted the code as everything I'd tried doing didn't work. I will admit I'm still learning php but feel its working ok so far.

 

Thanks for your assistance!

 

Happy new year!

 

Jason

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.