tags:

views:

33

answers:

3

I have a gallery page that change the category based on the $_POST("cat"), how do I use the option dropdown list to reload the page (or only the gallery) to change the gallery view accordingly.

Here is the option list:

    <form>
        <select>
        <option value="">Pick A Category:</option>
        <option value="1">Landscape</option>
        <option value="2">Wedding</option>
        <option value="3">Miscellaneous</option>
        </select>
    </form>

Here is the php option list :

 <?php
    $dir_cat[0] = "images/landscape/";
    $dir_cat[1] = "images/wedding/";
    $dir_cat[2] = "images/misc/";
    if (isset($_POST['cat']) && isset($dir_cat[$_POST['cat']])) {
      // alocate image category according to the POST value
      $image_dir = $dir_cat[$_POST['cat']]; 
    } else {
      // set default image category
      $image_dir = $dir_cat[0];
 ?>
A: 

in html you can add a value parameter to every option, this will be in the $_POST['cat'] then with a simple 'if' or 'switch' you can change the gallery view

In81Vad0
Hello, how exactly is the way to reload the page with the selected value into the "cat" POST value ?? I'm kinda lost to a javascript here..
Natasha Illyasviel
A: 
<form action="gallery.php" method= "post">
    <select name="cat" onchange="this.form.submit();">
        <option value="">Select Category:</option>
        <option value="0">Landscape</option>
        <option value="1">Wedding</option>
        <option value="2">Miscellaneous</option>
    </select>
</form>
dev-null-dweller
thank you, works like charm :)
Natasha Illyasviel
A: 
<form>
    <select id=cat name=cat>
    <option value="0">Pick A Category:</option>
    <option value="1">Landscape</option>
    <option value="2">Wedding</option>
    <option value="3">Miscellaneous</option>
    </select>
</form>

If the form uses the POST method, then you can access the cat like so;

if( isset( $_POST['cat'] ) && (int)$_POST['cat'] < 4 )

All you need to do is then match the value=n in your html form to the key in your $dir_cat[]

$dir_cat[1] = "images/Landscape" ;

Then when you are sure that the incoming 'cat' is within bounds (between 1 and 4) you can just do;

include $dir_cat[$_POST['cat']];
Cups
I see, this explains my confusion, thanks :)
Natasha Illyasviel