如何通过点击一个button来调用PHP函数

我是PHP的新手,刚刚开始学习这门语言的基础知识。

我创build了一个名为functioncalling.php的页面,其中包含两个button:Submit和Insert。 作为PHP的初学者,我想testing一个button被点击时执行哪个函数。 我想要输出到同一页面。 所以我创build了两个函数,每个button一个。 functioncalling.php的源代码如下:

<html> <body> <form action="functioncalling.php"> <input type="text" name="txt" /> <input type="submit" name="insert" value="insert" onclick="insert()" /> <input type="submit" name="select" value="select" onclick="select()" /> </form> <?php function select(){ echo "The select function is called."; } function insert(){ echo "The insert function is called."; } ?> 

这里的问题是,在任何button被点击后,我没有得到任何输出。

任何人都可以告诉我我到底错在哪里? 最早的回复将高度赞赏。 先谢谢你。

button点击是client side而PHP是server side ,但你可以通过使用ajax来实现

  $('.button').click(function() { $.ajax({ type: "POST", url: "some.php", data: { name: "John" } }).done(function( msg ) { alert( "Data Saved: " + msg ); }); }); 

在你的php文件中:

 <?php function abc($name){ //your code here } ?> 

是的,你需要在这里的Ajax。 有关更多详细信息,请参阅下面的代码。

像这样改变你的标记

 <input type="submit" class="button" name="insert" value="insert" /> <input type="submit" class="button" name="select" value="select" /> 

jQuery的: –

 $(document).ready(function(){ $('.button').click(function(){ var clickBtnValue = $(this).val(); var ajaxurl = 'ajax.php', data = {'action': clickBtnValue}; $.post(ajaxurl, data, function (response) { // Response div goes here. alert("action performed successfully"); }); }); }); 

在ajax.php

 <?php if (isset($_POST['action'])) { switch ($_POST['action']) { case 'insert': insert(); break; case 'select': select(); break; } } function select() { echo "The select function is called."; exit; } function insert() { echo "The insert function is called."; exit; } ?> 

你不能像调用HTML中的button那样调用php函数。 因为HTML在客户端而PHP在服务器端运行。

要么你需要使用一些Ajax或像下面的代码片段那样做。

 <?php if($_GET){ if(isset($_GET['insert'])){ insert(); }elseif(isset($_GET['select'])){ select(); } } function select() { echo "The select function is called."; } function insert() { echo "The insert function is called."; } ?>. 

您必须发布您的表单数据,然后检查被点击的适当的button。

在input中显示$消息:

  <?php if(isset($_POST['insert'])){ $message= "The insert function is called."; } if(isset($_POST['select'])){ $message="The select function is called."; } ?> <form method="post"> <input type="text" name="txt" value="<?php if(isset($message)){ echo $message;}?>" > <input type="submit" name="insert" value="insert"> <input type="submit" name="select" value="select" > </form> 

要使用functioncalling.php作为外部文件,你必须以某种方式将它包含在你的html文件中

尝试这个:

 if($_POST['select'] and $_SERVER['REQUEST_METHOD'] == "POST"){ select(); } if($_POST['insert'] and $_SERVER['REQUEST_METHOD'] == "POST"){ insert(); } 

你可以这样写在JavaScript或jQuery的Ajax和调用文件

 $('#btn').click(function(){ $.ajax({ url:'test.php?call=true', type:'GET', success:function(data){ body.append(data); } }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <form method='get' > <input type="button" id="btn" value="click"> </form> <?php if(isset($_GET['call'])){ function anyfunction(){ echo "added"; // your funtion code } } ?> 

您应该使button调用相同的页面,并在PHP部分检查button是否按下:

HTML:

 <form action="theSamePage.php" method="post"> <input type="submit" name="someAction" value="GO" /> </form> 

PHP:

 <?php if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction'])) { func(); } function func() { // do stuff } ?> 

我被困在这里,我用隐藏的领域解决了它

 <form method="post" action="test.php"> <input type="hidden" name="ID" value""> </form> 

在值你可以添加任何你想添加

在test.php中,您可以通过$ _Post [ID]

HTML中的onclick属性调用Javascript函数,而不是PHP函数。

在表单动作自行调用的地方使用recursion调用。 然后添加PHP代码以相同的forms来捕捉它。 在foo.php你的表单会在foo.php调用foo.php

 <html> <body> <form action="foo.php" method="post"> 

一旦表单被提交,它会自动调用( foo.php ),你可以通过PHP预定义variables$_SERVER来捕获它,如下面的代码所示

 <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { echo "caught post"; } ?> </form> </body> </html> 

这是一个你可以使用的例子:

 <html> <body> <form action="btnclick.php" method="get"> <input type="submit" name="on" value="on"> <input type="submit" name="off" value="off"> </form> </body> </html> <?php if(isset($_GET['on'])) { onFunc(); } if(isset($_GET['off'])) { offFunc(); } function onFunc(){ echo "Button on Clicked"; } function offFunc(){ echo "Button off clicked"; } ?> 

即时通讯不知道在PHP尝试只是

 <html> <head> <script> function insert() { document.getElementById("demo").innerHTML="The insert function is called"; } function select() { document.getElementById("demo").innerHTML="The select function is called."; } </script> </head> <body> <form> <input type="text" name="txt" /> <input type="button" name="insert" value="insert" onclick="insert()" /> <input type="button" name="select" value="select" onclick="select()" /> </form> <p id="demo"></p> </body> </html> 
 <input type="button" name="insert" value="insert" onclick="insert()" /> <input type="button" name="select" value="select" onclick="select()" /> 

改变你的这个,它应该工作。