jQuery to post to php file


I have an index.html file which I want to run some jQuery when it is loaded. Essentially, I want to check to see if a user is already logged in based on some session variables.

So index.html will contain some jQuery which uses $(document).ready(function(){ });

In this function I want to just fire autheticate.php which checks to see if $_SESSION['user'] is set, if it is then it will redirect to home page otherwise it will redirect to login page...

how can I post in jQuery without having a html form? I just want to post to a url...


Based on @jondavidjohn's answer I changed my web app so that it uses index.php to check sessions:



  //go to home page
  header('Location: ...home.html');
  //go to login page
  header('Location: ...login.html');



Why bother with the AJAX request? Since you're building the page with PHP, just have PHP embed some variables in a JavaScript block:

By : Marc B

You can use $.post(url, params), where url is a string and params is a hash with your post data.


    // do something based on response returned
      alert("not authenticated");

in your php file

 echo true;
return false;
By : 3nigma

This video can help you solving your question :)
By: admin