Safira
Safira

Reputation: 275

Cannot get value of php variable in accordance with the value of js variable

I have this html file that tries to call script2.php when the button is clicked:

<div id="wrapper">
        <a id="test" href="try.html" onclick="return false;">
            <input type="submit" id="gen" value="Generate!" onclick="dothis();"><br />
        </a>
        <script type="text/javascript">
            function dothis() {
                var url = document.getElementById("test").valueOf('href');
                alert(url);
                $("#quote p").load("script2.php");
            }
        </script>

        <div id="quote"><p></p></div>
    </div>

The script2.php has this in it, in which I try to get a text file from try.html:

<?php
  header("Cache-Control: no-cache");
  $file = 'test_put.txt';
  $url = "try.html";
  $current = file_get_contents($url);
  $current2 = strip_tags($current);
  file_put_contents($file, $current2);
  echo "Thanks for your response!";
?>

You see, the php file doesn't get the value of $url directly from the value of var url inside the javascript thing. While what I want is the $url value can vary in accordance with the var url.

I'd tried:

$url = "<script>document.write(url)</script>";

And this:

$url = "<script language ='javascript'>
          var url = document.getElementById('test').valueOf('href');
           document.write(url);
       </script>";

Then it twice said, failed to open stream.

I've googled and searched in SOF as well to address this problem, all I could find was the server-side and client-side thing that make this won't work simply. I found this too, but since I'm not doing such form I can't extract that solution into my problem.

Thanks..

Upvotes: 1

Views: 175

Answers (1)

McGarnagle
McGarnagle

Reputation: 102793

I think you want something like this:

$("#quote p").load("script2.php?url=" + url);

That passes the URL in the query string. Then retrieve it in "script2.php":

$url = $_GET["url"];

Upvotes: 2

Related Questions