0

Hi im trying to dend data to my method in the controller using an ajax script :

$("#form_enr").submit(function(ev) {
    ev.preventDefault(); 

    var ev = $("#mySelect").val();
    var text =$("#form_Text").val();

    console.log(text);
    console.log(ev);

    $.ajax({
        type: "POST",
        url: "{{ path('invitation_enregistrerInv')}}",
        data : 'ev=' + ev + '&text=' + text,
        success: function(data){
            $('.portlet-body').html(data);        
        }
    });    

});

and to get it I use this syntax :

public function enregistrerInvAction()
{
    $inv = new Invitation();    
    $em = $this->getDoctrine()->getManager();
    $request = $this->get('request');

    $ev = $request->request->get('ev');
    $text = $request->request->get('text');
      
    $inv->setCreatedAt(new \Datetime());
    $inv->setUpdatedAt(new \Datetime());
    $inv->setText($text);
       
    $em->persist($inv);
    $evenement=$em->getRepository('EvEvenementBundle:Evenement')->findById($ev);
    $evenement=new Evenement();
    $evenement->setInvitation($inv);
    $em->persist($evenement);
    $em->flush();
    $this->addFlash(
        'success',
        'Les informations ont été enregistrées!'
    );

    return $this->redirect($this->generateUrl('invitation_index2')); 
}

but i seems not working, because I have this error :

An exception occurred while executing 'INSERT INTO invitation (created_at, updated_at, text) VALUES (?, ?, ?)' with params ["2016-08-01 17:12:55", "2016-08-01 17:12:55", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'text' ne peut être vide (null) !!! because text is null , and I don't get it

2

2 Answers 2

1

I believe that you pass your data wrong in js side. Try to change data to this:

$.ajax({
   (...)
    data : {
        ev: ev,
        text: text
    }
});    

(You are using POST method, you don't pass arguments in query string in this method)

EDIT:

Your controller action should look like:

public function enregistrerInvAction(Request $request){
    (...)
    $text = $request->request->get('text');
    (...)
}

Are you sure that valid data is coming from request? (you can try to do var_dump($_POST))

Sign up to request clarification or add additional context in comments.

6 Comments

and here is one problem when using this suggestion : Uncaught SyntaxError: Unexpected identifier
you need to give us more details What is exactly the error? Paste whole error info
check my edit. Did you check firebug or other tool if valid request is sent to server?
think you , but look at this : Controller "Invitation\InvitationBundle\Controller\InvitationController::enregistrerInvAction()" requires that you provide a value for the "$request" argument (because there is no default value or because there is a non optional argument after this one). I tried with : public function enregistrerInvAction(Request $request) and my controller give this after using :var_dump($_POST) , array (size=0) empty it means that I don't receive any data!!!
you need to type hint the $request parameter, I've updated the answer to reflect this
|
0

As described in the doc here, you can try:

// Better use the request passed on the controller's method signature
$request = $this->get('request');

 $ev = $request->request->get('ev');
 $text = $request->request->get('text');

instead of:

$request = $this->get('request');

$ev = $request->get('ev');
$text = $request->get('text');

Hope this help

1 Comment

Hi , I tried your suggestion but it is not working :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.