0
votes

Im trying to add a class after a form is submitted depending on the option the user chose.

<form enctype="multipart/form-data" action="upload.php" method="POST">
    title:<br /> 
    <input type="text" name="title" value="" /> <br />
    description:<br /> 
    <input type="text" name="description" value="" /> <br />
    Categories:<br /> 
    <input type="checkbox" name="categories[]" value="1">Landscpae<br />
    <input type="checkbox" name="categories[]" value="2">Portrait<br />
    <input type="checkbox" name="categories[]" value="3">Monochrome<br />
    Please choose an image: <input name="uploaded" type="file" /><br />
    <input type="submit" value="Upload" />
</form> 


<li class='INSERT OPTION HERE'> test </li>

With the option to select more than one class so add a space after each one. on my database the Value="1" "2" "3" == the option name

let me know if more information is needed

2

2 Answers

0
votes

Something along these lines might work:

$valid = array(1,2,3);#valid items
if(isset($_POST['categories']) && is_array($_POST['categories'])){
    $post = array_unique($_POST['categories']);#you might not need this, but this removes duplicate values
    foreach($post as $key => $value){
        if(in_array($value, $valid)){
            $class = true;
        }else{
            unset($post[$key]);#remove bad values, also this may not be necessary but removes unwanted items.
        }
    }
    if(isset($class)){
        $class = 'class="class' . implode(' class', $post) . '"';
    }
}#else needed for non array categories?

<li <?php if(isset($class)){ echo $class;} ?> > test</li>

DEMO

0
votes

You want to make sure that the form is submitted, and if it is then you echo a class and can create a switch statement that will alter the class depending on what was selected.

<li 
<?php 
if(isset($_POST['categories']))  {
echo 'class="';
switch($_POST['categories']) {
case 1;
echo 'class1';
break;
case 2;
echo 'class2';
break;
case 3;
echo 'class3';
break;
echo '"';
}
}
?> 
> test </li>