Jump to content

checkboxes


samoht

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.