如何在 javascript 函数中使用 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8471945/
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
How can you use php in a javascript function
提问by fosho
<html>
<?php
$num = 1;
echo $num;
?>
<input type="button"
name="lol"
value="Click to increment"
onclick="Inc()" />
<br>
<script>
function Inc()
{
<?php
$num = 2;
echo $num;
?>
}
</script>
</html>
This is what i have so far, not working though, i think i need to use ajax or something but i have no idea what to do.
这是我到目前为止所拥有的,虽然没有工作,但我想我需要使用 ajax 或其他东西,但我不知道该怎么做。
Thanks
谢谢
回答by Andreas Eriksson
You can't run PHP code with Javascript. When the user recieves the page, the server will have evaluated and run all PHP code, and taken it out. So for example, this will work:
您不能使用 Javascript 运行 PHP 代码。当用户收到页面时,服务器将评估并运行所有 PHP 代码,并将其取出。例如,这将起作用:
alert( <?php echo "\"Hello\""; ?> );
Because server will have evaluated it to this:
因为服务器会对它进行评估:
alert("Hello");
However, you can't perform any operations in PHP with it.
但是,您不能使用它在 PHP 中执行任何操作。
This:
这个:
function Inc()
{
<?php
$num = 2;
echo $num;
?>
}
Will simply have been evaluated to this:
将简单地评估为:
function Inc()
{
2
}
If you wan't to call a PHP script, you'll have to call a different page which returns a value from a set of parameters.
如果不想调用 PHP 脚本,则必须调用不同的页面,该页面从一组参数中返回一个值。
This, for example, will work:
例如,这将起作用:
script.php
脚本文件
$num = $_POST["num"];
echo $num * 2;
Javascript(jQuery) (on another page):
Javascript(jQuery)(在另一个页面上):
$.post('script.php', { num: 5 }, function(result) {
alert(result);
});
This should alert 10.
这应该提醒 10。
Good luck!
祝你好运!
Edit: Just incrementing a number on the page can be done easily in jQuery like this: http://jsfiddle.net/puVPc/
编辑:只需在页面上增加一个数字就可以在 jQuery 中轻松完成,如下所示:http: //jsfiddle.net/puVPc/
回答by Vigrond
I think you're confusing server code with client code.
我认为您将服务器代码与客户端代码混淆了。
JavaScript runs on the client after it has received data from the server (like a webpage).
JavaScript 在从服务器(如网页)接收数据后在客户端上运行。
PHP runs on the server before it sends the data.
PHP 在发送数据之前在服务器上运行。
So there are two ways with interacting with JavaScript with php.
因此,有两种方式可以通过 php 与 JavaScript 交互。
Like above, you can generate javascript with php in the same fashion you generate HTML with php.
像上面一样,您可以使用 php 生成 javascript,就像使用 php 生成 HTML 一样。
Or you can use an AJAX request from javascript to interact with the server. The server can respond with data and the javascript can receive that and do something with it.
或者您可以使用来自 javascript 的 AJAX 请求与服务器交互。服务器可以用数据响应,javascript 可以接收它并用它做一些事情。
I'd recommend going back to the basics and studying how HTTP works in the server-client relationship. Then study the concept of server side languages and client side languages.
我建议回归基础并研究 HTTP 在服务器-客户端关系中的工作方式。然后研究服务器端语言和客户端语言的概念。
Then take a tutorial with ajax, and you will start getting the concept.
然后学习 ajax 教程,您将开始了解概念。
Good luck, google is your friend.
祝你好运,谷歌是你的朋友。
回答by dunno
you use script in php..
你在php中使用脚本..
<?php
$num = 1;
echo $num;
echo '<input type="button"
name="lol"
value="Click to increment"
onclick="Inc()" />
<br>
<script>
function Inc()
{';
$num = 2;
echo $num;
echo '}
</script>';
?>
回答by MEX
Simply, your question sounded wrong because the JavaScript variables need to be echoed.
简单地说,您的问题听起来是错误的,因为需要回显 JavaScript 变量。
<?php
$num = 1;
echo $num;
echo "<input type='button' value='Click' onclick='readmore()' />";
echo "<script> function readmore() { document.write('";
$num = 2;
echo $num;
echo "'); } </script>";
?>
回答by metatron
There is a way to run php in client-side javascript (not talking about server-side js here). Don't know if this is a very good idea, but you can. As some people pointed out you have to keep in mind the php is evaluated on the server and then returned as static stuff to the browser who will then run the javascript with the added data from the server.
有一种方法可以在客户端 javascript 中运行 php(这里不讨论服务器端 js)。不知道这是否是一个很好的主意,但你可以。正如某些人指出的那样,您必须记住,php 在服务器上进行评估,然后作为静态内容返回给浏览器,浏览器将使用来自服务器的添加数据运行 javascript。
You have to tell the server to evaluate the js files as php files, if you are running an apache server you can do this with a htaccess-file like this:
您必须告诉服务器将 js 文件评估为 php 文件,如果您正在运行 apache 服务器,则可以使用 htaccess 文件执行此操作,如下所示:
<FilesMatch "\.js$">
SetHandler application/x-httpd-php
Header set Content-type "application/javascript"
</FilesMatch>
More info here:
更多信息在这里:
Parse js/css as a PHP file using htaccess
使用 htaccess 将 js/css 解析为 PHP 文件
http://css-tricks.com/snippets/htaccess/use-php-inside-javascript/
http://css-tricks.com/snippets/htaccess/use-php-inside-javascript/
Edit: On http page request this trick makes files with js extension be parsed by the php compiler. All php parts in the js file will get executed and the js file with added server data will be handed to the browser.
编辑:在 http 页面请求上,这个技巧使带有 js 扩展名的文件被 php 编译器解析。js 文件中的所有 php 部分都将被执行,并且添加了服务器数据的 js 文件将被传递给浏览器。
However, the OP uses an html formated file (probably with php extension), no js file, so in this specific case there's no need for my suggestion.
但是,OP 使用 html 格式的文件(可能带有 php 扩展名),没有 js 文件,因此在这种特定情况下不需要我的建议。
The suggested js solutions are the way to go. If the variable needs to get stored on the server, use ajax to send it there.
建议的 js 解决方案是要走的路。如果变量需要存储在服务器上,请使用 ajax 将其发送到那里。
回答by Vid
In the above given code
在上面给出的代码中
assign the php value to javascript variable.
将 php 值分配给 javascript 变量。
<html>
<?php
$num = 1;
echo $num;
?>
<input type = "button" name = "lol" value = "Click to increment" onclick = "Inc()">
<br>
<script>
var numeric = <?php echo $num; ?>"; //assigns value of the $num to javascript var numeric
function Inc()
{
numeric = eVal(numeric) + 1;
alert("Increamented value: "+numeric);
}
</script>
</html>
One thing in combination of PHP and Javsacript is you can not assign javascript value to PHP value. You can assign PHP value to javascript variable.
PHP 和 Javsacript 组合的一件事是您不能将 javascript 值分配给 PHP 值。您可以将 PHP 值分配给 javascript 变量。
回答by Jagadeesan
Try this may work..
试试这可能有用..
<html>
<?php $num = 1; ?>
<div id="Count"><?php echo $num; ?></div>
<input type = "button" name = "lol" value = "Click to increment" onclick = "Inc()">
<br>
<script>
function Inc() {
i = parseInt(document.getElementById('Count').innerHTML);
document.getElementById('Count').innerHTML = i+1;
}
</script>
</html>
回答by Daniel Loureiro
despite some comments, at some cases it's really necessary to access PHP functions at Javascript (e.g. the AJAX cases).
尽管有一些评论,但在某些情况下确实需要在 Javascript 中访问 PHP 函数(例如 AJAX 情况)。
At these cases you can use the mwsX library to use your PHP functions at Javascript.
在这些情况下,您可以使用 mwsX 库在 Javascript 中使用您的 PHP 函数。
mwsX library: https://github.com/loureirorg/mwsx
mwsX 库:https: //github.com/loureirorg/mwsx
回答by user6387858
Simply return it:
只需返回它:
<html>
<?php
$num = 1;
echo $num;
?>
<input type="button"
name="lol"
value="Click to increment"
onclick="Inc()" />
<br>
<script>
function Inc()
{
Return <?php
$num = 2;
echo $num;
?>;
}
</script>
</html>