jQuery to post to php file

Question!

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...

EDIT:

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

<?php

session_start();

if(isset($_SESSION['username'])){
  //go to home page
  header('Location: ...home.html');
}else{
  //go to login page
  header('Location: ...login.html');
}

?>


Answers

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.



    $.post("/authenticate.php",null,function(data){

    // do something based on response returned
   if($data){alert("authenticated");}
    else
      alert("not authenticated");
    });

in your php file

if(isset($_SESSION['user']))
{    
 echo true;
}
else
return false;
By : 3nigma


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