Razor Razor - 1 year ago 181
HTML Question

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

I am a rookie PHP developer.

I have a PHP web project with an HTML page that contains an Add button. The name of the page is

. Elsewhere I have created a PHP class,
which contains a function.

The source code of my files is given as follows:


<div class="divparent">
<div class="modal-header">
<div class="btn-group">
<button class="btn" data-bind="click: closeModal">Exit</button>
<div class="modal-title">
<h1 id="headerid">Awards</h1>
<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" />

<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 class = "divbottom">
<div id="divAddAward">
<button class="btn" onclick="">Add</button>



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
of the
file. Can anyone please tell me how to do this?

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

Answer Source

You should do something like that

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

function onrequest() {
            json = $.parseJSON(resp);

and in example.php call your function

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

and in your clickFunction();

 $array = array(
   'status'  => '1'
 return $array; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download