samoht Posted November 9, 2007 Share Posted November 9, 2007 I have a contact form that has about 15 checkboxes on it for different interest. I am curious on the best practice for setting up my mysql table to store the info from the form and how to check which one is selected /checked Any help? Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 9, 2007 Share Posted November 9, 2007 Assuming the interests are associated with a single entitly (e.g. interests for a particular user) I would follow this approach: First, the user table should include a unique ID column for the users. Then I would create an interest table with two columns - one for a unique interest id and the interest name. Lastly, I would create an association table that will have a record for each interest associated with a user. There would just be two columns, one for the user ID and one for the interest ID: USER TABLE: user_id user_name user_phone ...etc INTEREST TABLE: interest_id interest_name USER_INTEREST TABLE: user_id interest_id Quote Link to comment Share on other sites More sharing options...
samoht Posted November 10, 2007 Author Share Posted November 10, 2007 ok - three tables but then how do I show the check boxes with there labels so that I know which ones were checked if the user gets bounced back for an incomplete form? (yes, I do want the allow the user to check more than one box) ?? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 10, 2007 Share Posted November 10, 2007 This is the logic I use for form processing: if(validate()){ process(); }else{ show(); } 99.99999999% of the time you never want to save anything to the database until the process() step. So your question of how do you redisplay the form and recheck the boxes has nothing to do with the database. Have you created a form yet? Have you seen what $_POST looks like when you submit it? Those would be two good places to start. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 10, 2007 Share Posted November 10, 2007 (edit) double post Quote Link to comment Share on other sites More sharing options...
samoht Posted November 10, 2007 Author Share Posted November 10, 2007 Thanks for the advice. I am in the process of making the form. currently I just have a js alert show if the required fields are not filled out. This is at the beginning of my processing to nothing has passed to the db yet. It is the $_POST that I'm trying to set up correctly so that it is just one variable?? but if I allow multiple checks then I can only store the info in an array - then what do I Post to my db??? Any one know of some multi-check tutorials or some thing to help with the output and POST of multi-select form info? Thanks again, Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 10, 2007 Share Posted November 10, 2007 Try this. Give each of your check inputs a name attribute of 'checks[field_name]'. For example: <form action="" method="post"> <input type="checkbox" name="checks[send_me_emails]" value="1" /> <input type="checkbox" name="checks[send_me_html_emails]" value="1" /> <input type="text" name="email" value="" size="50" /> <input type="submit" name="submit" value="Submit" /> </form> Now you'd want to have some other stuff in that form, such as labels to go along with the checks. But that's enough to submit a form. The page that processes the form: $ret = validate(); if($ret['OK']){ process(); }else{ show($ret['Errs']); } function validate(){ $valid = true; $errors = array(); if(empty($_POST)){ // Not submitted, so invalid return Array( 'OK' => false, 'Errs' => Array() ); } // Now we can check each field // check email if(!preg_match(REGEXP_EMAIL, $_POST['email'])){ $valid = false; $errors['inv_email'] = 'Your e-mail appears to be invalid.'; } // More validation continues... return Array( 'OK' => $valid, 'Errs' => $errors ); } function show($errors){ if(!empty($errors)){ // Display errors } // Display the form, refill in values from $_POST, etc. } function process(){ // Process the check controls foreach($_POST['checks'] as $key => $value){ // maybe save to database, maybe save in different structure, etc. } } Now how you save the check fields depends on what you want to do with them. Let's say you create a table with a column for each possible check in the database. Each column name corresponds to a checkbox and they have a value of 0 or 1. You may decide to use an approach where you only store checks that were actually checked. In that case a third table with two columns is better. One column to attach it to a submittal ID or something and another that is a VARCHAR for the check field name. You only need to use those approaches if you plan to run queries such as, "Which of my users checked this, that, and that other one?" If you're just saving profile information, you could just build up a big PHP array, serialize it, and save that in a single text field. But then accessing that information from anything other than PHP is difficult. You need to strike a balance between a few things. If you tie the form too closely to your database structure, a minor change in the form will require database changes and take more time and effort. If you tie the form loosely to the DB structure (as in saving a serialized array), you can change the form freely and never really worry about the DB, but that comes at the expense of not being able to ask the DB more specific questions. It's a judgement call. Quote Link to comment Share on other sites More sharing options...
samoht Posted November 12, 2007 Author Share Posted November 12, 2007 This seems to work ok but I know have a different field for each check box. For this form that gives me 18 fields. I was hopping to have one field with 18 possible values something like: enum('intrest1', 'intrest2', 'intrest3', 'intrest4', 'intrest5') etc. Quote Link to comment Share on other sites More sharing options...
revraz Posted November 12, 2007 Share Posted November 12, 2007 You can have them all in an Array. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.