如何从 HTML 按钮单击调用类中的 PHP 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21304054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:02:11  来源:igfitidea点击:

How to Call PHP Function in a Class from HTML Button Click

phphtmlbuttonclick

提问by Razor

I am a rookie PHP developer.

我是一个菜鸟 PHP 开发人员。

I have a PHP web project with an HTML page that contains an Addbutton. The name of the page is Awards.html. Elsewhere I have created a PHP class, Awards.phpwhich contains a function.

我有一个包含一个Add按钮的 HTML 页面的 PHP Web 项目。页面的名称是Awards.html。在其他地方,我创建了一个 PHP 类,Awards.php其中包含一个函数。

The source code of my files is given as follows:

我的文件的源代码如下:

Awards.html

奖项.html

<div class="divparent">
    <div class="modal-header">
        <div class="btn-group">
            <button class="btn" data-bind="click: closeModal">Exit</button>
        </div>
    </div>
    <div class="modal-title">
        <h1 id="headerid">Awards</h1>
    </div>
    <div class="modal-body">
        <input id="hdnValueCurrentAwardSoid" type="hidden" value="null" />
        <div class="divleft">

            <input id="txtName" maxlength="80" type="text" class="water newpost1" placeholder="Award Name" tabindex="1" />        

            <section class="ThumbnailContainer">
                <img id="imgThumbnail" class="imgThumbnail" />
                <img src="http://localhost/rockontechnologies/Images/GreenRibbon.png" id="pictureribbon" class="pictureribbon" />
                <input type="text" contenteditable="false" readonly id="transformtext" />
            </section>

            <textarea id="txtdescription" placeholder="Description" class="water newpost1" rows="4" tabindex="2"></textarea>    

            <div class="ui-widget">
                <input id="txtIssueOrg" maxlength="50" type="text" placeholder="Issue Organization" />
            </div>

        </div>
    </div>
    <div class = "divbottom">
        <div id="divAddAward">
            <button class="btn" onclick="">Add</button>
    </div>
    </div>
</div>

Awards.php

奖项.php

    <?php

    class Awards
    {
        function clickFunction()
        {
            //Function that needs to be executed!
        }
    }

The problem here is that on the click event of the Addbutton, I need to call the clickFunction()of the Awards.phpfile. Can anyone please tell me how to do this?

这里的问题是,在单击事件Add按钮,我需要调用clickFunction()的的Awards.php文件。谁能告诉我如何做到这一点?

Replies at the earliest will be highly appreciated. Thank you.

尽早答复将不胜感激。谢谢你。

采纳答案by Agha Umair Ahmed

You should do something like that

你应该做这样的事情

<button class="btn" onclick="onrequest();">Add</button>

function onrequest() {
      $.post( 
          'example.php'
       ).success(function(resp){
            json = $.parseJSON(resp);
            alert(json);
       });
}

and in example.php call your function

并在 example.php 中调用您的函数

$class = new Awards();
$method =  $class->clickFunction();
echo json_encode($method);

and in your clickFunction();

并在您的 clickFunction();

clickFunction(){
 $array = array(
   'status'  => '1'
 );    
 return $array; 
}

回答by Agha Umair Ahmed

first of you have to a instance of class which is like that

首先你必须有一个这样的类的实例

$class = new Awards();

then if you want to onclick event you should make a javascript code but how to get the function you can do like this

那么如果你想 onclick 事件你应该做一个 javascript 代码但是如何获得你可以这样做的功能

<?php echo $class->clickFunction(); ?>

回答by olahell

I think you can't call a class directly from HTML in PHP you have to call it through HTTP by loading a page.

我认为你不能直接从 PHP 中的 HTML 调用一个类,你必须通过加载页面通过 HTTP 调用它。

Hence, use a AJAX call to call Awards.php. Eg. with JQuery it could look like this $.get("Awards.php", function() { alert( "success" ); })

因此,使用 AJAX 调用来调用 Awards.php。例如。使用 JQuery,它可能看起来像这样$.get("Awards.php", function() { alert( "success" ); })

In your php-file you should also call your class something like this <?php echo $class->clickFunction(); ?>

在你的 php 文件中,你也应该这样称呼你的班级 <?php echo $class->clickFunction(); ?>