Anvesh
Anvesh

Reputation: 43

making two ajax call one after other in single handler function using php and jquery

I want to write a file three.php which loads one.php and two.php one after other, i.e. two.php should load after one.php is received. And of this should happen after clicking a button present in three.php

my code for one.php

<?php

header("Content-type: image/png");

$image = imagecreatetruecolor(800, 700);

$red = imagecolorallocate($image, 250, 0, 0);

imagefilledellipse($image,10,10,10,10, $red);

imagepng($image);

?>

code for two.php

<?php

header("Content-type: image/png");

$image = imagecreatetruecolor(800, 700);

$red = imagecolorallocate($image, 250, 0, 0);

imagefilledellipse($image,10,10,10,10, $red);

imagepng($image);

?>

so help with the code of three.php which should these two php files using two different buttons

Upvotes: 1

Views: 329

Answers (1)

abhinsit
abhinsit

Reputation: 3272

Your code would ideally look like this :

File three.php

    <script type="text/javascript" src="<path to jquery>"></script>
    <button onclick="loadFiles()"></button>

    <script type="text/javascript">


        function loadFiles(){
              $.ajax({
                    type : 'GET',
                    url : '/one.php/',
                    data : {}
                    success :function(data)
                    {

                            //file one content is available in data 
                            //now calling for file2

                            $.ajax({
                                  type : 'GET',
                                  url : '/two.php/',
                                  data : {}
                                  success :function(file2Data)
                                  {
                                   //file2 available here
                                  },
                                  error : function(data)
                                   {
                                   //error handling code here
                                  }

                            });

                    },
                    error : function(data)
                    {
                        //error handling code here
                    }

                });

            }
     </script>

Upvotes: 2

Related Questions