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.

3
  • 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 Commented Jan 23, 2014 at 9: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? Commented Jan 23, 2014 at 9:31
  • You should be able to extract enough information from this thread stackoverflow.com/questions/5004233/… Commented Jan 23, 2014 at 9:47

3 Answers 3

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; 
}
Sign up to request clarification or add additional context in comments.

9 Comments

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?
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.
download and add jquery-1.10.2.js from jquery.com
write function in script tag like this <script> function onrequest(){ code } </script>
Is it necessary for the button to be surrounded by the <form> tag?
|
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(); ?>

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.