0

I want to use my javascript result to set a php variable.

<script>
var hello;
hello = "ilovetuna.php";
</script>

and

 <?php
    $variable = "<script>document.write(hello);</script>";
?>

and

<?php
require('simple_html_dom.php');
$html = file_get_html($variable);
echo $html;
?>

Warning: file_get_contents(document.write(hello);): failed to open stream

Does someone know how to deal with this? Thank you

ps: i tried to use \ string before " and also tried different ' and ", also ( and ) . but doesnt seem to work.

assayag.org
  • 709
  • 10
  • 24

1 Answers1

0

You need to pass your javascript on to the php script, you can do this with jquery's ajax

api.jquery.com/jQuery.ajax

    var hello;
    hello = "ilovetuna.php";

    $.ajax({  
      type: "POST",  
      url: "yourphpscript.php",  
      data: hello,  
      success: function() {  
        alert("it works");
      }  
    });  

In your PHP retrieve the variable with $_POST['data'];.

<?php
    $variable = $_POST['data'];
?>


<?php
    require('simple_html_dom.php');
    $html = file_get_html($variable);
    echo $html;
?>
Melbourne2991
  • 11,707
  • 12
  • 44
  • 82
  • 1
    why do people keep posting answers in jQuery even if the sample code in the question is in core javascript? – Kevin Jun 17 '13 at 18:49
  • Ajax in pure javascript is a nightmare, with jQuery its just a few lines of code, and jQuery is javascript anyway. – Melbourne2991 Jun 17 '13 at 18:53
  • The question has nothing to do with Ajax. I've seen many answers like this that give the answers in jQuery even though the user had used pure JS. Keep in mind that users who don't know jQuery will be very much confused with such answers. So, atleast for simple solutions, try using core javascript. – Kevin Jun 17 '13 at 18:59
  • I get what you're saying, but as the other users commented on this, there is no real solution to this, you can't mix server side and client side, ajax is the closest thing to that, and for someone who doesn't know about javascript being client side, I think they would struggle writing ajax calls in vanilla javascript – Melbourne2991 Jun 17 '13 at 19:04
  • thank you Melbourne2991. I get the alert but no retrieve of hello data. Notice: Undefined index: data in C:\Users\cfmj\Desktop\web\search\broogle2.php on line 35 – assayag.org Jun 17 '13 at 19:26