1

Why does this not work?

    var data1 = "<? http_build_query($_GET); ?>";
    var data2 = "buy.php?";
    var url = data2+data1
    document.getElementById('framebox').src = url;

Thanks.

Laurent
  • 1,292
  • 4
  • 23
  • 47
  • What do you mean by it doesn't work? Does it crash? Pass the wrong variable? The right variable to the wrong place? Get the wrong data back? – andrewsi May 10 '13 at 13:49
  • What error do you get? And what does $_GET hold? – Borniet May 10 '13 at 13:51
  • it does not add key1=value1&key2=value2&... after buy.php? – Laurent May 10 '13 at 13:52
  • I'm confused. `$_GET` is a PHP array, you're trying to access it from javascript. Am I misunderstanding? If I'm not, you can't do what you want that way. Or is your javascript file running through the PHP parser on the way to the client? In which case there's probably a simpler way to do what you want. – Gareth May 10 '13 at 13:53
  • so i can't load a php variable into a javascript variable? – Laurent May 10 '13 at 13:54
  • No, PHP runs on the server and javascript runs on the client. They don't share any data. – Gareth May 10 '13 at 13:55
  • @Gareth: I assume he's generating the Javascript code from PHP, if that's the case it'll work. – Alix Axel May 10 '13 at 13:56
  • @Gareth: you can output PHP data in a JavaScript script. – Marcel Korpel May 10 '13 at 13:56
  • @Alix Axel did however manage to share the data... – Laurent May 10 '13 at 13:57
  • @MarcelKorpel: True, but I think in this case it's the other way around. – Alix Axel May 10 '13 at 13:57
  • @AlixAxel I know you can, but generating javascript with PHP? That's a very complicated way of doing anything. – Gareth May 10 '13 at 13:58
  • @Laurent Alix's solution sent the data from the server to the client. Like I said, if you're running the javascript file through the PHP parser on the way to the client there's probably a simpler solution to your problem. – Gareth May 10 '13 at 14:00

2 Answers2

3

Because data1 is empty (PHP is not outputing anything), try:

var data1 = "<?= http_build_query($_GET); ?>"; // or
var data1 = "<?php echo http_build_query($_GET); ?>";

Any reason you're using PHP to build the query string instead of doing it directly in Javascript?

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
1

You can also archive it the plain old pure javascript way:

var data1 = location.href.split('?').pop();
var data2 = "buy.php?";
var url = data2+data1
document.getElementById('framebox').src = url;

But it's much more fun to mix stuff.....

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123