Michael Novello
Michael Novello

Reputation: 4901

Can I use jQuery to run a PHP file on my server, without using POST or GET?

The information needed by my PHP script is already in the $_SESSION variable, so I don't need to pass anything along to the server. I just need the PHP file to run.

Right now I'm using jQuery's POST method. I'm passing along a fake post variable (whatever: 'you want') to make the browser happy. Is there a cleaner way to do this?

Thanks!

Upvotes: 1

Views: 1464

Answers (6)

andres descalzo
andres descalzo

Reputation: 14967

try this

HTML:

<div id="status"></div>

JS:

try {

    var 
        xmlhttp = false,
        url = "YOUR PHP SCRIPT";

    if (window.XMLHttpRequest) {
       xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 

      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try{
           page_request = new ActiveXObject("Microsoft.XMLHTTP")
        } catch (e){}
      }

    }

    if (xmlhttp) {

      $("#status").append("<div>xmlhttp ok</div>");

      xmlhttp.onreadystatechange = function() {
          $("#status").append("<div>xmlhttp onreadystatechange</div>");
          setTimeout(function() { xmlhttp.abort() }, 50);
      }

      xmlhttp.open('GET', url, true);

      xmlhttp.send(null);

    }
} catch(e) {
    $("#status").append("<div>error: "+e.description+"</div>");
 }

Send the request but then aborted. The PHP script should continue working.

Upvotes: 0

turtlepick
turtlepick

Reputation: 2714

9.4 HEAD The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

From http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

Check this question:

Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

Upvotes: 1

DGM
DGM

Reputation: 26979

Technically, as your question is worded, no. jquery, running on the client must talk to the server to run the PHP, and thus must use http. http can use GET, POST, PUT, or DELETE, but realistically, most browsers don't support PUT or DELETE, so therefore you must use GET or POST.

As others have indicated, return nothing and ignore the return.. but you still have to at least go through the motions of doing a http request.

Upvotes: 0

jcolebrand
jcolebrand

Reputation: 16035

Here's the deal, in order to talk to a webserver, you have to make an HTTP request, which means you have to use an HTTP verb. If you want to send data (such as a cookie for a session identification) then you're going to have to use either GET, POST, PUT, or DELETE. (altho technically for this you could use HEAD as well) See Wikipedia for more information on HTTP verbs http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

So what you need to realize is that you're pretty well stuck with using either POST or GET if you're going to use jQuery, and I'm not even sure that an XmlHttpRequest (which is how these requests are usually made) can do anything BUT those two. This link says you can. http://www.jibbering.com/2002/4/httprequest.html

So that's the short and long of things. Once you understand how HTTP works this becomes painfully obvious.

Upvotes: 0

d2burke
d2burke

Reputation: 4111

You can run the PHP just by virtue of an AJAX call if you'd like. You can declare it as 'POST', yet you do not have to send any data:

$.ajax({
type: "POST",
url:"some_script.php",
success: function(html){
    //DO NOTHING
}
});

Upvotes: 1

picus
picus

Reputation: 1537

.load()

http://api.jquery.com/load/

Although there isn't much wrong with .get() the functions only get more flexible from .load() on up.

Upvotes: 3

Related Questions