Php 在symfony2中更改db中的值而不重定向
大家好,我在symfony2.7工作,我的问题是当我想勾选复选框时,我想在不重定向的情况下更改值(因为在控制器函数中我必须设置重定向) 所以我尝试了一些东西,但没有成功:s) 我希望你能帮助我 我的问题是,当我选中复选框时,数据库中没有任何更改 这是我的index.html.twigPhp 在symfony2中更改db中的值而不重定向,php,ajax,symfony,symfony-2.7,Php,Ajax,Symfony,Symfony 2.7,大家好,我在symfony2.7工作,我的问题是当我想勾选复选框时,我想在不重定向的情况下更改值(因为在控制器函数中我必须设置重定向) 所以我尝试了一些东西,但没有成功:s) 我希望你能帮助我 我的问题是,当我选中复选框时,数据库中没有任何更改 这是我的index.html.twig <td> {% if entity.etat==true %} <div class="switch"> <input id="cmn-toggle-{{ entity
<td>
{% if entity.etat==true %}
<div class="switch">
<input id="cmn-toggle-{{ entity.id }}" class="cmn-toggle cmn-toggle-round-flat" checked="true" type="checkbox">
<label class="after" for="cmn-toggle-{{ entity.id }}"></label>
</div>
{% else %}<div class="switch">
<input id="cmn-toggle-{{ entity.id }}" class="cmn-toggle cmn-toggle-round-flat" type="checkbox">
<label class="after" for="cmn-toggle-{{ entity.id }}"></label>
</div>{% endif %}
这是我的路由器
statut_root:
path: /statut/{id}
defaults: { _controller: SwibBundle:prospect:Statut}
你有什么问题?有错误吗?会发生什么?请求是否执行到服务器?如果希望在Javascript中使用PHP数组数据,可以使用JsonResponse。每个控制器操作都必须返回一个响应对象<代码>返回新的JsonResponse(数组('result'=>1,'message'=>'ok','data'=>'')此外,代码中有一个输入错误:ajax
success
方法的msg
参数应该命名为data
。感谢大家快速给出答案,我的问题是当我选中复选框时,数据库没有任何变化,我也不知道为什么。你有什么问题?有错误吗?会发生什么?请求是否执行到服务器?如果希望在Javascript中使用PHP数组数据,可以使用JsonResponse。每个控制器操作都必须返回一个响应对象<代码>返回新的JsonResponse(数组('result'=>1,'message'=>'ok','data'=>'')此外,代码中有一个输入错误:ajaxsuccess
方法的msg
参数应该命名为data
。感谢大家快速给出答案,我的问题是当我选中复选框时,数据库没有任何变化,我也不知道为什么。
public function StatutAction($id)
{
$em = $this->getDoctrine()->getManager();
$pros = $em->getRepository('SwibBundle:prospect')->find($id);
if($pros->getEtat()==true){
$pros->setEtat(false);
}else if($pros->getEtat()==false){
$pros->setEtat(true);
}
$em->persist($pros);
$em->flush();
if ( $request->isXmlHttpRequest() ) {
if (!$form->isValid()) {
return array(
'result' => 0,
'message' => 'Invalid form',
'data' => $this->getErrorMessages($form)
);
return array(
'result' => 1,
'message' => 'ok',
'data' => ''
);}
}
}
protected function getErrorMessages(\Symfony\Component\Form\Form $form)
{
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$errors[] = $error->getMessage();
}
foreach ($form->all() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getErrorMessages($child);
}
}
return $errors;
}
statut_root:
path: /statut/{id}
defaults: { _controller: SwibBundle:prospect:Statut}