所以我通过Post ajax请求发送了一些信息:
$.ajax("<?php echo $this->url('panel/academy/locals/editlocal')?>", {
method: 'POST',
data: {
id: id, // number
distrito: newDistrito, // string
direccion: newDireccion, // string
telefono1: newTelf1, // string
telefono2: newTelf2, // string
efectivo: $('#local_'+id).find('.efectivo').prop('checked'),
visa: $('#local_'+id).find('.visa').prop('checked'),
mastercard: $('#local_'+id).find('.mastercard').prop('checked'),
american: $('#local_'+id).find('.american').prop('checked'),
deposito: $('#local_'+id).find('.deposito').prop('checked'),
central: newCentral,
}
})
efectivo,visa,american,mastercard和central都是布尔值。
在服务器中我这样做:
$prg = $this->prg();
if($prg instanceof \Zend\Http\PhpEnvironment\Response)
return $prg;
elseif ($prg === false)
return new JsonModel(array(
'msg' =>'error prg false',
'success' => false
));
$localForm = new LocalForm();
$localForm->setData($prg);
if(!$localForm->isValid()){
return new JsonModel(array(
'success ' => false,
'error' => 'Invalid',
));
}
$id = (int) $localForm->get('id')->getValue();
$local = (new LocalMapper())->byId($id);
//change local model
$local->setDistrictId($localForm->get('distrito')->getValue());
$local->setAddress($localForm->get('direccion')->getValue());
$local->setPhone1($localForm->get('telefono1')->getValue());
$local->setPhone2($localForm->get('telefono2')->getValue());
$local->setCentral($localForm->get('central')->getValue());
$localpayments = (new LocalPaymentMapper())->byLocalId($id);
// I will have to fix what I'm about to do as soon as possible
foreach($localpayments as $payment){
// Efectivo 1
// Visa 2
// Mastercard 3
// American Express 4
switch($payment->getPaymentId()){
case 1:
if(!$prg['efectivo']){
$payment->delete();
}
break;
case 2:
if(!$prg['visa']){
$payment->delete();
}
break;
case 3:
if(!$prg['mastercard']){
$payment->delete();
}
break;
case 4:
if(!$prg['american']){
$payment->delete();
}
break;
default:
break;
}
}
问题在于,当我尝试向localForm添加一个元素,其中包含一个类似于复选框的布尔值时,表单总是无效的,所以我永远不会到达我访问db的部分并保存用户所做的更改。我尝试使用$ prg数组来访问信息,但也没有运气。我该怎么做呢?我在尝试错误的做法吗?
提前致谢
这是完整的表格
<?php
namespace GrouPanel\Form\Locals;
use GrouCore\Form\Form;
use GrouCore\Form\Element\DistrictSelector;
use GrouCore\Form\Element\UserPhone;
use GrouCore\Form\Element\LocalAddress;
use Zend\Form\Element\Checkbox;
class LocalForm extends Form {
public function __construct($name = "localForm") {
parent::__construct ( $name );
$this->setAttribute ( 'novalidate', 'novalidate' );
$localDistrict = new DistrictSelector ( 'distrito' );
$localAddress = new LocalAddress ( 'direccion' );
$localPhone1 = new UserPhone ( 'telefono1' );
$localPhone2 = new UserPhone ( 'telefono2' );
$localCentral = new Checkbox ( 'central' );
$this->add ( array (
'name' => 'id',
'type' => 'text'
) );
$this->add( $localDistrict )
->add( $localAddress )
->add ( $localPhone1 )
->add ( $localPhone2 )
->add ( $localCentral );
$this->getInputFilter ();
}
DistrictSelector,UserPhone和LocalAddress都按预期工作,复选框似乎是某种方式的问题
答案 0 :(得分:0)
您使用语法jQuery.ajax(url [,settings])。尝试添加密钥:
dataType: 'json',
在您的设置结构中。
答案 1 :(得分:0)
Http在发送前将所有内容字符串化。 所以你可以在js中使用1或0而不是true或false。
efectivo: ($('#local_'+id).find('.efectivo').prop('checked')) ? 1 : 0,
添加到ajax
dataType: 'json',