loose my mind loose my mind - 3 months ago 10
CSS Question

set up error message for drop down

Is it possible to set up an error message for drop down menu?

On first page I have a drop down menu the user can select any combination and click submit.

On second page I have the same exact dropdown menu and if the user does not select the same combination, some error is shown.



<select id="men" class="select_class1" name="subselector" style="display:none" >
<option value="">Choose an item</option>
<option value="tsm">T-Shirt</option>
<option value="lsm">long sleeve</option>
<option value="tankm">Tank Top</option>
</select>
<select id="color" class="select_class1" name="subselector" style="display:none" >
<option value="">Choose an item</option>
<option value="blue">blue</option>
<option value="red">red</option>
<option value="black">black</option>
</select>




Answer

Yes. You can acheive that with the combination of PHP and JQUERY

Let's say this is your first page

<form action="secondPage.php" method="post">
    <select id="men" class="select_class1" name="subselector">
        <option value="">Choose an Item</option>
        <option value="tsm">T-Shirt</option>
        <option value="lsm">Long Sleeve</option>
        <option value="tankm">Tank Top</option>
    </select>
    <input type="submit" value="submit" />
</form>

Then this would be your secondPage.php

<?php
    $subselector = $_POST['subselector'];
?>
<form action="anotherPage.php" method="post">
    <select id="men" class="select_class1" name="subselector">
        <option value="">Choose an Item</option>
        <option value="tsm">T-Shirt</option>
        <option value="lsm">Long Sleeve</option>
        <option value="tankm">Tank Top</option>
    </select>
    <input type="submit" value="submit" />
</form>

<!-- Now using jQuery to prevent the form from submitting if only the previous selected value doesn't correspond with the present selected value -->
<!-- don't forget to include your jQuery library here -->

<script>
    $('form').submit(function(e){
        prevSelVal = "<?php echo $subselector; ?>";
        presentVal = $('select').val();
        if(prevSelVal !== presentVal){
            e.preventDefault();
            alert('Your Previous Selected Value must Correspond With Your Present Selected Value');
        }
    });
</script>
Comments