Aditya Shukla
Aditya Shukla

Reputation: 14225

Validate input from drop down box

I have a drop down box which is pulling data from my database. when a user inputs data , should I still validate the drop down data on the server?

Upvotes: 0

Views: 421

Answers (2)

alex
alex

Reputation: 490153

An easy way to validate it is...

<?php

$array = array(1 => 'a', 2 => 'b');

if ($_POST) {

   if ( ! in_array($_POST['choose'], array_keys($array)) {
       echo 'Invalid input';
   }

}

?>

<form action="?" method="post">
    <select name="choose">
    <?php foreach($array as $value => $node): ?>
       <option value="<?php echo $value; ?>"><?php echo $node; ?></option>
    <?php endforeach; ?>
    </select>
</form>

Which you must do, otherwise it may as well be a text input :)

Upvotes: 0

Macy Abbey
Macy Abbey

Reputation: 3887

Yes. Always validate any information you are receiving from a client if you are storing, reading or performing some operation based on that data. Someone can always spoof a request not using a browser at all.

Upvotes: 6

Related Questions