1

I'm trying to submit checkbox values to a database. the form submits as it should to the database, but all the checkbox fields are not. In short, if a checkbox is checked, I want the appropriate column on the database to be updated with a value of 1. If the checkbox wasn't checked on form submit, the column should have a value of 0.

What am I doing wrong? Note, I know the query is long. Once working, I will probably break this down into 2 tables. FYI $this -> input -> post('email'); & $this -> input -> post('email'); are values received from Ajax call not posted.

My Model:

// Add or update campaign on database
public function add_campaign() 
{        
    // grab campaign session data
    $userid = $this -> session -> userdata('user_id');
    $campaign = $this -> session -> userdata('campaign_name');
    $website = $this -> session -> userdata('campaign_user_website');
    $headline = $this -> session -> userdata('campaign_headline');
    $bar_color = $this -> session -> userdata('campaign_bar_color');
    $head_color = $this -> session -> userdata('campaign_head_color');
    $main_color = $this -> session -> userdata('campaign_main_color');
    $thanks_msg = $this -> session -> userdata('campaign_thanks');        

    //grab scorecard options
    $email_q = $this -> input -> post('email');
    $brand_q = $this -> input -> post('brand');
    $design_q = $this -> input -> post('design');
    $usability_q = $this -> input -> post('usability');
    $support_q = $this -> input -> post('support');
    $service_q = $this -> input -> post('service');
    $recommend_q = $this -> input -> post('recommend');
    $suggestion_q = $this -> input -> post('suggestion');
    $comments_q = $this -> input -> post('comments');

    $modified =  date('Y-m-d H:i:s');        

    // insert OR if campaign already exists, update the campaign values and date modified
    $this -> db -> query("
        INSERT INTO campaigns (userid, campaign, website, headline, bar_color, head_color, main_color, thanks_msg, email_q, brand_q, modified)
        VALUES ('$userid', '$campaign', '$website', '$headline', '$bar_color', '$head_color', '$main_color', '$thanks_msg', '$email_q', '$brand_q', '$modified')
        ON DUPLICATE KEY UPDATE userid='$userid', campaign='$campaign', website='$website', headline='$headline', bar_color='$bar_color', head_color='$head_color', main_color='$main_color', thanks_msg='$thanks_msg', email_q='$email_q', brand_q='$brand_q', modified='$modified'
        ");      
}

And portion of my view:

<!-- Scorecard options -->
<div class="scordOption roundtop">
    <div class="checkicon"><input type="checkbox" name="email" class="email_score" value="1"></div>
    <div class="scoreOptionTxt">What is your email address?</div>
</div>

<div class="scordOption">
    <div class="checkicon"><input type="checkbox" name="brand" class="brand_score" value="1"></div>
    <div class="scoreOptionTxt">How would you rate our company's branding?</div>
</div>

Ajax function that points to controller function that works fine. No need to show code. In that function it points to the model above.

var score_options = {
        email: $('input.email_score').val(),
        brand: $('input.brand_score').val(),
        ajax : '1' // needed for controller, to verify that request is ajax
    };

    //display ajax loader animation
    $('#loading').show();

    $.ajax({
        url : 'ajax/embed_step',
        type : 'POST',
        data : score_options,
        success : function(msg) {
            $('.wizardContent').html(msg);
            // output success in this container             
            $.scrollTo(0, 500);
            // scroll to top on success 
            $('#loading').hide();
            // hide loading icon
        }
    });

    return false;    
9
  • What specific errors are you getting? Is it possible for you to break down this question to a smaller example? Doing so is likely to reveal your error, and you may find the answer yourself. If not, it will make it much easier for us to help you. Commented Nov 23, 2012 at 23:02
  • No errors man. Values from the checkboxes just won't write to the database...all the other fields (i.e. from the session) are added...just no dice on the checkboxes. Not sure how I can simplify my question. I guess I could remove the controller stuff, as it doesn't matter... Commented Nov 23, 2012 at 23:20
  • So you're saying the generated SQL query is correct, but it doesn't update or insert anything? Or, what happens when you hardcode the values into the query (try all 1s)? You could simplify it further by testing with one or two checkboxes. Commented Nov 23, 2012 at 23:25
  • Exactly! doesn't add, nor update. I cut back some of the fields to simplify the question. I'll try hard coding to see the values to test. Commented Nov 23, 2012 at 23:31
  • hardcoded - the values are inputed. Commented Nov 23, 2012 at 23:36

1 Answer 1

1

All the checkbox fields register on the DB with the 1 value, regardless if the checkbox was checked

Here's your main problem:

email: $('input.email_score').val(),

This will grab the value regardless of whether or not the input was checked, you'll want to use something like this (for now at least, there are probably more efficient ways like using serialize() to submit the form, but you should understand the problem first):

email: $('input.email_score:checked').val() ? 1 : 0,

In case you aren't familiar with this syntax, it's generally called a "conditional operator", and is basically the same as doing this:

if ($('input.email_score:checked').val()) {
   email = 1;
} else {
   email = 0;
}

$('input.email_score:checked').val() will return undefined if the input is not checked. As a side note, it might make more sense to grab the input by its name attribute rather than class. There are many other ways to do this, such as $('input.email_score:checked').length (How many are checked?), but you can do it whichever way you want.

Additionally, on the PHP side, go ahead and cast to integer or use intval() so you know the values are safe:

$email_q = (int) $this->input->post('email');

Or instead, once again, use the conditional operator (probably better since you only want 1 or 0):

$email_q = $this->input->post('email') ? 1 : 0;

Checkboxes are a little tricky, since unchecked ones normally do not post at all, rather than posting an empty value which is what you might expect.

Sign up to request clarification or add additional context in comments.

3 Comments

Amazing!! Thanks man!I was LITERALLY just writing this as we speak...// Email Q - set checkbox value if($('input.email_score').is(':checked')){ var $email_q = 1; } else { var $email_q = 0; }. figured validating with jQuery was the way to go here. But I like your simplified version better.
I will say there's loads of room for improvement and making all your scripts more concise and accurate, but... baby steps - one thing at a time. Glad you figured this out and thanks for being attentive to your post.
Exactly!! Thanks man!! I'll take any tips though if you got any ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.