Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a PHP function upon button click?

Tags:

php

I want to run a PHP function with an HTML button click.

I can call a PHP script this way:

<form action="action.php" method="post">
  Name: <input type="text" name="txt"/>
  <input type="submit" />
</form>

But I don't want to invoke "action.php". I just want to call the PHP function I defined in this page. Can this be done?

like image 228
Voldemort Avatar asked Apr 27 '11 03:04

Voldemort


People also ask

Can we call PHP function on button click?

You can't run PHP functions on the click of a button like this. You can do it in Javascript, however.

How can I call PHP functions by JavaScript?

To summarise, you can use AJAX when you want to call a PHP function from JavaScript or run PHP code on some data generated inside browsers. You can use echo in PHP to output JavaScript code which will run later in the client's browser.

How do you call a function button in HTML?

To invoke this function in the html document, we have to create a simple button and using the onclick event attribute (which is an event handler) along with it, we can call the function by clicking on the button.

How can I check if a button is clicked in PHP?

How can I check if a button is clicked in PHP? PHP isset() function is used to check if a variable has been set or not. This can be useful to check the submit button is clicked or not. The isset() function will return true or false value.


3 Answers

The method I prefer to use is a mixture of jQuery/ajax calling an Object Oriented PHP class.

I have a jQuery listener/trigger for the button press with something like this:

$('#buttonId').live('click', function() {
    $.get('api.php?functionName=test&inputvar=something');

    return false;
});

That will call the api.php file through ajax and prevent any further action, such as form submission.

Then in the PHP file you could always do something basic like:

if ($_REQUEST['functionName'] == 'test') {
    test();
}

or if you have huge classes and alot of dynamic input you could do something more interesting like:

$functionName = $_REQUEST['functionName'];
if (method_exists($myClassInstance, $functionName))
    $myClassInstance->$functionName();

There are numerous ways to approach this, but these are my favorites. Another alternative is the Extjs framework which is built for this kind of activity but if you are not familiar with it already and the project is not 'huge' I would not concern myself with it.

Lastly, if you are needing to get a response back from the php file such as json results, then instead of using the jQuery: $.get() function, use the function $.getJSON

Hope that helps :)

like image 100
domnissient Avatar answered Oct 05 '22 11:10

domnissient


You need to do this on your end. Process the post and call the function in the action.php page on your own. You can also create a separate php page to call a function defined in action.php.

like image 26
Max Avatar answered Oct 05 '22 11:10

Max


You could easily do it with jquery

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>



    <script type="text/javascript">
    function submitform(){
        $('#myForm').post("pageName.php", $("#myForm").serialize());
    }
    </script> </head>

<form action="pageName.php" method="post" id"myForm">
Name: <input type="text" name="txt"/>
<input type="submit" />
</form>
like image 33
Tim Joyce Avatar answered Oct 05 '22 10:10

Tim Joyce