【问题标题】:How to Call PHP Function in a Class from HTML Button Click如何从 HTML 按钮单击调用类中的 PHP 函数
【发布时间】:2014-02-13 18:11:23
【问题描述】:

我是一名新手 PHP 开发人员。

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

我的文件源代码如下:

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

    <?php

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

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

我们将非常感谢您尽早回复。谢谢。

【问题讨论】:

  • PHP 是服务器端和 HTML 客户端。所以你不能直接在你的班级里直接打电话给clickFunction。您将需要一个 Ajax 调用或将您的表单提交到另一个调用 clickFucntion 的 PHP 页面
  • 非常感谢您的回复。您能否提供有关如何实现此功能的 AJAX 源代码以及我需要放置此代码的位置?
  • 你应该能够从这个线程stackoverflow.com/questions/5004233/…提取足够的信息

标签: php html button click


【解决方案1】:

你应该这样做

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

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

在 example.php 中调用你的函数

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

在你的 clickFunction();

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

【讨论】:

  • 此代码是否可以在我的奖项页面中使用,因为它是一个 HTML 文件?另外,它如何准确地将调用路由到 Awards.php 类文件中的 clickFunction() 函数?
  • 当我将文件格式从 HTML 更改为 PHP 时它不起作用,因为该页面是单页应用程序框架的一部分。
  • 从jquery.com下载并添加jquery-1.10.2.js
  • 像这样在脚本标签中写入函数
  • 按钮需要被
    标签包围吗?
【解决方案2】:

首先你们必须有一个类似的类的实例

$class = new Awards();

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

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

【讨论】:

  • 非常感谢您的回复。能否请您提供有关如何实现此功能的完整源代码以及我需要放置此代码的位置?
【解决方案3】:

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

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

在您的 php 文件中,您还应该将类称为 &lt;?php echo $class-&gt;clickFunction(); ?&gt;

【讨论】:

    猜你喜欢
    • 2015-02-27
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多