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!

5
  • please elaborate with more code... Commented Dec 30, 2010 at 3: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). Commented Dec 30, 2010 at 3: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. Commented Dec 30, 2010 at 3:21
  • What do you want to use? HEAD, PUT, DELETE? Commented Dec 30, 2010 at 3: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! Commented Dec 30, 2010 at 4:04

6 Answers 6

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.

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

4 Comments

.load() is a GET or POST request, which OP does not want.
It is roughly equivalent to $.get(url, data, success)
If you want to request to a url, then for all intents and purposes, it needs to be a get or post.
Passing an object as the second parameter forces a post. So maybe, pass an empty/dummy object?
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
}
});

4 Comments

Thanks! I noticed that you wrote: function(html) Does this mean that the return type can be HTML? I really don't want to return ANYthing, if possible.
@MichaelNovello notice that ANY $.ajax will return something. His function just catches whatever comes back.
In the code above, what would happen if the 'success:' part were left off, since I don't need it? And my PHP file didn't have any 'echo' statements?
To answer the question of @MichaelNovello, "nothing".
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?

Comments

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.

4 Comments

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!
@MichaelNovello Well don't forget to mark an answer as accepted so we know which one helped you.
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.
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"
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.

1 Comment

Technically HTTP can use any of nine verbs.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.