简体   繁体   English

如何在php内调用函数形式javascript?

[英]how to call function form javascript inside php?

hi im new to php and im wonder if there any way to use javascript inside php code then i want to know how to call that function in javascript inside php code , really im sorry for my bad english and i hope that make my problem clear 嗨,我是php新手,我想知道是否有任何方法可以在php代码内使用javascript,然后我想知道如何在php代码内的javascript中调用该函数,真的很抱歉,我的英语不好,我希望能使我的问题更清楚

<?php
    ob_start();
    require 'connect.inc.php';

    session_start();
    $usrequest = $_REQUEST['usr'];
    $pwdrequest = $_REQUEST['pwd'];

    $id = 'msg';

    echo '<script type="text/javascript" language = "javascript">

        function send ()
        {
            document.getElementById('msg').innerHTML = "asdsd";
        }
    </script>';    
?>

1) If your question is "Can I echo javascript code using the php echo function?" 1)如果您的问题是“我可以使用php echo函数回显javascript代码吗?” ,

you have not escaped the string to be echoed. 您尚未转义要回显的字符串。 Change it as follows: 进行如下更改:

<?php
    ob_start();
    require 'connect.inc.php';

    session_start();
    $usrequest = $_REQUEST['usr'];
    $pwdrequest = $_REQUEST['pwd'];

    $id = 'msg';

    echo '<script type="text/javascript" language = "javascript">

        function send ()
        {
            document.getElementById(\'msg\').innerHTML = "asdsd";
        }
    </script>';    
?>

2) If your question is "Can I call a javascript function from php (from the server)?" 2)如果您的问题是“我可以从php(从服务器)调用javascript函数吗?”

you cannot. 你不能。 PHP executes in the server and sends the resultant page to the client (browser). PHP在服务器中执行,并将结果页面发送到客户端(浏览器)。 Then, in your browser, the javascript code is executed. 然后,在您的浏览器中,执行javascript代码。

在此处输入图片说明

You can't. 你不能

PHP runs on the server side before the page is downloaded by the client. 在客户端下载页面之前,PHP在服务器端运行。 JavaScript runs on the client side after the page has been downloaded by the client. 客户端下载页面后,JavaScript将在客户端上运行。

Therefore it is impossible for PHP to call a JavaScript function. 因此,PHP无法调用JavaScript函数。 It is possible to write JavaScript with PHP as example your code shows but not to execute it. 可以用PHP编写JavaScript作为示例,但是代码却不能执行。

What you're doing would work except that you're using single quotes inside single quotes. 除了在单引号内使用单引号之外,您正在执行的操作将起作用。 Change it to: 更改为:

document.getElementById("msg").innerHTML = "asdsd";

It's a different matter, of course, get a return value back from JavaScript to the PHP script, as the PHP script has finished running before the JavaScript one starts. 这是一个不同的问题,当然,得到的JavaScript和PHP脚本返回值 ,作为PHP脚本有JavaScript的一个开始之前完成运行。

Writing JavaScript in PHP is nothing but writing JavaScript for the DOM. 用PHP编写JavaScript就是为DOM编写JavaScript。 You can write it in PHP. 您可以用PHP编写它。 use within PHP or directly. 在PHP中使用或直接使用。

Example: 例:

<?php
echo "
    <script type=\"text/javascript\">
        function greeting(name){
            alert(name);
        }
    </script>
";
?>
<script type="text/javascript">
greeting('srihari');
</script>

I hope that this will help you. 希望对您有帮助。

And don't use single quotes within single quotes, 而且不要在单引号内使用单引号,

You can simply call that function and this will run after PHP code will send to your browser but it will give you result as you want 您可以简单地调用该函数,该函数将在PHP代码发送到您的浏览器后运行,但可以根据需要提供结果

You can use this 你可以用这个

<html>
<head>
    <title></title>
<?php
ob_start();
require 'connect.inc.php';

session_start();
$usrequest = $_REQUEST['usr'];
$pwdrequest = $_REQUEST['pwd'];

$id = 'msg';

echo '<script type="text/javascript" language = "javascript">

    function send ()
    {
        document.getElementById("msg").innerHTML = "asdsd";
    }
</script>';
echo '<script type="text/javascript">
        document.addEventListener("DOMContentLoaded",function(){
            document.getElementById("msg").innerHTML = "asdsd";
        });
    </script>';
?>
</head>
<body>
    <div id="msg"></div>
</body>

有一些尝试使PHP可以使用Google的V8 javascript引擎,例如https://github.com/preillyme/v8js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM