简体   繁体   English

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

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

I am a rookie PHP developer. 我是一名新手PHP开发人员。

I have a PHP web project with an HTML page that contains an Add button. 我有一个PHP Web项目,其中包含一个包含Add按钮的HTML页面。 The name of the page is Awards.html . 该页面的名称是Awards.html Elsewhere I have created a PHP class, Awards.php which contains a function. 在其他地方,我创建了一个包含函数的PHP类Awards.php

The source code of my files is given as follows: 我的文件的源代码如下:

Awards.html Awards.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 Awards.php

    <?php

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

The problem here is that on the click event of the Add button, I need to call the clickFunction() of the Awards.php file. 这里的问题是,在Add按钮的click事件中,我需要调用Awards.php文件的clickFunction() Can anyone please tell me how to do this? 谁能告诉我怎么做?

Replies at the earliest will be highly appreciated. 最早的回复将受到高度赞赏。 Thank you. 谢谢。

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

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(); ?>

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. 因此,使用AJAX调用来调用Awards.php。 Eg. 例如。 with JQuery it could look like this $.get("Awards.php", function() { alert( "success" ); }) 使用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(); ?> <?php echo $class->clickFunction(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM