5

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 Awards.html. Elsewhere I have created a PHP class, Awards.php which contains a function.

The source code of my files is given as follows:

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

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

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

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Razor
  • 2,581
  • 6
  • 21
  • 27
  • 1
    PHP is serverside and HTML client side. So you can't really call the `clickFunction` in your class directly. You will need an Ajax call or submit your form to another PHP page that calls the `clickFucntion` – tlenss Jan 23 '14 at 09:28
  • Thank you very much for replying. Can you please provide me the AJAX source code on how to achieve this and the location where I need to place this code? – Razor Jan 23 '14 at 09:31
  • You should be able to extract enough information from this thread http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – tlenss Jan 23 '14 at 09:47

3 Answers3

3

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

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

and in your clickFunction();

clickFunction(){
 $array = array(
   'status'  => '1'
 );    
 return $array; 
}
Agha Umair Ahmed
  • 1,037
  • 7
  • 12
  • Will this code work in my Awards page as it is an HTML file? Also, how will it exactly route the call to the clickFunction() function in the Awards.php class file? – Razor Jan 23 '14 at 09:53
  • It doesn't work when I change the file format from HTML to PHP because that page is part of a single-page application framework. – Razor Jan 23 '14 at 10:16
  • download and add jquery-1.10.2.js from jquery.com – Agha Umair Ahmed Jan 23 '14 at 10:18
  • write function in script tag like this – Agha Umair Ahmed Jan 23 '14 at 10:20
  • Is it necessary for the button to be surrounded by the
    tag?
    – Razor Jan 23 '14 at 12:48
  • no if your calling ajax request u don't need this but if you submitting a form you have to add
    tag
    – Agha Umair Ahmed Jan 23 '14 at 12:55
  • I have removed the
    tag but it's still not working. Basically the program control is not entering the .success property. I tested it by inserting alert('Test'); code over there. When I insert the same alert before the $.post() function, it does get executed successfully which means that it enters the onrequest() function but not inside the .success property.
    – Razor Jan 23 '14 at 13:04
  • Okay I checked that and I saw that it is not able to find the index.php file. Any statement on how to include files in JavaScript. Sorry for sounding like a noob but I'm new to PHP. – Razor Jan 23 '14 at 13:50
  • Okay I got it working now. The example.php file was not getting loaded. So I added the $("#divAddAward").load('example.php'); code and the code got executed. Thank you very much for your help. :-) – Razor Jan 23 '14 at 14:05
0

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

<?php echo $class->clickFunction(); ?>
Agha Umair Ahmed
  • 1,037
  • 7
  • 12
  • Thank you very much for replying. Can you please provide me the complete source code on how to achieve this and the location where I need to place this code? – Razor Jan 23 '14 at 09:33
0

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

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

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

olahell
  • 1,931
  • 3
  • 19
  • 35