2

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.

4
  • A little more info would be nice, what data do you need to send to the script? Commented Aug 21, 2012 at 13:40
  • stackoverflow.com/faq#howtoask - then look at HTML Forms and Ajax Commented Aug 21, 2012 at 13:42
  • @TimPost It's a simple script that needs to run when a button is pressed. It is a click counter. Commented Aug 21, 2012 at 13:51
  • Ah I see what you mean .. by 'return anything' you mean it sends no output to the browser, so submitting a form to it is not an option. You might want to edit for clarity, when many think 'return' they think of it as in the return value of a function. Commented Aug 21, 2012 at 13:52

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think so. A form would be the most basic way to do this.
@Vap0r - I'm presuming that the OP doesn't want to reload the page when they click the button, and given the alternative most folks prefer AJAX to hidden iframe submissions (though I still use them occasionally). Regardless, I've appended the suggestion to my answer as an alternate solution.
It looks like the file the OP is posting to does not send any output to the browser at all, which is why he doesn't want to submit to it directly .. so yeah, AJAX or an iframe is going to be his only options.
4

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.

3 Comments

@user1431627 I my answer for you.
@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.
Nowadays, support for IE5 can be dropped for even more sure than for IE6.
2

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!");
  });
});

2 Comments

Not a fan of using jQuery. I like Vanilla JS. :)
@RobB, how do you figure? user1431627, then don't use it. The first code box is standalone and works without the jQuery.
0

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.

1 Comment

Re-read. I am simply trying to run a PHP file that does something on another page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.