单击按钮运行php函数 [英] Run php function on button click

查看:89
本文介绍了单击按钮运行php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在单击按钮时运行php函数.例如:

I want to run a php function on button click. for eg :

<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";
}

?>

我的问题是,当我这样做时,我没有得到想要的预期输出.请给我最好的解决方案,以便在按钮单击时运行php函数,无论它是简单的button还是submit.

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.

推荐答案

我尝试了William的代码,谢谢兄弟.

I tried the code of William, Thanks brother.

但是它不能用作简单的按钮,我必须使用method ="post"添加表单.另外,我必须写提交而不是按钮.

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.

这是我的下面的代码.

<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();
}

?>

这篇关于单击按钮运行php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆