1

it's my first post, I'm stuck on this, please help.

On a page I've got two symfony forms (without classes) from which I get some data that I store in arrays. The first form is for a contract, and the other form is used to fill a datatable of products with their data (price, quantity, etc.). On each of those forms, I've got a submit button that I manage with ajax (onclick) to get to the step forward of the pagen while preventing the data processing. The datatable listing the products is the ending step of the page and under it I have a validation button that I want to use to submit all the data from the 1st form and the datatable (in which are the data from the second form) that I previously have stored in arrays.

I know two ways of doing this ($.post with jQuery and the other with ajax but both don't work). I manage to redirect to the other page but I can't get the $_POST (or $request) data.

Here's the 'final button' ajax part (with the other way I tried in comments) :

$("#btn_final").click(function( e ) {

    // console.log(produits);

    // $.post(
    //     path_confirm, 
    //     { produits: produits },
    //     function() {
    //         window.location.replace(path_confirm);
    //     }
    // );


    $.ajax({
        url: path_confirm,
        type: 'POST',
        // dataType: 'html',
        data: { 'produits' : produits, 'contrat' : contrat },
        // context: this,
        // dataType: "json",
        success:function(data) {
            window.location.replace(path_confirm);
            // console.log(data);
        },
        error:function(data) {
            alert("erreur");
        }
    });

and here's my basic controller supposedly getting the data :

public function getData(Request $request) {

    // $produits = $request->request->get('produits');
    // $contrat = $request->request->get('contrat');

    if ($_POST) {
       var_dump($_POST);die;
    } else {
        echo "<h1> no data :/ </h1>";die;
    }

I get redirected to the page but the answer is "no data". Thank you in advance for your help.

3
  • 1
    Are produits and contrat defined? Commented Sep 5, 2018 at 12:17
  • Yes @FelDev, right here data: { 'produits' : produits, 'contrat' : contrat } Commented Sep 5, 2018 at 12:18
  • Yes, I did a 'console.log' just before the .click() function to be sure they are defined before putting them in the ajax function. Commented Sep 5, 2018 at 12:28

2 Answers 2

2

Because the redirection causes the POST data to be lost., The AJAX request is expecting to return the information via AJAX. Add a prevent default to stop the redirection:

$("#btn_final").click(function( e ) {
    e.preventDefault();

Then observe the AJAX request on the developer tools. If $('#btn_final') is a submit button you will need to utilize the .submit() handler.

If you plan to do a redirect to the PHP script processing the data there is absolutely no need for jQuery or AJAX at all.

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

8 Comments

Hi, thank you for your quick answer. It wasn't a submit button (I thought it was ok to do it like that), this is why I wrote a ".click()" function. I've tried with a submit button and wrote a ".submit()" function but now when clicking on the button it just doesn't do anything. I also have put the "e.preventDefault()" line
OK - then you can just use a button, no need for submit. Where are you setting your post variables?
The problem is the redirection inside the success callback. @HalimEntation You're first making an Ajax request to the URL with POST parameters, then discarding the result and redirecting to the same URL which makes a new GET request without any parameters. What you're seeing in the browser is the result of this second request.
My array "contrat" is defined before the ".submit()" function of the first form, and my array 'produits' is defined before the ".submit()" function of the second form. During the two ".submit()" functions, I just push the data to the arrays and after the two submit() functions I get the two arrays filled.
Alright @JJJ I think I got it, but how am I supposed to redirect after posting the data ? I shouldn't redirect in the success callback ?
|
0

I managed to do it with one simple line thanks to a jQuery plugin called "redirect". If that can help someone up later :

What you need is simply import the script from here in your view : https://github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js

Then simply write :

$.redirect(url, [values, [method, [target, [traditional, [redirectTop]]]]])

In my case, I wrote :

$.redirect(path_confirm, {'produits': produits, 'contrat': contrat, 'pj': pj, 'idclient': id});

and it works.

Comments

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.