-2

I have this simple code as part of a PHP file, the purpose of this code is to execute the job1.js file on the client side (has to be done so), in this file I have a function called job1() that returns 3 variable back. The returned variables must be passed to the Server for further work.

<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<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
            job1();
            window.alert( data ); // Data, actually it has only the content of the file as a string
            window.alert( textStatus ); // Success
            window.alert( jqxhr.status ); // 200
        });
    });
</script>

The function is being executed without any problems, it’s doing some internal string operations (very boring :) ) and returns 3 variables back.

My question is how do I get the 3 return variables from the job1() function with their input back. I searched a lot and even tried

var result = eval('(' + data + ')');
console.log(result);

Because the data variable holds the function as a string but still without any success, this option was suggested on some other page on this site.

EDIT: the data and the job1 have same text:

´function job1() { var text = ""; var vocabulary = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 1; i <= 500; i++) {
            text += vocabulary.charAt(Math.floor(Math.random() * vocabulary.length));
            }

    var subText1 = text.slice(0, Math.floor(Math.random() * text.length));

    var subText2 = text.slice(subText1.length, text.length);

    //test for output
    alert("This is JavaScript");

    var nextJob = "job2, job3";
    var prevJob = "null";

    return {
    text_RT : text,
    subText1_RT : subText1,
    subText2_RT : subText2
    };

18
  • The server will not execute the js. When the js is executed on the client, make an ajax call to the server, passing the values for further processing. Commented Jun 30, 2016 at 18:14
  • It is a bit confusing, if function job1 is defined in job1.js then you need to "load" the js by adding a script tag in your document before you can execute it. ex: stackoverflow.com/a/2579936/3802077 Commented Jun 30, 2016 at 18:19
  • function job1() {return [varone, vartwo, varthree];} - var job1Data = job1(); console.log(job1Data); Commented Jun 30, 2016 at 18:19
  • @user3802077 that's precisely what getScript does. Commented Jun 30, 2016 at 18:19
  • The server doesn’t have to execute the js. only the client. I'm executing the .js in my web browser on my pc with no problem. Do you have a good example of an Ajax call to the server passing the values the server? Commented Jun 30, 2016 at 18:20

2 Answers 2

3

java script is for the client side, php is for the server side, just use ajax call and pass the data using post, after job1() is done and you got the returned array, just use an ajax call like this:

var yourdata=job1();
var postdata= [{"1": yourdata[0], "2": yourdata[1], "3": yourdata[2]}];
    $.ajax({
    type: POST,
    data: postdata,
    url: "test.php", //stands for the php file you want to send the data to
    }).done(function() {
      //do something.....
    })

on the server side fetch the data using $_POST["1"] for example
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I’ll check it out tomorrow and report back
I have put some new input at the end of the post
0

Well that what I have until now

<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();
            window.alert( data ); // Data, actually it has only the content of the file as a string
            console.log(job); // shows all the return parameters from job1();
            window.alert( textStatus ); // Success
            window.alert( jqxhr.status ); // 200
            //  push the return parameters into result array
            var result = [];
            $.each(job, function(index, value) {
                //window.alert(index + ": " + value);
                result.push(value);
            });
            // post the results back to the same page 
            $.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 +'" />' +
                '</form>');
                $('body').append(form);
                $(form).submit();
            });
        });
    });
</script>

And after that to get the results on the the executedJobs.php page I used

<?php
$myArray = $_REQUEST['result'];
echo "myArray:<br/>";
print_r($myArray);
?>

It works that way, the only thing is I get the word myArray echoed twice every time before the print_r($myArray);.

20 Comments

@guest271314 do you know why is $myArray empty?
No url appears at $.ajax({ type: "POST", data: {resultArray : result}, });?
@guest271314 have put he url: but the $myArray is still empty and the POSThas calld the same page with the code 200 and content-lenght: 1041
Did you echo $myArray at php? Also, no .then() or .done() handles response from $.ajax(); e.g., $.ajax({ type: "POST", data: {resultArray : result}, url: 'execJob.php' // remove trailing comma }).then(function(data) {console.log(data)});
@guest271314 I'm really new to this, what suppose to come in the .then() or .done() section?
|

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.