Andrew
Andrew

Reputation: 4441

AJAX loaded php page not letting $_GET's find values

I have a PHP project that I'm using AJAX in to refresh a part of the page, part of that page grabs options from the URL on what it's going to show. On load and refresh this works perfectly. When AJAX is introduced to reload the php file inside of the div, the only part that doesn't work is the $_GET.

I really don't know anything about AJAX and am trying to learn, but can not figure this out for the life of me. And Google searches haven't lead me in the right direction.

AJAX:

jQuery(document).ready(function(){
    jQuery('.ajaxform').submit( function() {

        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            data    : $(this).serialize(),
            success : function( data ) {
              $('#footer-holder').html( data );
              $('#authCheck').load('authentication.php');
                      },
            error   : function(){
                         alert('Something wrong');
                      }
        });

        return false;
    });

});

authentication.php has some $_GET commands, the page executes perfectly on refresh and everything else executes perfectly with this AJAX call except the $_GET requests.

Sample authentication.php

if(isset($_GET['test'])){
 echo $_GET['test'];
}else{
 echo "nothing in GET";
}

Sample index.php

echo "<div id='authCheck'>";
include 'authentication.php';
echo "</div>";

Upvotes: 0

Views: 389

Answers (1)

Anand G
Anand G

Reputation: 3200

If I could understand your question then , you are trying to authenticate user. Using GET method for login is never ever a good way of doing it, even if you are using ajax, This may help you

   jQuery(document).ready(function(){
    jQuery('.ajaxform').submit( function() {

    $.ajax({
        url     : '/authentication.php',
        type    : 'POST',
        data    : $(this).serialize(),
        success : function( data ) {
                     if(data == 'success')
                       //Successfully authenticated
                     } else {
                        console.log('Invalid Credentials')
                     }               
                  },
           error   : function(){
                     alert('Something wrong');
                  }
    });

    return false;
});

});

in PHP

  //Check username and password, Checking isset POST is not enough for security
  if($_POST['username'] != '' && $POST['password'] != ''){
      //Do you logic to check password id correct or not
      //if true
      //echo success
      //else 
      //echo 'fail'
  }else{
     echo "Invalid credentials";
  }

hope this will help you

Upvotes: 1

Related Questions