I have a problem when I try to get list of checkbox checked in my controller
my controller:
public function comparaisonAction() {
$request = $this->get('request');
$ids=$request->get('assur');
foreach($_POST['assur'] as $valeur)
{
echo "Checkbox $valeur has been checked<br>";
}
foreach($ids as $value) {
$entities=$value;
}
var_dump($entities);
$em=$this->getDoctrine()->getManager();
//$assur1=$em->getRepository("CMSiteBundle:Offresante")->findOneBy(array('idassurance'=>$idassur));
//$queryimg1=$em->getRepository("CMSiteBundle:Assurance")->findOneBy(array('id'=>$idassur));
//$assur2=$em->getRepository("CMSiteBundle:Offresante")->findOneBy(array('idassurance'=>$id2));
//$queryimg2=$em->getRepository("CMSiteBundle:Assurance")->findOneBy(array('id'=>$id2));
//$img1=$queryimg1->getImage();
//$img2=$queryimg2->getImage();
//return $this->render('CMSiteBundle:Sante:ComparaisonSante.html.twig',array('assurance1'=>$queryimg1,'img1'=>$img1,'id'=>$idassur));
return $this->render('CMSiteBundle:Sante:ComparaisonSante.html.twig',array('id'=>$entities));
}
my view:
{% for ass in assurance %}
.....
<div style="padding-left:40px;padding-bottom:20px" ><input type="checkbox" name="assur[]" value={{ass.ident}} /></div>
{% endfor %}
You need to pass the checked checkboxes array to the view and test if the current checkbox value is in the passed array :
public function fooAction() {
//...
//...
return $this->render('CMSiteBundle:Sante:ComparaisonSante.html.twig', [
'chk_foo' => isset($_POST['chk_foo']) ? $_POST['chk_foo'] : [],
]);
}
foo.twig
{% for foo in foos %}
{% set checked = foo.id in chk_foo|default({}) ? ' checked' : '' %}
<input type="checkbox" name="chk_foo" value="{{ foo.id }}"{{ checked }} />
{% endfor %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.