如果复选框选择值将为1,否则将为0,我正在尝试保存复选框值。
但是我不知道我错在哪里,价值没有保存到数据库中,请查看我的表格。
我的表格:
<form id="preferences_form" role="form" method="POST" novalidate action="{{ url('/preference') }}" class="mujucet-registration">
<h5>Nastavení zpráv</h5>
<div class="row">
<div class="col-md-4">
<div class="checkbox">
<label for="ch1">
<input type="checkbox" name="monthly" id="ch1"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Měsíční výpisy
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label >
<input type="checkbox" name="weekly"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Týdenní přehled
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="tax_reviews"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Daňové výpisy
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="quarterly"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Čtvrtletní výpisy
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="annually"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Roční výpisy
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="newsletter"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Novinky
</label>
</div>
</div>
<div class="col-md-12">
<div class="checkbox">
<label class="border">
<input type="checkbox" name="direct_mail_reviews"><span class="cr"><i class="cr-icon fa fa-check"></i></span> Chci výpisy zasílat také Poštou <span class="light">20 kč za výpis</span>
</label>
</div>
</div>
</div><h5>Kontaktní preference</h5>
<div class="row">
<div class="col-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="email"><span class="cr"><i class="cr-icon fa fa-check"></i></span>Email
</label>
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="sms"><span class="cr"><i class="cr-icon fa fa-check"></i></span>SMS
</label>
</div>
</div>
</div>
</form>
我保存我的值的控制器动作:
public function preferences(Request $request)
{
$user = Auth::guard()->user();
$preferences = new Preference();
$preferences->userID = $user;
$preferences->monthly = Input::get('monthly')? 1 : 0;
$preferences->weekly = Input::get('weekly')? 1 : 0;
$preferences->tax_reviews = Input::get('tax_reviews')? 1 : 0;
$preferences->quarterly = Input::get('quarterly')? 1 : 0;
$preferences->quarterly = Input::get('quarterly')? 1 : 0;
$preferences->annually = Input::get('annually')? 1 : 0;
$preferences->newsletter = Input::get('newsletter')? 1 : 0;
$preferences->direct_mail_reviews = Input::get('direct_mail_reviews')? 1 : 0;
$preferences->email = Input::get('email')? 1 : 0;
$preferences->sms = Input::get('sms')? 1 : 0;
$preferences->save();
return back();
}
我正在使用JavaScript代码通过按钮保存值:
submitForms = function(){
document.getElementById("preferences_form").submit();
}
我的模特:
class Preference extends Model
{
protected $table = 'db_preferences';
protected $fillable = ['userID','monthly','weekly','tax_reviews','quarterly','annually','newsletter','direct_mail_reviews','email','sms'];
}
如果有错,请通知我,我们将不胜感激。 预先感谢
答案 0 :(得分:0)
您可以检查是否已选中“复选框”。
$preferences->monthly = Input::get('monthly') == 'on' ? 1 : 0;
更简化的代码
$preferences->monthly = Input::get('monthly') == 'on' ?;
答案 1 :(得分:0)
value
属性。因为它很安全。如果使用tinyInt,此代码可能会对您有所帮助。
`$preferences->monthly = $request->has('monthly');
$preferences->weekly = $request->has('weekly');`
答案 2 :(得分:0)
问题在于 $request
不会返回空的表单元素值,因此您的复选框甚至不会出现在提交的表单输入值数组中。因此没有什么可以写入数据库。
在保存或更新 update
数据之前将此行添加到您的控制器 $request
函数中,如果未选中或选中它,则会设置 0 或 1 值。它查看 $request
数组中是否有返回的元素。如果存在,则应选中该值,否则不选中。
!$request->monthly ? $request["monthly"] = 0 : $request["monthly"] = 1;