user123
user123

Reputation: 5407

Post parameters value to multiple file during ajax requesnt

I am passing myid to url.php page and also loading this by ajax request. At same moment I want to post myid to some other file other then url.php. Like x.php,y.php.

FB.api('/me?fields=movies,email,name', function(mydata) {
           var email=mydata.email;
              var json = JSON.stringify(mydata.movies.data);
              var a = JSON.parse(json);
              $.post('movies_db.php',{'myd':a, name: name, email: email, myid:myid}, function(data) 
                     {
                       $.ajax({
                         url:'url.php'
                         ,async:     true
                         ,type : 'POST'
                         ,cache:     false
                         ,data : 'myid=' + myid   //Here I post myid to url.php. How can I post it to other files like x.php , y.php on same moment?
                         ,dataType:  'html'
                         ,success:   function(data){
                           $('body').html(data);
                           FB.XFBML.parse();
                         }
                       }
                             );

                     }
                    );

Bottom line is I want to use current user_id e.i. myid on various page after user performs login action.

UPDATE

I post variables from index.php to url.php. inside index.php: $.ajax({ url:'url.php' ,async: true ,type : 'POST' ,cache: false ,data : 'myid=' + myid //Here I post myid to url.php. How can I post it to other files like x.php , y.php on same moment? ,dataType: 'html' ,success: function(data){}

In url.php I will recv:

<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['myid'];
?>

Now in url.php suppose I alsi have:

<script>
function funInclude()
{
  include "data1.php";
  include "data2.php";
}
</srcipt>

So data1.php and daa2.php both of them will recv myid right?

Upvotes: 0

Views: 193

Answers (1)

Don Rhummy
Don Rhummy

Reputation: 25820

You have two options.

  1. Put two more ajax calls inside the success callback of the first ajax call

  2. In the url.php page, simply include the other two php pages and they'll also get the variable data

If you choose option 2, I suggest you do the include inside a function call to partially separate those pages' variables from the calling page

Edit: To explain option #2 and putting it in a function call:

In PHP, when you include a file, you are not just "calling" the other page, but including the code as though it were part of the currently running page. They will be compiled together as one page. This can cause unforeseen problems as page 1, which includes page 2, might have a variable of the same name as page 2 and page 2 could change that variable. This would lead to an unexpected variable value on page 1.

To account for this, PHP allows you to (partially) restrict the scope of an include page and its variables by calling the include inside a function.

Example of "calling.php"

<?php
//This included page's variables are accessible as though they were in this "calling.php" page
include "included.php";

function restrictScope()
{
    //This page's variables/changes are only accessible inside this function
    include "included2.php";
}

http://php.net/manual/en/function.include.php

EDIT: Complete working example

This is the base page: include.php

<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>
        <form action="include.php" method="POST">
            <input type="hidden" name="myid" value="3" />
            <input type="submit" name="submit" value="submit" />
        </form>
    </body>
</html>
<?php
if ( $_SERVER[ "REQUEST_METHOD" ] == "POST" )
{
    //Let's call included.php
    passOff();

    //Now let's check it actually did it
    $savedId = ( file_exists( "myid.txt" ) ) ? file_get_contents( "myid.txt" ) : "null";

    echo "MyId: " . $savedId;
}

function passOff()
{
    include "included.php";
}
?>

This is the included page: included.php

<?php
if ( $_SERVER[ "REQUEST_METHOD" ] == "POST" )
{
    //Create and write the "myid"
    file_put_contents( "myid.txt", $_POST[ "myid" ] );
}
?>

IMPORTANT: For my example to work, you must have write permissions in your web folder. But even if the writing of the file fails, the included page still gets the posted variables.

Upvotes: 1

Related Questions