0

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'
        }
    }
}

?>

5
  • What are you expecting it to do? Commented Aug 18, 2012 at 18:17
  • When a=chkUser, it's supposed to see if the chkUser exists as a key in $users. As long as I can get that to work, I can figure out the rest. But my problem is that the php doesn't get into the if (isset($_GET)) { } block for some reason. Commented Aug 18, 2012 at 18:19
  • 1
    I recommend not building your $.get URL like that...try something like $.get('checkName.php',{'a':'chkUser','user':rqUser}, function ... Commented Aug 18, 2012 at 18:27
  • var_dump( $_GET, $_POST, $_REQUEST ); Commented Aug 18, 2012 at 18:42
  • Isnt it just a typo in your check ? it seems to me that you are using two different "actions" chkUser and chkPw but in your check you are assuming that you get "chkUser" and "checkPw" ? Commented Aug 18, 2012 at 19:03

4 Answers 4

1

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

  }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! I don't know what I was doing with loops and crazy things. I am not used to using maps, heh. It does work! and I actually found the original reason for my script breaking - a silly misspelled variable... I am not used to having to type $ in front of them! I feel embarrassed. Thanks to everyone though!
Well i saw the mistakes when i copied your code but thought it would be more helpful with a working sample :)
0

Try this if statement instead of current

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

Hope this will help you.

Comments

0

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

3 Comments

Right, it returns 'okay' if the key doesn't exist. It means the username is free to be taken.
Alright, I see what you're doing. I thought you were validating :( Anyways, what does your get url look like in firebug?
I have narrowed it down to a problem with the } elseif ($_GET['a'] == 'checkPw') { It does work if I delete that elseif block...
0

You could try this:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.