开发者

Calling a php function with the help of a button or a click

I have created a class named as "member" and inside the class I have a function named update(). Now I want to call this function when the user clicks 'UPDATE' button.

I know I can do this by simply creating an "UPDATE.开发者_如何学Cphp" file.

MY QUESTION IS :-

Can I call the "update() function" directly without creating an extra file? I have already created an object of the same class. I just want to call the update function when the user clicks on update button.


an action.php example:

<?
if (isset($_GET[update])){
$id=new myClass;
$id::update($params);
exit;}

//rest of your code here

?>


Your button is in your view. Your method is in your class . You need a controller sitting in the middle routing the requests. Whether you use 1 file or many files for your requests is up to you. But you'll need a PHP file sitting in the middle to capture the button click (a POST) and then call the method.


As far as I know you can't do this without reloading your page, checking if some set parameters exist which indicate the button is clicked and than execute the button. If this is what you are looking for... yes it is possible on page reload. No as far as I know it is not possible directly because your php-function has to parse the results again.

Remember that a Model-View-Controller way is better and that this will allow you to ajax (or regular) requests to the controller-class.


You do it on the same page and have an if statement which checks for the button submission (not completely event driven) like so

if (isset($_POST['btn_update']))
{
    update();
}

<input type="submit" name="btn_update" value="Update" />

That will have to be wrapped in a form.

Or you could do it with AJAX so that a full page refresh isn't necessary. Check out the jQuery website for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜