Reputation: 815
What is the best way to just run a PHP script when the user clicks a button? It sends nothing back to the user whatsoever! (The PHP script only sends a PostgreSQL query.)
I have only found ways of returning data. I only want to run it.
Upvotes: 2
Views: 10414
Reputation: 1826
This is the best I could come up with. Hope it was what you were looking for.
/* Function that we create to handle backwards compatiblity to browsers.
What it does is to try diffrent types of XMLHttpRequests. */
function getXMLHttp() {
var xmlHttp;
try {
//Firefox, Opera, Safari, Chrome, IE7+
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
//Internet Explorer 6
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
//Internet Explorer 5
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
return false;
}
}
}
return xmlHttp;
}
// Here, we send the request
function send() {
/* We say the variable xmlHttp is the function
where we try the diffrent XMLHttpRequests*/
var xmlHttp = getXMLHttp();
// We open your PHP file...
xmlHttp.open("POST", "yourphpfile.php", true);
// ...and send it to the server
xmlHttp.send();
}
A little brief Since you're not getting anything back to the user, you use POST and not GET. It sends a request to the server to the file. And as you said in your question, something was sent to a PostgreSQL server. However, that PHP script is runned on your hosting server that supports PHP.
Upvotes: 4
Reputation: 14479
You're looking for AJAX (asynchronous javascript). Just have a javascript function call the target script and either don't return anything OR don't do anything with the return value. Alternately, you could simply have form that submits to a hidden iframe on the page.
Upvotes: 6
Reputation: 2616
The most basic way to do this would be an html form.
<form action="somePHPfile.php" method="post">
<input type="button" value="Run somePHPfile.php" />
</form>
You could also do what Ben D suggested, but that's not the most basic way of doing this. Using jquery:
$("#YOURbuttonID").click(function(){
$.post("yourPHPfile.php", function(){
alert("Success!");
});
});
Upvotes: 2
Reputation: 77
You will need to use Ajax and then you can update a div etc so user knows if the query was executed properly or not.
Upvotes: 0