【问题标题】:Why I can't get javascript values into my php5 code? [duplicate]为什么我不能在我的 php5 代码中获取 javascript 值? [复制]
【发布时间】:2017-12-27 10:15:21
【问题描述】:

我有这个代码:

<?php       $html=file_get_contents('testmaker_html.html');
        echo $html;
?>



<script type="text/javascript">


    document.getElementById('save_finaly_TEST').addEventListener("click", function(){
        cover = document.getElementById('cover').value;
        keywords = document.getElementById('keywords').value.split(",");

        notificationAboutElement = "Ok!";
        notifyMe(notificationAboutElement);
      html_saver();

    <?php $test_html = "<script>document.write(html)</script>"?>   


    }); 

</script>
<?php 
        $new_test = rand().".html";
        $myfile = fopen($new_test, "w") or die("Unable to create file!");
        write($myfile, $test_html)  or die("Can't write to file");
        fclose($myfile)  or die("Can't close the file!");
        echo $new_test;
?>


<script type="text/javascript">
    console.log("$test_html: " + <?php echo $test_html; ?>);
    console.log("$new_test: " + <?php echo $new_test; ?>);
</script> 

为什么$test_html 是空的?我知道 php 和 javascript 不在同一台服务器上,但是这种方法可以多次获取值,对很多人都有效。这有什么问题?

【问题讨论】:

  • PHP 和 JS 在不同的时间运行。你需要重新考虑这一点。
  • 我看不出这对任何人都有效。 JS 在发送到客户端之前不会运行。这对 PHP 来说只是毫无意义的文本。
  • 您让工厂(服务器/PHP)为您制造了一辆汽车,并将其运送到您家门口。现在在您的车道(客户)上,您在挡风玻璃上贴了一个红色贴纸……并期望工厂自动知道这一点?不,这当然行不通。您需要将此信息发送到工厂 - 这意味着,您需要直接(提交表单,加载带有 URL 参数的新页面)或在后台通过 AJAX。
  • 因为 php 先运行所以这行得通? :&lt;script&gt; var jsvar = &lt;?php echo json_encode($PHPVar); ?&gt;; &lt;/script&gt;
  • @KronologiaitVme 其中的 PHP 部分将运行,它会回显 Javascript 代码,但 Javascript 在发送到客户端之前不会真正运行。

标签: javascript php php-5.2


【解决方案1】:

试试这个

您必须使用fwrite 而不是write 仅用于文件写入

<?php       
        $html=file_get_contents('testmaker_html.html');
        echo $html;
?>
<script type="text/javascript">
    document.getElementById('save_finaly_TEST').addEventListener("click", function(){
        cover = document.getElementById('cover').value;
        keywords = document.getElementById('keywords').value.split(",");

        notificationAboutElement = "Ok!";
        notifyMe(notificationAboutElement);
      html_saver();

    <?php $test_html = "<script>document.write(html)</script>"?>   
    }); 
</script>
<?php 
        $new_test = rand().".html";
        $myfile = fopen($new_test, "w") or die("Unable to create file!");
        fwrite($myfile, $test_html)  or die("Can't write to file");
        fclose($myfile)  or die("Can't close the file!");
        echo $new_test;
?>
<script type="text/javascript">
    console.log("$test_html: " + <?php echo $test_html; ?>);
    console.log("$new_test: " + <?php echo $new_test; ?>);
</script> 

【讨论】:

  • 这行不通,因为 $new_test 不会被引用
  • 谢谢!我试过这个,但在$new_test文件里面只有&lt;script&gt;document.write(html)&lt;/script&gt;我怎样才能把htmljs变量的值放入$test_html
  • 那么你想做什么解释一下
  • 我在 js 中有一个 html 变量,我想用这个 html 变量在文件中创建一个带有 php 的文件。我该怎么做?
【解决方案2】:

PHP 在服务器上运行,Javascript 在客户端上运行。所以当Javascript运行时,PHP已经运行了。

<?php 
    $new_test = rand().".html";
    $myfile = fopen($new_test, "w") or die("Unable to create file!");
    fwrite($myfile, $test_html)  or die("Can't write to file");
    fclose($myfile)  or die("Can't close the file!");
    echo $new_test;
?>
<script>
    var new_test = "<?php echo $new_test; ?>";

    console.log("$new_test: " + new_test);
</script>

在这里,您将获取文件的内容,并对其进行 JSON 编码,以便将其作为有效的 Javascript 对象发送到客户端。查看view source 了解我的意思。

其他情况:

  • 如果您希望客户端向服务器提交内容,请创建一个 POST API
  • 如果您只想在页面上显示来自 Javascript 的内容,请使用 Javascript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多