Run PHP code on button click

10,362

Solution 1

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.

Solution 2

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.

Solution 3

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!");
  });
});
Share:
10,362

Related videos on Youtube

user1431627
Author by

user1431627

Updated on September 16, 2022

Comments

  • user1431627
    user1431627 over 1 year

    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.

  • user1431627
    user1431627 over 11 years
    Re-read. I am simply trying to run a PHP file that does something on another page.
  • Jason Stackhouse
    Jason Stackhouse over 11 years
    @user1431627 I my answer for you.
  • Jason Stackhouse
    Jason Stackhouse over 11 years
    @user1431627 In my opinion this is a good way of doing it. But if you're in doubt, ask somebody else or set this question up for bounty.
  • Shi
    Shi over 11 years
    Nowadays, support for IE5 can be dropped for even more sure than for IE6.