将JavaScript / jQuery变量发布到另一个PHP页面并重定向到该页面

I’m trying for a long time to:

  1. Execute a .js file on the client side (succeeded).
  2. Pass the returned object (the returned variables) to another php page as an array for further work with them (succeeded with problems).

The 1. Point I’m able to execute with no problem on the client side with

Some PHP code to prepare the variables for the jQuery part, for example like the jobJSPath[0].

<script type = "text/javascript" >
 $(document).ready(function() {
        // $jobJSPath[0] holds the path to the job1.js file
        var scriptPath = "<?php echo $jobJSPath[0]; ?>"; 
        $.getScript(scriptPath, function(data, textStatus, jqxhr) {
            // Call the function
            var job = job1();
            console.log(job); // shows all the return parameters from job1() as an Object;

           // push the return parameters from the Object into the result array without the labels
            var result = [];
            $.each(job, function(index, value) {
                //window.alert(index + ": " + value);
                result.push(value);
            });

For the 2. Part I use an Ajax call

        $.ajax({        
            type: 'POST',
            data: {result : result},
            url: 'executedJobs.php',
            }).done(function(data) {
                var redirectUrl = "executedJobs.php";
                var form = $('<form action="' + redirectUrl + '" method="post">' +
                '<input type="hidden" name="result" value="'+ data +'" />' +
                '<input type="hidden" name="job_batch_id" value="<?php echo $jobsArray[0];?>" />' +
                '<input type="hidden" name="job_id" value="<?php echo $jobsArray[1];?>" />' +
                '<input type="hidden" name="job_name" value="<?php echo $jobsArray[2];?>" />' +
                '</form>');
                $('body').append(form);
                $(form).submit();
            });
        });
    });
</script>

The problem is:

When I’m being redirected to the executedJobs.php all my echo strings there are being posted 2 times, one time without the variables outputs any directly after that with the outputs like this:

Job Return-Arguments

Job Return-Arguments Array ( [0] => ixFm5QjDGZ4PWLC1phPcSZUF4 3Byi23bxr6sd6Hw9JSOpLBEcafkzgfDFcO0jO4DcVCk67aH4aDcqiQYTEdSsGrnufo7MfXronhl1IRL9Luz7Wq8dgZVvON1STUGJKrA26NdWqe1teHQ4hbOWojyzyEC6s8aHs991qZDWbp 54UCUF4w2UV5c2INB1F8usENsFAg4vT8LNAqMzpNiqr Bhp5MC6kSLhlbXLHozsGVdyej63rANnPMWQflpjXtEsmD6Hbu7Ut2efHwPr7u9A9gYjcGPlrtkC2ZDjThKYx6wylRb6YNUHMrb6nNkwhlaMla8iiTPrfEO6v5WSBB7ubuIZreadDd2Gr0p69xJtCdWZkvlisn4MbJsE e0xWf8xjjGNexNPABGbXH29JYgt2iejC9 tuvo6mEFreUapRjGuLnOqCssvKORENrClI34ioTXnpx9BmmMDXn5OLcXcmtPHb0kAVDEJ0ZfnrqX7MGd2z3XFXGYk [1] => ixFm5QjDGZ4PWLC1phPcSZUF4 3Byi23bxr6sd6Hw9JSOpLBEcafkzgf [2] => DFcO0jO4DcVCk67aH4aDcqiQYTEdSsGrnufo7MfXronhl1IRL9Luz7Wq8dgZVvON1STUGJKrA26NdWqe1teHQ4hbOWojyzyEC6s8aHs991qZDWbp 54UCUF4w2UV5c2INB1F8usENsFAg4vT8LNAqMzpNiqr Bhp5MC6kSLhlbXLHozsGVdyej63rANnPMWQflpjXtEsmD6Hbu7Ut2efHwPr7u9A9gYjcGPlrtkC2ZDjThKYx6wylRb6YNUHMrb6nNkwhlaMla8iiTPrfEO6v5WSBB7ubuIZreadDd2Gr0p69xJtCdWZkvlisn4MbJsE e0xWf8xjjGNexNPABGbXH29JYgt2iejC9 tuvo6mEFreUapRjGuLnOqCssvKORENrClI34ioTXnpx9BmmMDXn5OLcXcmtPHb0kAVDEJ0ZfnrqX7MGd2z3XFXGYk )

Job-Batch-ID: ,Job-ID: and Jobname:

Job-Batch-ID: 1, Job-ID: 1 and Jobname: job1

I’m suspecting that this happens because of the Ajax POST call followed by the .done() section with var form = and at the end $(form).submit(). The thing is when I’m using the window.location.href = "executedJobs.php?result=" + data+ ""; in the .done() action instead 0f the var form = followed by the $(form).submit() action I get the same echo duplicates and all the passed data are shown in the URL because of the ?result=" + data+ ""; part which I’m trying to avoid also, therefore I use the form with the type = “hidden”.

I could instead of the whole $.ajax({………..}).done({………..}) part do only this:

var redirectUrl = "executedJobs.php";
var form = $('<form action="' + redirectUrl + '" method="post">' +
                '<input type="hidden" name="result" value="'+ result +'" />' +
                '<input type="hidden" name="job_batch_id" value="<?php echo $jobsArray[0];?>" />' +
                '<input type="hidden" name="job_id" value="<?php echo $jobsArray[1];?>" />' +
                '<input type="hidden" name="job_name" value="<?php echo $jobsArray[2];?>" />' +
                '</form>');
$('body').append(form);
$(form).submit();

But then my result array is being passed as a string and not as an array to the executedJobs.php.

Can someone please help me with passing the result array from point 1 with all the other data defined in the var form = to the executedJobs.php without getting the echoed duplicates as shown above?

I hope I was clear enough with the post, I tried to explain as much as I could. Please do ask if something is still not clear enough.

This is because two times the "executedJobs.php" file is executed. Once when the ajax call is called, i.e., the url of ajax call is executedJobs.php Another is when the form is submitted in the success function call of ajax call.

So, you are receiving the two sets of output.

You can try to save the result in cookies, if using HTML5 in localstorage. And access that data to other page, on load of the page. Else you want to go with same approach, then you can use two different Files for ajax call and form submission.