简体   繁体   English

Symfony2通过Ajax保存表单数据

[英]Symfony2 Save Form Data via Ajax

I have a form in Symfony that needed to be submitted via ajax call. 我在Symfony中有一个需要通过ajax调用提交的表单。 I worte the code but it's not saving any data in db but also doesn't give/show any error. 我编写了代码,但是它没有在db中保存任何数据,但是也没有给出/显示任何错误。

send_dict = {
    type: 'POST',
    url: $(this).attr('action'),
    processData: true,
    data: $('#Form').serialize(),
    beforeSend: function(request) {alert('before send');},
    success: function (data) {alert("success")},
    error: function(xhr, textStatus, thrownError) {
        alert('Some Thing Went Wrong, Please Refresh and Try Again...');
    }
}
$.ajax(send_dict);


public function createAction(Request $request)
{
    $user = $this->getUser();
    $address = new Addresses();

    if($request->isXmlHttpRequest()) {
        // Do something...
        if ($request->isMethod('POST')) {
            $request = $this->get('request');
            $permanent_is_present   = $request->get('permanent_is_present');
            $present_address  = $request->get('present_address');
            $present_address_country   = $request->get('present_address_country');
            // Persisting Objects to the Database
            if($permanent_is_present==true){
                $address->isIsPresent(true);
                $address->isIsPermanent(true);
            }else{
                $address->isIsPresent(true);
            }
            $address->setUser($user);

            $address->setStreet1($present_address);
            $address->setCountry($present_address_country);
            $address->setState($present_address_state);
            $address->setCity($present_address_city);
            //exit(\Doctrine\Common\Util\Debug::dump($address));
            // Entity Manager To Get Connected with Doctrine
            $em = $this->getDoctrine()->getManager();
            // Persists the entire objects....
            $em->persist($address);
            // Flush queries into database
            $em->flush();
            $output = array();
            $response = new Response();
            $output[] = array('success' => true);
            $response->headers->set('Content-Type', 'application/json');
            $response->setContent(json_encode($output));
            return $response;
        }else{
            return $this->render('AddressBundle:Addresses:new.html.twig');
        }
    } else {
        return $this->redirect($this->generateUrl('address_new'));
    }

}

both alerts in before send and success on ajax function are showing. 同时显示发送前的警报和ajax功能的成功。 but the data is not saved in the database? 但是数据没有保存在数据库中? New to symfony, also don't know how to trace/debug this? 是symfony的新手,也不知道如何跟踪/调试它?

UPDATE: One more thing i notice that there is no user login in the system, and em trying to made ajax calls. 更新:还有一件事,我注意到系统中没有用户登录,他们试图进行ajax调用。 and it is sending to controller and because user id is not found so it is not saving into database 并且它正在发送到控制器,因为找不到用户标识,所以它没有保存到数据库中

You need to serialize form data. 您需要序列化表单数据。 Give your form an Id attribute then: 然后为您的表单提供一个Id属性:

data: $('#Form').serialize(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM