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;
1s)? You could simplify it further by testing with one or two checkboxes.