Shamss Zaidan
Shamss Zaidan

Reputation: 635

Passing variable from PHP file to php file using Jquery

I want to pass a variable from file1.php to file2.php using jquery.

file1.php

<?php
    $user_rank = $rank;
?>

file2.php

<?php
    $user_rank = $_GET['user_rank'];
?>

AJAX

function getRank()
{
$.ajax({
       type: "GET",
       url: "file2.php",
       data: ?????,
       success: function(result){
         $("#TargetRank").html(result);
       }
     });         
};

Can anyone help me with this?

Upvotes: 2

Views: 2315

Answers (4)

Telmo Dias
Telmo Dias

Reputation: 4168

  1. On file1.php output the variables as JSON (see Returning JSON from a PHP Script )

  2. Then on the JavaScript do an ajax call to read the variables to an object (let's call it data).

  3. Then do your call placing the data variable where you have the ????? .

Example:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script>

    $(function(){
        $.getJSON( "http://dev/json.php", function( data ) {
            $.ajax({
                type:'GET',
                url:'json2.php',
                data: data,
                success: function(data){
                    console.log(data);
                }
            });
        });
    });
    </script>
</head>
<body>
    <div id="content"></div>
</body>
</html>

json.php

<?php
header('Content-Type: application/json');
echo '{"name":"Telmo Dias"}';

json2.php

<?php print_r($_GET); ?>

Result: Result

Upvotes: 0

www.data-blogger.com
www.data-blogger.com

Reputation: 4164

The passing part can happen in the script where the variable is defined, so in file1.php. Then you get the following files:

file1.php:

<?php
$user_rank = 123;
?>
<script>
function getRank()
{
$.ajax({
       type: "GET",
       url: "file2.php?user_rank=<?php echo $user_rank; ?>",
       success: function(result){
         $("#TargetRank").html(result);
       }
     });         
};
</script>

file2.php:

<?php
$user_rank = $_GET['user_rank'];
echo $user_rank;
?>

Upvotes: 2

j_quelly
j_quelly

Reputation: 1449

Assuming your AJAX is in file1.php you could do this:

file1.php

<?php
    $user_rank = $rank;
?>

<script>
    function getRank()
    {
        $.ajax({
            type: "GET",
            url: "file2.php",
            data: {user_rank: '<?php echo $user_rank; ?>'},
            success: function(result){
                $("#TargetRank").html(result);
            }
        });         
    }
</script>

Upvotes: 0

Dan
Dan

Reputation: 11084

I'm guessing the Javascript code is used in file1.php. Then your question becomes more like "How do I pass a PHP variable to Javascript?". The best way I have seen is with a "data element" in the DOM.

Add this to file1.php (somewhere logicalish)

<span id="user-rank" data-rank="<?= $user_rank ?>"></span>

Then you can grab that value in your JS

function getRank()
{
var rank = $("#user-rank").attr("data-rank");
$.ajax({
       type: "GET",
       url: "file2.php?user_rank="+rank,
       success: function(result){
         $("#TargetRank").html(result);
       }
     });         
};

Upvotes: 0

Related Questions