Reputation: 47
I have some checkboxes like this:
<input type="checkbox" name="regions[]" value="north-east" />North East<br />
<input type="checkbox" name="regions[]" value="north-west" />North West<br />
<input type="checkbox" name="regions[]" value="east-midlands" />East Midlands<br />
<input type="checkbox" name="regions[]" value="west-midlands" />West Midlands<br />
<input type="checkbox" name="regions[]" value="south-east" />South East<br />
<input type="submit" name="selectionsSubmit" value="Submit" />
Names and values can not be set differently, because I am using it for this php code:
if(isset($_POST['selectionsSubmit'])) {
$regions=$_POST["regions"];
$how_many=count($regions);
if($how_many>0)
{
$link=home_url('/') ."?tag=";
if($how_many!=12 && $how_many!=0)
{
for($i=0; $i<$how_many; $i++)
{
$link=$link ."+". $regions[$i];
}
}
wp_redirect($link);
}
else
{
echo 'You did not select anything.';
}}
It generates a tag link and redirects to that link. This is my first php code ever, and I should mention that I use wordpress.
Now I need a way to make selected checkboxes to be remembered for a session... until the user closes the site. I found many ways how to accomplish this, but I couldn't make it work. I think that HTML5 sessionStorage
might be of some use, but my lack of knowledge prevents me from finding the way.
Upvotes: 0
Views: 1639
Reputation: 392
You should put
<?php session_start ?>
at the top of the page, so that you can use the $_SESSION, Remember: if your accessing or using session you need to declare the session_start first
Then
$_SESSION['religion'] = $_POST['religion'];
and so on
Upvotes: 0
Reputation: 304
Yeah, Wordpress doesn't support session. So you gotta start the session by yourself.
session_start();
May be somewhere in the beginning before the header is sent. People usually add it in the config file or the functions file. Just try it and see. And then use the session variable to store your data.
$_SESSION['regions'] = $regions;
Also, you can use 'foreach' in you're looping instead of 'for'. Just take a look about 'foreach'. It'll help a lot than 'for'.
Upvotes: 0
Reputation: 2407
Try php sessions, and if your values are not being tracked make sure to declare the session start. Try it without it first just in case wordpress has already started a session, which is likely. http://php.net/manual/en/features.sessions.php
$regions=$_POST["regions"];
$_SESSION["regions"] = $regions;
EDIT: @Chibuzu is right, if sessions isn't working just use this code ABOVE where you are trying to access or set session variables
if(session_id() == '') {
session_start();
}
Upvotes: 2