mercredi 12 septembre 2018

Checking $_GET after imploding checkboxes to url to reselect previously chosen checkboxes

I Have a form, which I run through checkbox.php and implode checkboxes to a string and return via URL.

The form is here:

<form action="checkbox.php" method="post">
<span class="gamma widget-title">YEAR</span><br />

<input type='checkbox' name='product_year[]' value="1970s" <?php echo ($_GET['product_cat'] == '1970s') ? ' checked "' : ''; ?>>1970s<br />
<input type='checkbox' name='product_year[]' value="1980s" <?php echo ($_GET['product_cat'] == '1980s') ? ' checked "' : ''; ?>>1980s<br />
<input type='checkbox' name='product_year[]' value="1990s" <?php echo ($_GET['product_cat'] == '1990s') ? ' checked "' : ''; ?>>1990s<br /><br />


<span class="gamma widget-title">PRODUCT</span><br />
<input type='checkbox' name='product_type[]' value="cards" <?php echo ($_GET['product_cat'] == 'cards') ? ' checked "' : ''; ?>>Cards<br />
<input type='checkbox' name='product_type[]' value="frames" <?php echo ($_GET['product_cat'] == 'frames') ? ' checked "' : ''; ?>>Frames<br /><br />

<span class="gamma widget-title">OCCASION</span><br />
<input type='checkbox' name='product_occasion[]' value="birthday" <?php echo ($_GET['product_cat'] == 'birthday') ? ' checked "' : ''; ?>>Birthday<br />
<input type='checkbox' name='product_occasion[]' value="anniversary" <?php echo ($_GET['product_cat'] == 'anniversary') ? ' checked "' : ''; ?>>Anniversary<br />

 <br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset"><br><br>
</form>

And here is checkbox.php

$product_year = $_POST['product_year'];
$product_type = $_POST['product_type'];
$product_occasion = $_POST['product_occasion']; 

if (isset ($product_year) && !isset($product_type) && !isset($product_occasion) )  {    
    $url = 'http://localhost/yy-search-test/?product_cat=' . implode( ',' ,$product_year);
    header('Location:'. $url);
    exit;    
}

else if (isset ($product_type) && !isset($product_year) && !isset($product_occasion) ) {

    $url = 'http://localhost/yy-search-test/?product_cat=' . implode( ',' ,$product_type);

    header('Location:'. $url);
    exit;    
}

else if (isset ($product_occasion) && !isset($product_year) && !isset($product_type) ) {

    $url = 'http://localhost/yy-search-test/?product_cat=' . implode( ',' ,$product_occasion);

    header('Location:'. $url);
    exit;    
}

else if (isset ($product_year) && isset($product_type) && !isset( $product_occasion) )  {

        $merged = array_merge($product_year, $product_type);
        $url = 'http://localhost/yy-search-test/?product_cat=' . implode( ',' ,$merged);

        header('Location:'. $url);
        exit;    
}

else if (isset ($product_type) && isset($product_occasion) && !isset( $product_year) )  {
        $merged = array_merge($product_type, $product_occasion);
        $url = 'http://localhost/yy-search-test/?product_cat=' . implode( ',' ,$merged);

        header('Location:'. $url);
        exit;    
}

else if (isset ($product_year) && isset($product_occasion) && !isset( $product_type) )  {
    $merged = array_merge($product_year, $product_occasion);
    $url = 'http://localhost/yy-search-test/?product_cat=' . implode( ',' ,$merged);

    header('Location:'. $url);
    exit;    

}

else if (!isset ($product_year) && !isset($product_type) && !isset($product_occasion) )  {


$msg = "Please make a selection";
$url = 'http://localhost/yy-search-test/?msg';

header('Location:'. $url);
exit;    
}

else {
    $merged = array_merge($product_year, $product_occasion, $product_type);
    $url = 'http://localhost/yy-search-test/?product_cat=' . implode( ',' ,$merged);

    header('Location:'. $url);
    exit;    
}

Everything is working well, but I want to reselect the checkboxes based on the output string from URL.

It works fine when I select 1 category, say 1970s as then the rule is met.

Problem I have is when I select 2 categories. So then the == 1970s does not matchm, as example of string is ?product_cat=1970s,cards.

I have tried a few things, like exploding strings to array but I cannot seem to get it to work.

Can anyone help?

Thanks, Jamie




Aucun commentaire:

Enregistrer un commentaire