dousi2029 2013-07-13 04:15
浏览 58
已采纳

无法根据js变量的值获取php变量的值

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..

  • 写回答

1条回答 默认 最新

  • duandie5707 2013-07-13 04:28
    关注

    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"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算