Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

php - 单击按钮如何调用PHP函数(How to call a PHP function on the click of a button)

I have created a page called functioncalling.php that contains two buttons, Submit and Insert .

(我创建了一个名为functioncalling.php的页面,其中包含两个按钮SubmitInsert 。)

As a beginner in PHP, I want to test which function is executed when a button gets clicked.

(作为PHP的初学者,我想测试单击按钮时执行的功能。)

I want the output to come on the same page.

(我希望输出出现在同一页面上。)

So I created two functions, one for each button.

(因此,我创建了两个功能,每个按钮一个。)

The source code for functioncalling.php is as follows:

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

The problem here is that I don't get any output after any of the buttons are clicked.

(这里的问题是单击任何按钮后都没有任何输出。)

Where exactly am I going wrong?

(我到底哪里出问题了?)

  ask by Razor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, you need Ajax here.

(是的,您在这里需要Ajax。)

Please refer to the code below for more details.

(请参阅下面的代码以获取更多详细信息。)

Change your markup like this

(像这样更改您的标记)

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

jQuery:

(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

(在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;
    }
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...