Symfony,使用ajax将html表单提交给控制器

时间:2019-05-17 21:34:40

标签: php html mysql ajax symfony

我正在尝试使用AJAX将hmtl格式的数据发送到控制器,但错误消息显示,控制器未接收到数据。

html格式:

<form id="evento" name="evento" method="post" >     
      <div class="form-group">
        <label for="evento-titulo" class="col-form-label">Título:</label>
        <input type="text" class="form-control" id="evento_titulo" name="evento_titulo">
      </div>          
      <div class="form-group">
        <label for="evento_descripcion" class="col-form-label">Descripción:</label>
        <textarea class="form-control" id="evento_descripcion" name="evento_descripcion"></textarea>
      </div>          
      <div class="form-group">
        <label for="evento_inicio" class="col-form-label">Inicio:</label>
        <input type="datetime-local" class="form-control" id="evento_inicio" name="evento_inicio">
      </div>          
      <div class="form-group">
        <label for="evento_fin" class="col-form-label">Fin:</label>
        <input type="datetime-local" class="form-control" id="evento_fin" name="evento_fin">
      </div>              
      <div class="form-group">
        <label for="evento_checkmail" class="col-form-label">
             <input type="checkbox"  id="chkmail" name="chkmail" style="vertical-align:middle;">            
             Añadir correo  &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
             Color:
             <input type="color"  id="color" name="color" style="vertical-align:middle;">

        </label>
        <input type="email" class="form-control" id="evento_mail" name="evento_mail" disabled="disabled">
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-outline-danger" data-dismiss="modal">Cerrar</button>
        <button type="submit" class="btn btn-outline-success" id="save_event" name="save_event">Confirmar</button>
    </div>
    </form>

通过ajax将表单数据传递给控制器​​:

$("#evento").submit(function(){     
    var data = $('#evento').serialize();        
    $.ajax({ 
        type: "POST",
        data: data,
        url: {{ render( controller('CalendarBundle:Calendar:addEvent')) }},
        success: function(data) 
        {
            console.log($data);
            alert('OK');
        }  
    });         });

控制器:

public function addEventAction() {

    $em = $this->getDoctrine()->getManager();
    //fetch the POST Data

    $request = $this->get('request_stack')->getCurrentRequest();
    $data = $request->request->all();


    $evento = new Evento();


    $evento->setBeginat(new \DateTime($data["evento_inicio"]));
    $evento->setEndat(new \DateTime($data["evento_fin"]));
    $evento->setTitle($data["evento_titulo"]);
    $evento->setMail($data["evento_mail"]);
    $evento->setDescription($data["evento_descripcion"]);
    $evento->setColor($data["color"]);

    $em->persist($evento);
    $em->flush();

    //return response
    $serializedEntity = $this->container->get('serializer')->serialize($evento, 'json');
    $response = new Response($serializedEntity);
    $response->headers->set('Content-Type', 'application/json');
    return $response;

}}

这是错误消息

  

在渲染模板期间引发了异常   (“注意:未定义的索引:evento_inicio”)。

3 个答案:

答案 0 :(得分:0)

您的AJAX网址属性不好,您应该仅调用与控制器动作addEventAction()相关联的URL(路由)。

比方说,您到addEventAction的路线是“ / event / add”,然后放入url: "/event/add",或者即使您有此路线的名称,也要放在url: "{{ path('name_of_your_route') }}"

AJAX请求的URL属性必须是发送请求的有效地址:-)

答案 1 :(得分:0)

您的Ajax请求以application/json内容类型发送,但是您的控制器期望application/x-www-form-urlencoded

尝试json_decode() ing $request->getContent()来获取您的$data

答案 2 :(得分:0)

看看这个捆绑包,它可以通过javascript:https://github.com/FriendsOfSymfony/FOSJsRoutingBundle管理Symfony URL。 请记住,Javascript将在树枝之前执行。我确定您显示的错误会显示在页面加载中。祝你好运