4

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!

6
  • Are you sure you getting value in array2 & not in array3 ? , if you can cross check for me. Commented Feb 26, 2013 at 10:43
  • What does a print_r($_SESSION) return in array3.php? Commented Feb 26, 2013 at 10:44
  • use the power of print_r($_SESSION); after session_start(); in array3.php Commented Feb 26, 2013 at 10:44
  • I took out the redirect header on array2, and added in the for statement into array 2 and it works fine if that is what you mean Commented Feb 26, 2013 at 10:44
  • using print session i get Array ( [user] => 1 [checked] => Array ( [0] => 1 ) [checked2] => Array ( [0] => camping ) ) You have selected to recive tickets And you have selected to recive for accommodation are you sure? Commented Feb 26, 2013 at 10:45

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

2 Comments

okay i will work on that, i got it working by removing the $checked = $_POST['options']; $checked2 = $_POST['options2']; and it works but i will look into what you are saying.
yeh i will look into that, I've only been doing php for the last 6 weeks, really just trying to get the basics done the idea behind using both in the same statement was that if you have tickets selected then you must have accommodation hopefully i turn it into a better statement but really fow what i currently know i cant do much better than that for now.
2

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.