2

This is the code fragment I have :

<html>
<script type="text/javascript">
function Run()
{var a=["ONE" ,"TWO" ,"THREE", "FOUR", "FIVE"];
window.location.href = "index.php?w1=" +a;}
</script>
<body>
 <input type="button" id="upload" value="RUN" onclick="Run();"  />
<body>
</html>

when clicked 'RUN', I need to send array value to multiple pages one after the other without shifting between pages.

1. Page1 index.php  // a has no use, executed first
2. Page2 index2.php // a is used here, executed second
3. Page3 index3.php  // a is  used here, executed third

Is it possible?

Rahul Gopi
  • 414
  • 1
  • 16
  • 31

2 Answers2

2

Sounds like you can achieve what you're trying with ajax. To make them run synchronously (one after the other), you could call the next PHP page in the success callback of the currently running ajax. Some pseudo code below:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

    <script type="text/javascript">
    var data = "my data";
    $.ajax({ url: 'index.php',
     data: { data: data },
     type: 'post',
     success: function(output) {             
            runindex2(output.data);
     }
    });

    function runindex2(data) {
        $.ajax({ url: 'index2.php',
         data: { mydata: data },
         type: 'post',
         success: function(output) {             
                runindex3();
         }
        });
    }

    function runindex3()....
    </script>
Jonathan Clark
  • 1,180
  • 1
  • 8
  • 26
  • I am not much familiar with Ajax. Is this code directly executable? – Rahul Gopi Jun 09 '15 at 10:48
  • You will need to include a jquery library to use my above code, or you can use vanilla javascript to ajax as well, have a look at this post for examples of both: http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery – Jonathan Clark Jun 09 '15 at 10:53
  • I checked the code fragment, How can it be checked that whether php files had recieved the array? OR Should I use this method in php page like this: var_dump($_GET["w1"]) $x=$_GET["w1"]; – Rahul Gopi Jun 09 '15 at 12:29
  • Debugging with ajax can be a little tricky, since you don't _see_ the page you're on. Instead you can see an output. If you have a look at the line success: function(output) you can console.log that output to have a look. So in your PHP you can echo a variable, and see that in the output. – Jonathan Clark Jun 09 '15 at 13:45
  • Javascript: data: { myTest: "hello" } type: 'post' .... Then on the PHP side: $myTest = $_POST['myTest']. $myTest now is equal to "hello". if you echo $myTest, then in the callback function if you console.log(output) you should see "hello" in the console. – Jonathan Clark Jun 09 '15 at 13:50
  • @ Jonathan Clark : Consider, I executed this code and inside "runindex2()", I need another variable value of 'index2.php'. Is there any possibility to get that variable value? – Rahul Gopi Jun 25 '15 at 09:52
  • I'm not sure I fully understand. Within that function you can declare javascript functions as normal using var. If you're talking about passing a variable from javascript to the PHP page, you can do something like JS: `data: { myvar = 'index2.php' }` and to retrieve in PHP will be: `$myvar = $_POST['myvar'];` – Jonathan Clark Jun 25 '15 at 10:36
  • Then consider this case, I am using another ajax POST inside index2.php , I need that data in "index1.php", Is It possible? And I could not get value using $_POST method. – Rahul Gopi Jun 25 '15 at 10:51
  • Yes, but I would suggest doing it by passing the data back and forth between JS and PHP rather than doing more JS in the PHP ajax. See my edited answer that shows how you can do this. Not sure why you couldn't get value using post. make sure type: 'post' – Jonathan Clark Jun 25 '15 at 11:00
1

Best case:

Use ajax.

<html>
    <head>
        <script type="text/javascript">
        var xmlhttp = new Array;
        var url     = new Array;

        // Create 3 instances.
        for (var i = 0; i < 3; i++) {
            xmlhttp[i] = new XMLHttpRequest;
        }
        var arr = ["ONE", "TWO", "THREE", "FOUR", "FIVE"];

        // function which will run
        // on button click.
        function Run() {
            var url1 = "test.php?w1="  + arr;
            var url2 = "test1.php?w1=" + arr;
            var url3 = "test2.php?w1=" + arr;

            url = [url1, url2, url3];

            for (var i = 0; i < 3; i++) {
                xmlhttp[i].open("GET", url[i]);
                xmlhttp[i].onreadystatechange = function() {
                    if (this.readyState == 4) {
                        document.write(this.responseText);
                    }
                }
                xmlhttp[i].send(null);
            }
        }
        </script>
    </head>
    <body>
        <input type="button" id="upload" value="RUN" onclick="Run();"  />
    <body>
</html>

Complex Solution:

use 4 iframes in the document. First keep them display none. Then on button click, open the url in all the iframes.

<html>
    <head>
        <script type="text/javascript">
        var url     = new Array;

        var arr = ["ONE", "TWO", "THREE", "FOUR", "FIVE"];

        // function which will run
        // on button click.
        function Run() {
            var url1 = "test.php?w1="  + arr;
            var url2 = "test1.php?w1=" + arr;
            var url3 = "test2.php?w1=" + arr;

            url = [url1, url2, url3];

            var iframes = document.getElementsByTagName("iframe");
            for (var i = 0; i < iframes.length; i++) {
                iframes[i].src = url[i];
            }
        }
        </script>
    </head>
    <body>
        <input type="button" id="upload" value="RUN" onclick="Run();"  />

        <iframe style='display:none;'></iframe>
        <iframe style='display:none;'></iframe>
        <iframe style='display:none;'></iframe>
    <body>
</html>

Another method:

In your server you can include other 2 files to a file. here I have included test1.php and test2.php in test.php

<?php
var_dump($_GET["w1"]);
include "test1.php";
include "test2.php";
?>

html:

<html>
    <head>
        <script type="text/javascript">
        var arr = ["ONE", "TWO", "THREE", "FOUR", "FIVE"];

        // function which will run
        // on button click.
        function Run() {
            window.location.href = "test.php?w1=" +arr;
        }
        </script>
    </head>
    <body>
        <input type="button" id="upload" value="RUN" onclick="Run();"  />

    <body>
</html>
Dinesh Patra
  • 1,125
  • 12
  • 23