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!

Answers


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

.load()

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

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


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?


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.


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.


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.


Need Your Help

How to serialize a JavaScript associative array?

javascript jquery arrays forms

I need to serialize an associative JavaScript array.

How to show a child window full-screen mode?

javascript dom popup

I'm opening a child window through the parent window, and I want the child window to be displayed in full-screen mode.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.