I need to run some PHP function when a button is clicked. I know this is not supposed to be php's use, rather js should do it, but what my functions are doing in gathering data from a server when the user asks it. Specifically, it gets some user data and writes it to a file, and the user should decide what data will be gathered.
How can I do this? I saw the post Run PHP File On Button Click but I am still not sure how to use it.
I'm learning, so please don't be too harsh
I have tried onclick()
and all sorts of things but it didn't lead to anything useful
This question is related to
javascript
php
html
ajax
button
A php file is run whenever you access it via an HTTP request be it GET,POST, PUT.
You can use JQuery/Ajax to send a request on a button click, or even just change the URL of the browser to navigate to the php address.
Depending on the data sent in the POST/GET you can have a switch statement running a different function.
You can utilize the code here: How to call PHP function from string stored in a Variable along with a switch statement to automatically call the appropriate function depending on data sent.
So on PHP side you can have something like this:
<?php
//see http://php.net/manual/en/function.call-user-func-array.php how to use extensively
if(isset($_GET['runFunction']) && function_exists($_GET['runFunction']))
call_user_func($_GET['runFunction']);
else
echo "Function not found or wrong input";
function test()
{
echo("test");
}
function hello()
{
echo("hello");
}
?>
and you can make the simplest get request using the address bar as testing:
http://127.0.0.1/test.php?runFunction=hellodddddd
results in:
Function not found or wrong input
http://127.0.0.1/test.php?runFunction=hello
results in:
hello
GET Request via JQuery
See: http://api.jquery.com/jQuery.get/
$.get("test.cgi", { name: "John"})
.done(function(data) {
alert("Data Loaded: " + data);
});
POST Request via JQuery
See: http://api.jquery.com/jQuery.post/
$.post("test.php", { name: "John"} );
GET Request via Javascript location
See: http://www.javascripter.net/faq/buttonli.htm
<input type=button
value="insert button text here"
onClick="self.location='Your_URL_here.php?name=hello'">
See PHP Turotial for reading post and get: http://www.tizag.com/phpT/postget.php
http://php.net/manual/en/function.call-user-func.php http://php.net/manual/en/function.function-exists.php
Use an AJAX Request on your PHP file, then display the result on your page, without any reloading.
http://api.jquery.com/load/ This is a simple solution if you don't need any POST data.
If you want to make a server request you should use AJAX, so you can send your desired parameters to the server and it can run whatever php you want with these parameters.
Example with pure javascript:
<input type="text" id="name" value="..."/>
<input type="text" id="location" value="..."/>
<input type="button" onclick="ajaxFunction();" value="Submit" />
<div id="ajaxDiv"></div>
<script type="text/javascript">
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var name = document.getElementById('name').value;
var location = document.getElementById('location').value;
var queryString = "?name=" + name + "&location=" + location;
ajaxRequest.open("POST", "some.php" + queryString, true);
ajaxRequest.send(null);
}
</script>
Example with jQuery Ajax: http://api.jquery.com/jQuery.ajax/
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
You can have one file with functions called for example functions.php
functions.php
<?php
myFunction($Name, $Location) {
// etc...
}
myFunction2() {
}
// ... many functions
?>
some.php
<?php include("functions.php");
$Name = $_POST['name'];
$Location = $_POST['location'];
myFunction($Name, $Location);
// make what you want with these variables...?>
It depends on what function you want to run. If you need something done on server side, like querying a database or setting something in the session or anything that can not be done on client side, you need AJAX, else you can do it on client-side with JavaScript. Don't make the server work when you can do what you need to do on client side.
jQuery provides an easy way to do ajax : http://api.jquery.com/jQuery.ajax/
<?php
if (isset($_POST['str'])){
function printme($str){
echo $str;
}
printme("{$_POST['str']}");
}
?>
<form action="<?php $_PHP_SELF ?>" method="POST">
<input type="text" name="str" /> <input type="submit" value="Submit"/>
</form>
Use ajax, a simple example,
HTML
<button id="button">Get Data</button>
Javascript
var button = document.getElementById("button");
button.addEventListener("click" ajaxFunction, false);
var ajaxFunction = function () {
// ajax code here
}
Alternatively look into jquery ajax
http://api.jquery.com/jQuery.ajax/
Source: Stackoverflow.com