开发者

PHP Checkbox Validation

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='checkb开发者_JAVA百科ox' 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?


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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜