1

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!

Michael Novello
  • 4,871
  • 3
  • 15
  • 7
  • please elaborate with more code... – Jakub Dec 30 '10 at 03:19
  • Also I don't need anything returned by the PHP file. But to make the browser happy, I'm returning some bogus XML (even though I don't do anything with it). – Michael Novello Dec 30 '10 at 03:19
  • You may want to pass along the php sessionid from the frontend to the php script. I've seen on Safari in OSX that when ajax requests are sent cookies are not, so it wont know about your session. – John Boker Dec 30 '10 at 03:21
  • What do you want to use? HEAD, PUT, DELETE? – turtlepick Dec 30 '10 at 03:44
  • Pass along the session ID with the HTTP request? Does jQuery have access to the cookie (so I can grab the session ID)? I guess then I need to learn how to make PHP use the session ID passed to it via POST (or GET), even though if jQuery doesn't send a cookie along then the server wouldn't even know a session exists in the first place! – Michael Novello Dec 30 '10 at 04:04

6 Answers6

3

.load()

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

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

picus
  • 1,507
  • 2
  • 13
  • 23
1

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
}
});
d2burke
  • 4,081
  • 3
  • 39
  • 51
1

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?

Community
  • 1
  • 1
turtlepick
  • 2,694
  • 23
  • 22
0

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.

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • This makes sense. I think I'll create a function for making HTTP requests, so I can just call that function without worrying about parameters and return values (since that'll be taken care of in the function). Thanks! – Michael Novello Dec 30 '10 at 03:45
  • @MichaelNovello Well don't forget to mark an answer as accepted so we know which one helped you. – jcolebrand Dec 30 '10 at 05:11
  • you know, I would still use one of the ajax methods, and put up a spinner to indicate something is happening, and then use return codes or something simply for status - ok, or error. – DGM Dec 30 '10 at 16:30
  • I would actually use POST because I like to think ahead, and if I'm at least passing a session variable I may indeed need to pass something else in the next iteration of the design, since these things tend to be fluid. And if I'm passing more, I might like to get back notification of an error or the like. I can always discard the returned value ;) ... but yeah, I agree @DGM ~ I really only wanted to remark to his "how do I do it without using an HTTP verb" – jcolebrand Dec 30 '10 at 16:32
0

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.

DGM
  • 26,629
  • 7
  • 58
  • 79
0

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.

andres descalzo
  • 14,887
  • 13
  • 64
  • 115