weixin_33674976 2015-05-09 17:52 采纳率: 0%
浏览 5

使用ajax搜索表单

I am currently trying to find an address based on zip code using Ajax.

When I execute it, it returns an unexpected display. Instead of having a list of addresses with a search form, the list is repeated several times until it returns an error.

Can anyone provide any clarification?

The file :index.html.twig

<div class="loading"></div>

    {% include 'LesproduwebBundle:Adresse:index.html.twig' with {'entities':entities }  %}

 
<script>
$(".loading").hide();
$("#form_recherche").submit(function(){
    $(".loading").show();
    var codePostal = $("#codepostal").val();
    var DATA = 'codePostal=' + codePostal;
    $.ajax({
        type: "POST",
        url: "{{ path('adresse_recherhe')}}",
        data: DATA,
        cache: false,
        success: function(data){
           $('#resultats_recherche').html(data);
           $(".loading").hide();
        }
    });   
    return false;
});
</script>

The Controller

public function rechercherAction()
{              
    $request = $this->container->get('request');
 
    if($request->isXmlHttpRequest())
    {
        $codePostal = '';
        $codePostal = $request->request->get('codePostal');
 
        $em = $this->container->get('doctrine')->getEntityManager();
 
         if($codePostal != '')
        {
               $qb = $em->createQueryBuilder();
 
               $qb->select('a')
                  ->from('LesproduwebBundle:Adresse', 'a')
                  ->where("a.codePostal LIKE :codePostal")
                 
                  ->setParameter('codePostal', '%'.$codePostal.'%');
 
               $query = $qb->getQuery();              
               $entities  = $query->getResult();
        }
        else {
            $adresse = $em->getRepository('LesproduwebBundle:Adresse')->findAll();
        }
 
        return $this->container->get('templating')->renderResponse('LesproduwebBundle:Adresse:index.html.twig', array(
            'entities ' => $entities 
            ));
    }
    else {
        return $this->indexAction();
    }

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题