javascript 从 document.ready 调用 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9234570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:01:05  来源:igfitidea点击:

Calling PHP from document.ready

phpjavascriptdocument-ready

提问by user823527

Is there a way in document ready to call php scripts?

文档中有没有准备好调用 php 脚本的方法?

I want to do something like this.

我想做这样的事情。

<script>
    $(document).ready(function($) {

    <?php require("readenglish.php"); ?>
    <?php require("readfrench.php"); ?>
    <?php require("readspanish.php"); ?>
    <?php 

    $opload = $_GET['opload'];
    if ($opload == "reade") {

    }
    else if ($opload == "readf") {

       echo "<script type=\"text/javascript\">\n";

       echo "document.f1.r1[0].checked = false;\n";
       echo "document.f1.r1[1].checked = true;\n";
       echo "SelectRead();\n";

       echo "</script>";

    }

    ?>

    });

</script>

The three php scripts create divs and add information to them from an external domain php script.

这三个 php 脚本创建 div 并向它们添加来自外部域 php 脚本的信息。

回答by Kai Qing

You're mixing client and server side with no interface there. You're going to have to use $.ajaxto pull in the content from the php scripts and update your DOM accordingly.

您正在混合客户端和服务器端,那里没有接口。您将不得不使用$.ajax从 php 脚本中提取内容并相应地更新您的 DOM。

Of course... you could also design your site in a way that makes sense. This method looks suspicious at best and almost positively has a much better, exclusively server side solution.

当然...您也可以以有意义的方式设计您的网站。这种方法充其量看起来很可疑,并且几乎肯定有一个更好的、专门的服务器端解决方案。

回答by Ryan

You could do something like this in your page:

你可以在你的页面中做这样的事情:

$(document).ready(function($) {
   $('#result').load('test.php', function() {
    alert('Load was performed.');
  });
});

Where, on the server, test.phpcontains your code:

在服务器上,哪里test.php包含您的代码:

<?php require("readenglish.php"); ?>
<?php require("readfrench.php"); ?>
<?php require("readspanish.php"); ?>
<?php 

$opload = $_GET['opload'];
if ($opload == "reade") {

}
else if ($opload == "readf") {

   echo "<script type=\"text/javascript\">\n";

   echo "document.f1.r1[0].checked = false;\n";
   echo "document.f1.r1[1].checked = true;\n";
   echo "SelectRead();\n";

   echo "</script>";

}

This then needs to be added back into the DOM on the client side. Not good design though.

然后需要将其添加回客户端的 DOM。虽然不是很好的设计。

回答by Francis Lewis

I would recommend using jQuery. They have a load method that does exactly what you're asking: http://api.jquery.com/load/

我会推荐使用jQuery。他们有一个加载方法,可以完全满足您的要求:http: //api.jquery.com/load/