PHP Checkbox Validation

回眸只為那壹抹淺笑 提交于 2020-01-06 05:01:12

问题


I want to validate a PHP form with checkboxes preferably using Javascript or otherwise the client side code id:

Branch Features:</br>      
Micro Centre:<input type='checkbox' value='micro' name='features[]'>High Value Branch:<input type='checkbox' value='highval' name='features[]'>
        CBS Enabled:<input type='checkbox' value='cbs' name='features[]'>
                Trade Finance Branch:<input type='checkbox' value='tradef' name='features[]'>                 
        Personnel Banking Branch:<input type='checkbox' value='personnel' name='features[]'> 
                 High Security Branch:<input type='checkbox' value='hsbranch' name='features[]'>

and so on.. Basically on the server side I enter a Y in the database if the box is checked and a N if its unchecked. How exactly can I go about this?


回答1:


Don't be afraid and get your hands dirty! Make a simple example of HTML form with few checkboxes and the POST method, and then make simple PHP code which dumps the content of $_POST (e.g. like:

echo "<pre>";
var_dump($_POST);
echo "</pre>";

And see what is coming there... Then just process it and that's it!




回答2:


You should be performing both client-side and server-side validation. Client side validation can be circumvented by simply disabling javascript.

If you are looking to enter the checkbox data into a database... First grab the data using either PHP's $_GET[] or $_POST[] array.

You can use the print_r() function to have a peak at what you're sending to the script.

Process this data, validate it, and insert into the database. The database code is so heavily reliant on DMS you are using that I won't bother giving specifics, but I suggest taking a look at the PDO.

I always say that the quality of answer is directly proportional to the quality of question. If you need further information, consider modifying your question to be more specific.




回答3:


Here's an attempt:

function submit() {
 var checks = document.getElementsByName('features[]');
 var valid = false;   
 for (var check in checks)
        if (checks[check].checked) valid  = true;
 if (valid) 
     alert('submit');
 else
     alert('invalid')
}

Demo



来源:https://stackoverflow.com/questions/6795132/php-checkbox-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!