通过 onclick 事件调用 php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7451070/
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
Calling a php function by onclick event
提问by Zak
I am trying to call a function by "onclick" event of the button. When I do that it shows error message. Can anybody help me out on this so that when I click on the button it should call the function and execute it.
我试图通过按钮的“onclick”事件调用一个函数。当我这样做时,它会显示错误消息。任何人都可以帮助我解决这个问题,以便当我点击按钮时它应该调用该函数并执行它。
My php code is:
我的php代码是:
<?php
function hello() {
echo "Hello";
}
echo "<input type='button' name='Release' onclick= hello(); value='Click to Release'>";
?>
What is wrong with this code?
这段代码有什么问题?
回答by timpanix
The onClick attribute of html tags only takes Javascript but not PHP code. However, you can easily call a PHP function from within the Javascript code by using the JS document.write() function - effectively calling the PHP function by "writing" its call to the browser window: Eg.
html 标签的 onClick 属性只需要 Javascript 而不是 PHP 代码。但是,您可以使用 JS document.write() 函数从 Javascript 代码中轻松调用 PHP 函数 - 通过“写入”对浏览器窗口的调用来有效调用 PHP 函数:
onclick="document.write('<?php //call a PHP function here ?>');"
Your example:
你的例子:
<?php
function hello(){
echo "Hello";
}
?>
<input type="button" name="Release" onclick="document.write('<?php hello() ?>');" value="Click to Release">
回答by Zak
First quote your javascript:
首先引用你的javascript:
onclick="hello();"
Also you can't call a php function from javascript; you need:
你也不能从 javascript 调用 php 函数;你需要:
<script type="text/javascript">
function hello()
{
alert ("hello");
}
</script>
回答by Anuj
onclick event to call a function
onclick 事件调用函数
<strike> <input type="button" value="NEXT" onclick="document.write('<?php //call a function here ex- 'fun();' ?>');" /> </strike>
it will surely help you
它肯定会帮助你
it take a little more time than normal but wait it will work
它需要比平时多一点时间,但等待它会起作用
回答by Ali Raza
In Your HTML
在你的 HTML 中
<input type="button" name="Release" onclick="hello();" value="Click to Release" />
In Your JavaScript
在你的 JavaScript 中
<script type="text/javascript">
function hello(){
alert('Your message here');
}
</script>
If you need to run PHP in JavaScript You need to use JQuery Ajax Function
如果你需要在 JavaScript 中运行 PHP 你需要使用 JQuery Ajax 函数
<script type="text/javascript">
function hello(){
$.ajax(
{
type: 'post',
url: 'folder/my_php_file.php',
data: '&id=' + $('#id').val() + '&name=' + $('#name').val(),
dataType: 'json',
//alert(data);
success: function(data)
{
//alert(data);
}
});
}
</script>
Now in your my_php_file.php file
现在在你的 my_php_file.php 文件中
<?php
echo 'hello';
?>
Good Luck !!!!!
祝你好运 !!!!!
回答by Shashank Raj Chavan
Executing PHP functions by the onclick event is a cumbersome task and near impossible.
通过 onclick 事件执行 PHP 函数是一项繁琐且几乎不可能的任务。
Instead you can redirect to another PHP page.
相反,您可以重定向到另一个 PHP 页面。
Say you are currently on a page one.php and you want to fetch some data from this php script process the data and show it in another page i.e. two.php you can do it by writing the following code
<button onclick="window.location.href='two.php'">Click me</button>
假设您当前在页面 one.php 上,并且您想从这个 php 脚本中获取一些数据处理数据并将其显示在另一个页面中,即 two.php 您可以通过编写以下代码来完成
<button onclick="window.location.href='two.php'">Click me</button>
回答by Anuj
Use this html code it will surely help you
使用此 html 代码,它肯定会对您有所帮助
<input type="button" value="NEXT" onclick="document.write('<?php //call a function here ex- 'fun();' ?>');" />
one limitation is that it is taking more time to run so wait for few seconds it will work
一个限制是它需要更多的时间来运行所以等待几秒钟它会起作用
回答by AaronL
probably the onclick
handler should read onclick='hello();'
instead of onclick=hello();
onclick
处理程序可能应该读取onclick='hello();'
而不是onclick=hello();
回答by Byron Whitlock
You cannot execute php functions from JavaScript.
您不能从 JavaScript 执行 php 函数。
PHP runs on the server before the browser sees it. PHP outputs HTML and JavaScript.
PHP 在浏览器看到之前在服务器上运行。PHP 输出 HTML 和 JavaScript。
When the browser reads the html and javascript it executes it.
当浏览器读取 html 和 javascript 时,它会执行它。