javascript 混合javascript和php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7868474/
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 01:34:29  来源:igfitidea点击:

mix javascript and php

phpjavascriptjquery

提问by Gryz Oshea

I have a file called: file.php

我有一个文件叫:file.php

contained in this file is the following code:

此文件中包含以下代码:

<script type="text/javascript">
        //....
        var sumJS = 10;
    <?php $sumPHP = sumJS  ?>
</script>
    <?php echo "Sum = " . $sumPHP ?>

How can I assign sumJS for $sumPHP?

如何为 $sumPHP 分配 sumJS?

If I would like to make this conversely then I would write:

如果我想反过来做,那么我会写:

  $sumPHP = 10;
<script type="text/javascript">
    var sumJS;
    sumJS = <?php echo $sumPHP ?>;
    alert(sumJS);
</script>

But how can I re-make this for my problem?

但是我怎样才能为我的问题重新制作呢?

回答by rauprog

Javascript can change the variables of PHP and vice versa.

Javascript 可以改变 PHP 的变量,反之亦然。

Here is an example of that.

这是一个例子。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php $aphp=1 ?>

<script "text/javascript">document.write("<?php $aphp=2 ?>")
var ajava=1;</script>


<?php echo "aphp= $aphp"; echo "<br>";
echo "<script \"text/javascript\">ajava=2;</script>"
?>
<script "text/javascript">document.write("ajava= "); document.write(ajava)</script>
</body>
</html>

What if I want to assign a php variable to a java variable and vice versaThe above showed that you can assign values to java in php and vice versa by simply writing out an assignment using echo in php and document.write in java. That is how you assign a value.

如果我想将一个 php 变量赋值给一个 java 变量,反之亦然上面表明您可以在 php 中为 java 赋值,反之亦然,只需使用 php 中的 echo 和 java 中的 document.write 写出一个赋值。这就是您分配值的方式。

Mixing actual variables between php and java is not much more complicated. What you do is write an assignment in the language you need to get the value of a variable from and assign it to a variable in the other language. Here is an example of that.

在 php 和 java 之间混合实际变量并不复杂。你所做的是用你需要的语言编写一个赋值,从中获取变量的值并将其分配给其他语言的变量。这是一个例子。

Assign php to java

将 php 分配给 java

<?php echo "<script type='text/javascript'>javasum=$phpsum</script>" ?>

assign java to php can be done in two ways

可以通过两种方式将 java 分配给 php

<script type='text/javascript'>var str="<?php $phpsum=";
str=str+javasum; str=str+" ?>";
document.write(str);
</script>

or more conveniently

或者更方便

javasum=<?php echo $phpsum ?>;

Mixing php and javascript to do things other than variable assignments

混合 php 和 javascript 来做变量赋值以外的事情

Javascript can also be mixed in the phpcode to do a javascript function in the phpcode. Say you wanted to use javascript to highlight text only if a condition is met in the php code. But to do this you need to echo the javascript code section. Also use the escape character, \' to include quotes in a echo command. Here is an example of this

也可以在phpcode中混入javascript,在phpcode中做一个javascript的功能。假设您想使用 javascript 仅在 php 代码中满足条件时突出显示文本。但要做到这一点,您需要回显 javascript 代码部分。还可以使用转义字符 \' 在 echo 命令中包含引号。这是一个例子

Before you get started be sure php content type is in html. In dreamweaver site editor software, a header to do this is automatically included.

在开始之前,请确保 php 内容类型是 html。在 Dreamweaver 站点编辑器软件中,会自动包含执行此操作的标题。

    <p><span id="htext">htext</span></p>


  <?php 
$a=5;
echo "<h3> hello world</h3>";

if ($a==5) {
echo "<script type=\"text/javascript\">";
echo "document.getElementById(\"htext\").style=\"background-color:yellow\"";
echo "</script>";
}
?>
<p>is a=5</p>
<p><?php echo $a ?>&nbsp; </p>

回答by genesis

You're just missing tags

你只是缺少标签

<?php
$sumPHP = 10;
?>
<script type="text/javascript">
var sumJS;
sumJS = <?php echo $sumPHP ?>;
alert(sumJS);
</script>

demo

演示

回答by lya

$sumPHP is a server-side value, which will be evaluated on the server. Then the server generates page content and sent it to the client. Then browsers receive the page content and evaluate client-side value sumJS.

$sumPHP 是服务器端的值,它将在服务器上进行评估。然后服务器生成页面内容并发送给客户端。然后浏览器接收页面内容并评估客户端值 sumJS。

So you can never assign sumJS for $sumPHP because $sumPHP is calculated before sumJS.

所以你永远不能为 $sumPHP 分配 sumJS,因为 $sumPHP 是在 sumJS 之前计算的。

But can do this to show the page:

但是可以这样做来显示页面:

<?php echo "Sum = "?><script type="text/javascript">document.write(sumJS);</script>

or rather

更确切地说

<script type="text/javascript">document.write('Sum = ' + sumJS);</script>

回答by u4864450

Sorry that at the moment I do not have the time to write an example. But I believe that this idea will work. Maybe someone here can do a quick test.

抱歉,目前我没有时间写一个例子。但我相信这个想法会奏效。也许这里有人可以做一个快速测试。

Just create a hidden element with a unique id, say a span with id="uniqueBufferForCommunicatingBetweenPHPandJS" and style="display:none". Then, with the JS part of the code, write the result into this span by using

只需创建一个具有唯一 id 的隐藏元素,比如 id="uniqueBufferForCommunicatingBetweenPHPandJS" 和 style="display:none" 的跨度。然后,用代码的JS部分,通过使用将结果写入这个span

document.getElementById("uniqueBufferForCommunicationBetweenPHPandJS").innerHTML = XXX;

document.getElementById("uniqueBufferForCommunicationBetweenPHPandJS").innerHTML = XXX;

You can even write XXX in the form of JSON.

你甚至可以将 XXX 写成 JSON 的形式。

Afterwards, you can use something in PHP like

之后,您可以在 PHP 中使用类似的东西

$bufferspan = $dom->getElementById('uniqueBufferForCommunicationBetweenPHPandJS');

$bufferspan = $dom->getElementById('uniqueBufferForCommunicationBetweenPHPandJS');

to get the span, and read its content, probably by using

获取跨度并阅读其内容,可能是使用

$avariable = $dom->ownerDocument->saveHTML($avariable);

$avariable = $dom->ownerDocument->saveHTML($avariable);

and then interpret the result stored in $avariable. I am not totally sure about the last PHP code.

然后解释存储在 $avariable 中的结果。我不完全确定最后的 PHP 代码。