1

I have a small problem. I need to send a php variable from php file to second php file using setInterval. I dont know how i can write php variable in jquery code.

first.php

<?php

$phpvariable=1;
?>

In JavaScript

setInterval(function odswiez(id)
{

$('#chat').load('second.php?id=<here php variable how?>');
}, 3000);
});

second.php

<?php
$w=$_GET['id'];
echo $w;
?>
halfzebra
  • 6,771
  • 4
  • 32
  • 47
TonBar
  • 37
  • 3

3 Answers3

0

you need to echo your value

$('#chat').load('second.php?id=<?php echo $variable ?>');
}, 3000);

Alternative to do this will be..

$('#chat').load('second.php?id=<?php echo "id-{$variable}" ?>');
    }, 3000);
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
0

insted of

setInterval(function odswiez(id)
{

$('#chat').load('second.php?id=<here php variable how?>');
}, 3000);

to

setInterval(function odswiez(id)
{

    $.ajax({
        url: "second.php",
        type: "GET",
        data: { id: "<?php echo $php_id; ?>" }
        success: function(data){
            $('#chat').load(data);
        },
        error: function(){}             
    });


}, 3000);
Umesh Maliya
  • 115
  • 7
0

I think that's isn't the best way to manage this kind of Ajax calls, First, I suggest you instead of bringing an entire PHP file passing a GET argument, you should just pass the params and receive just plain data.

var myvalue = $('.whateverinput').val();

$.ajax({
      type: "POST",
      dataType: "json", //can be post or get
      url: "second.php", //you php who will recive data
      data: {myname : myvalue},
      success: function(data) {
        // Callback if all things goes Ok you will recive data 
        $(".the-return").html("here we atach the response"+data);
      },
      error: function(xhr, error){
        //if something wrong callback do something
        console.debug(xhr); console.debug(error);
      }
    });

I recommend using JSON to send/receive your data between PHP and javascript. You can find more info in :

Example using ajax php and using json

Good Luck!

Joaquin Javi
  • 880
  • 7
  • 12