Kore
Kore

Reputation: 353

PHP $_GET with jQuery $.get call

I am currently learning jQuery and php.

So I have this php file that gets called with a jQuery function (there is another call somewhere else in the script where a=chkPw):

$.get("checkName.php?a=chkUser&user=" + rqUser, function(data) { ... });

My problem is that the php script doesn't seem to get in the if(isset($_GET)) { } block. I have debugged the value that it returns (1), so I don't understand what's wrong! I have also debugged the value for $_GET['a'] and it is indeed 'chkUser'.

Any help would be greatly appreciated.

<?php

$users = array('bill' => 'ore', 'ted' => 'wood');

if (isset($_GET)) {

    if ($_GET['a'] == 'chkUser') {
        if (!array_key_exists($_GET['user'], $users)) {
            echo 'okay';
        } else {
            echo 'denied';
        }

    } elseif ($_GET['a'] == 'checkPw') {

        $user = $_GET['user'];
        $pw = $_GET['pw'];
        $i = 0;

        // get username id
        foreach (array_keys($users) as $value) {
            if ($value == $user) {
                $user = $users[i];
            }
            i++;
        }

        // match pw
        if ($pw == $user) {
            echo 'okay';
        } else {
            echo 'denied'
        }
    }
}

?>

Upvotes: 0

Views: 927

Answers (4)

Bj&#248;rn Thomsen
Bj&#248;rn Thomsen

Reputation: 348

Hey it seems to me that this is working:

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
 <script type="text/javascript">
    $.get("checkNames.php?a=chkUser&user=bill", function(data) { console.log(data) });
    $.get("checkNames.php?a=chkPass&user=bill&pw=ore", function(data) { console.log(data)                 });

</script>

<?php

$users = array('bill' => 'ore', 'ted' => 'wood');

if (isset($_GET["a"])) {

  if ($_GET["a"] == 'chkUser') {
    if (!array_key_exists($_GET['user'], $users)) {
        echo 'okay';
    } else {
        echo 'denied';
    }

  } elseif ($_GET["a"] == 'chkPass') {

    $user = $_GET['user'];
    $pw = $_GET['pw'];
    if(array_key_exists($user, $users))
    {
        if($users[$user] == $pw)
        {
            echo "okay";
        }
        else
        {
            echo "denied";
        }
    }
    else
    {
        echo "denied";
    }

  }
}
?>

Upvotes: 1

tuxtitlan
tuxtitlan

Reputation: 1048

You could try this:

<?php
if(!empty($_GET)){
  ...
}
?>

To be sure about the GET payload use var_dump($_GET)

Upvotes: 0

Jared Drake
Jared Drake

Reputation: 1002

With a url like www.something.com/index.html?a=1
This code does in fact work on testing:

$users = array('bill' => 'ore', 'ted' => 'wood');

if (isset($_GET)) {
echo 'something';
}


Try commenting it down further to let us know if it is the whole whole elseif or just a part of it

Upvotes: 0

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

Try this if statement instead of current

if (count($_GET) > 0) {
    // existing code
}

Hope this will help you.

Upvotes: 0

Related Questions