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.ph开发者_开发问答p". I just want to call the PHP function I defined in this page. Can this be done?
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 :)
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.
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>
This won't work. PHP is pretty much done processing by the time the user handles the page. You'd have to use AJAX to send the data to the server and retrieve the response.
You can have the following approaches:
1> Use AJAX to load "action.php" with the data entered in the fields as POST to the page and then take the response and put it into the body; or,
2> You cannot make it work as PHP is a server-side programming language and HTML which you are trying to use with PHP is a client side programming language. So there is no way to make it work other than AJAX.
This is a little complicated. You can either submit the form to itself, check to see if the post variable is set (if it is, then the page is being run because the form was submitted to it), or you can use ajax. The first is an easier solution, and the second doesn't require a page reload. Pick your poison ;)
Look at this for the first solution, and look at jquery's ajax functions for the second.
You should include a.php In action.php, For example :
action.php :
include( 'a.php' );
//Your codes
if you want for example ok() function in a.php, now a.php exist in action.php
You could try:
<form action="pageName.php/functionName" method="post">
Name: <input type="text" name="txt"/>
<input type="submit" />
</form>
精彩评论