单击按钮时运行 php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32824360/
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
Run php function on button click
提问by JD_bravo
I want to run a php function on button click. for eg :
我想在单击按钮时运行一个 php 函数。例如:
<input type="button" name="test" id="test" value="RUN" onclick="<?php echo testfun(); ?>" /><br/>
<?php
function testfun()
{
echo "Your test function on button click is working";
}
?>
My question is that when I do this I don't get the expected output I was looking for. Please give me the best solution for this to run a php function on button click whether it is a simple button
or submit
.
我的问题是,当我这样做时,我没有得到我正在寻找的预期输出。请给我最好的解决方案,以便在按钮单击时运行 php 函数,无论它是简单的button
还是submit
.
回答by JD_bravo
I tried the code of William, Thanks brother.
我尝试了威廉的代码,谢谢兄弟。
but it's not working as a simple button I have to add form with method="post". Also I have to write submit instead of button.
但它不是一个简单的按钮,我必须添加带有 method="post" 的表单。另外我必须写提交而不是按钮。
here is my code below..
下面是我的代码..
<form method="post">
<input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
<?php
function testfun()
{
echo "Your test function on button click is working";
}
if(array_key_exists('test',$_POST)){
testfun();
}
?>
回答by William Madede
Do this:
做这个:
<input type="button" name="test" id="test" value="RUN" /><br/>
<?php
function testfun()
{
echo "Your test function on button click is working";
}
if(array_key_exists('test',$_POST)){
testfun();
}
?>
回答by Florian Lauterbach
You are trying to call a javascript function. If you want to call a PHP function, you have to use for example a form:
您正在尝试调用 javascript 函数。如果你想调用一个 PHP 函数,你必须使用例如一个表单:
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
(Original Code from: http://www.w3schools.com/html/html_forms.asp)
(原始代码来自:http: //www.w3schools.com/html/html_forms.asp)
So if you want do do a asynchron call, you could use 'Ajax' - and yeah, that's the Javascript-Way. But I think, that my code example is enough for this time :)
所以如果你想做一个异步调用,你可以使用'Ajax' - 是的,这就是Javascript-Way。但我认为,这次我的代码示例已经足够了:)
回答by Ivan Abadzhiev
<a href="home.php?click=1" class="btn">Click me</a>
<?php
if($_GET['click']){
doSomething();
}
?>
But is better to use JS and with ajax to call function!
但是最好使用JS和ajax来调用函数!
回答by Rahul Gite
No Problem You can use onClick()
function easily without using any other interference of language,
没问题您可以onClick()
轻松使用功能,而无需使用任何其他语言干扰,
<?php
echo '<br><Button onclick="document.getElementById(';?>'modal-wrapper2'<?php echo ').style.display=';?>'block'<?php echo '" name="comment" style="width:100px; color: white;background-color: black;border-radius: 10px; padding: 4px;">Show</button>';
?>
回答by deepak mishra
You can use isset()
.
您可以使用isset()
.
<form method="post">
<input type="submit" name="test" id="test" value="RUN" />
</form>
<?php
function testfun()
{
echo "Your test function on button click is working";
}
if(isset($_POST('submit')))
{
testfun();
}
?>