如何在单击按钮上调用PHP函数

Raz*_*zor 138 html php forms button

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

我创建了一个名为functioncalling.php的页面,其中包含两个按钮:Submit和Insert.作为PHP的初学者,我想测试单击按钮时执行的功能.我希望输出出现在同一页面上.所以我创建了两个函数,每个按钮一个.functioncalling.php的源代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.";
            }
        ?>
Run Code Online (Sandbox Code Playgroud)

这里的问题是在点击任何按钮后我没有得到任何输出.

任何人都可以告诉我我哪里错了?最早的回复将受到高度赞赏.先感谢您.

Roo*_*dra 77

是的,你需要ajax.有关详细信息,请参阅以下代码.

 

像这样更改你的标记

<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
Run Code Online (Sandbox Code Playgroud)

 

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");
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

在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;
    }
?>
Run Code Online (Sandbox Code Playgroud)

  • 我做了但只是意识到//code.jquery.com/...etc没有加载到localhost,https://会!代码工作正常对不起我的错误. (8认同)

小智 62

按钮单击是__CODE__PHP而PHP __CODE__,但您可以通过使用来实现此目的__CODE__

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

在你的php文件中:

<?php
    function abc($name){
        // Your code here
    }
?>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你提供解决方案.在您提供给我的AJAX代码片段中,如何在.done属性中调用PHP函数? (5认同)

Dav*_*idS 33

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

HTML:

<form action="theSamePage.php" method="post">
    <input type="submit" name="someAction" value="GO" />
</form>
Run Code Online (Sandbox Code Playgroud)

PHP:

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

  • 谢谢你!该方法是一个简单的基本HTML方法,无需插入AJAX即可使用 (2认同)
  • 不要直接访问超全局 $_SERVER 数组。 (2认同)

Jas*_*n W 15

你不能调用PHP函数,比如单击HTML中的按钮.因为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.";
    }
?>.
Run Code Online (Sandbox Code Playgroud)

您必须发布表单数据,然后检查单击的相应按钮.


小智 12

要在输入中显示$ message:

<?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>
Run Code Online (Sandbox Code Playgroud)

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


use*_*308 8

试试这个:

if($_POST['select'] and $_SERVER['REQUEST_METHOD'] == "POST"){
    select();
}
if($_POST['insert'] and $_SERVER['REQUEST_METHOD'] == "POST"){
    insert();
}
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案,而不是使用愚蠢的框架 (3认同)

小智 7

你可以在javascript或jquery ajax中这样写,并调用该文件

$('#btn').click(function(){
  $.ajax({
    url:'test.php?call=true',
    type:'GET',
    success:function(data){
    body.append(data);
    }
  });
})
Run Code Online (Sandbox Code Playgroud)
<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
        }
    }
?>
Run Code Online (Sandbox Code Playgroud)


lin*_*zzy 5

onclickHTML 中的属性调用 JavaScript 函数,而不是 PHP 函数。


San*_*hid 5

我被困在这个问题上,我用一个隐藏的字段解决了它:

<form method="post" action="test.php">
    <input type="hidden" name="ID" value"">
</form>
Run Code Online (Sandbox Code Playgroud)

value你可以添加任何你想添加。

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