我正在尝试将复选框值提交到数据库。表单按应有的方式提交到数据库,但所有复选框字段都没有。简而言之,如果复选框是checked
,我希望数据库上的相应列更新为1
。如果复选框不在checked
表单提交上,则该列的值应为0
.
我究竟做错了什么?请注意,我知道查询很长。一旦工作,我可能会将其分解为 2 个表。仅供参考 $this -> input -> post('email');
,$this -> input -> post('email');
是从未发布的 Ajax 调用收到的值。
我的模型:
// 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'
");
}
以及我的部分观点:
<!-- 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 函数。无需显示代码。在该函数中,它指向上面的模型。
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;