首页 文章

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

提问于
浏览
121

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

我创建了一个名为functioncalling.php的页面,其中包含两个按钮:Submit和Insert . 作为PHP的初学者,我想测试单击按钮时执行的功能 . 我希望输出出现在同一页面上 . 所以我创建了两个函数,每个按钮一个 . 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.";
}
?>

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

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

11 回答

  • 2

    按钮单击是 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 );
    });    
    
        });
    

    In your php file:

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

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

    Change your markup like this

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

    In 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;
    }
    ?>
    
  • 53

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

    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     
        }
    ?>
    
  • 69

    你不能调用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.";
        }
    
    ?>.
    

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

  • 3

    要在输入中显示$ 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>
    

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

  • 3

    试试这个:

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

    你可以在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
    
    }
    }
    ?>
    
  • 8

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

  • 15

    使用表单操作调用自身的递归调用 . 然后以相同的形式添加PHP代码以捕获它 . 在 foo.php 中,您的表单将在 post 上调用 foo.php

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

    表单提交后,它将自行调用( foo.php ),您可以通过PHP预定义变量 $_SERVER 捕获它,如下面的代码所示

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

    我被困在这里,我用隐藏的字段解决了它

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

    在值中,您可以添加要添加的内容

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

  • 12

    以下是您可以使用的示例:

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

相关问题