Reputation: 269
I am having allot of trouble trying to get an array to work with sessions if anyone can help that would be great I'm not bothered about validation etc if I can just get it working i can then expand upon it.
HTML
<form method="post" action="array2.php">
Select amount of tickets you require.
<select name="options[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
Select the acomidation you require.
<select name="options2[]">
<option value="camping">Camping</option>
<option value="caravan">Caravan</option>
</select>
<input type="submit" value="Go!" />
</form>
array2.php
<?php
session_start();
$checked = $_POST['options'];
$checked2 = $_POST['options2'];
$_SESSION['user'] = true;
$_SESSION['checked'] = $checked;
$_SESSION['checked2'] = $checked2;
header('Location: array3.php');
?>
array3.php
<?php
session_start();
if(!isset($_SESSION['user'])){
die("To access this page, you need to <a href='register.html'>LOGIN</a>");
}
$checked = $_SESSION['checked'];
$checked2 = $_SESSION['checked2'];
?>
<?php
for($i=0; $i < count($checked && $checked2); $i++){
echo "You have selected to receive " . $checked[$i] . " tickets<br/>";
echo "And you have selected to receive " . $checked2[$i] . " for accommodation are you sure? <br/>";
}
?>
The main problem is that the values are not being passed from array2
into array3
, any help is welcomed.
EDIT - this worked fine until I tried to add in the sessions to get it working over multiple pages so I'm sure thats where the problem is
EDIT2 - thanks for all the help guys i got it working when I took out
$checked = $_POST['options'];
$checked2 = $_POST['options2'];
From array 3 it worked :) much appreciated!
Upvotes: 4
Views: 2020
Reputation: 6909
Your for loop is written wrong.
for($i=0; $i < count($checked && $checked2); $i++)
That count statement will not work as you think. Break it into 2 for loops or an inner loop.
like this:
for($i=0; $i < count($checked); $i++){
echo stuff here
}
for($i=0; $i < count($checked2); $i++){
echo stuff here
}
Upvotes: 2
Reputation: 88697
count($checked && $checked2)
...is your problem.
$checked && $checked2
is a logical expression will either be true
or false
, which means thats the count()
call will always return false
, and false
will equate to 0
in a numeric comparison (the less than <
comparison with $i
), so the for loop won't perform any iterations.
However, the use of arrays in this situation is not appropriate anyway, because you have used a <select>
element without a multiple
property so it will only represent a single value, which means the array would only ever hold a single value. You should simplify this to just scalar values and you will find that a) it works and b) it's much easier in general.
Upvotes: 4