0

I have a website selling fruit.

I have a category titled 'fruit' and within that category I have 'apples' and 'oranges'.

The customer chooses which category they desire to search in. Example: 'fruit'.

This is then passed to another page which displays what is in the category 'fruit'.

I then have a check box for each item withing the category. Example:

 <input type="checkbox" name="item['.$x_value.'][]" value="'.$item.'">

$x_value contains the category 'fruit' (in a while loop).

$item contains the item 'apple, orange'.

I am wanting to say; if no checkbox is selected for 'item'

if(!isset($item))

Then return to previous page

die(header("location: select_item.php"))

I am passing the $x_value through the URL using

die(header("location: select_category.php?item = $item"))

My problem is, I give the customer the option to choose 2 items 'apples and oranges'. How can I pass my $item array through the URL and received on the previous page 'select_item.php'

Here is my current code:

Page: Select Item.php

$item = $_GET['item'];
foreach($item as $x => $x_value){    

$query = mysql_query("SELECT * FROM category WHERE item='$x_value'");

while($fetch = mysql_fetch_assoc($query)){

$db_item = $fetch['item'];

echo "<p>";
echo '<input type="checkbox" name="item['.$x_value.'][]" value="'.$db_item.'"> '.$db_item.'';
echo "</p>";
}

echo "<br />";

}

Page: Action Page.php

$item = $_POST['item'];

if(!isset($item)){

die(header("location: select_fruit.php?item=$citem"));
}
James Osguthorpe
  • 143
  • 3
  • 12
  • if $category is not set then its useless to send it in the url. Catch 22, use sessions to store last category – Hanky Panky Apr 29 '14 at 15:23
  • See [How to pass an array via $_GET in php?](http://stackoverflow.com/questions/7206978/how-to-pass-an-array-via-get-in-php?rq=1) or [sending arrays in _GET in php](http://stackoverflow.com/questions/8327758/sending-arrays-in-get-in-php?rq=1) – Gabriel Santos Apr 29 '14 at 15:28
  • Gabriel, I have tried this and for some reason it doesn't work! – James Osguthorpe Apr 29 '14 at 15:30
  • @JamesOsguthorpe Improve your code. I already formatted it the first time to improve readability, but you edited the question and changed all the code to a ugly version again. – Gabriel Santos Apr 29 '14 at 15:30
  • I get the following in my URL: ../select_item.php?item[] Apple – James Osguthorpe Apr 29 '14 at 15:31
  • Hanky, I tried $_SESSION but I couldn't get the session to store all the array due to the foreach loop! – James Osguthorpe Apr 29 '14 at 15:35
  • why have you added name = item['.$x_value.'][] instead of item. – Avinash Babu Apr 29 '14 at 15:36
  • @JamesOsguthorpe string packing. If your strings don't have ` ` in them, delimit with space, then parse by space. If your strings don't have some other URL-safe character in them, use that instead. If you have very complicated strings, use escaping with quotes... (or base64-encode each item etc) –  Apr 29 '14 at 15:36

0 Answers0