1

I try to build a small log with ajax. I followed a guide but he builds without classes and functions in php. When I try to rebuild the entire application by putting it in the classes and functions, I get this error: Undefined index: is_ajax

and my code:

<?php
  class Login{
    public function LoggedIn(){
      $is_ajax = $_REQUEST['is_ajax'];
      if(isset($is_ajax) && $is_ajax)
      {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];

        if($username == 'demo' && $password == 'demo')
        {
          echo "success";   
        }
      }
    }
  }
?>

view:

public function DoLoginBox() {
    $Login = new Login();
    //inloggning form-tagg...
    return '<p>&nbsp;</p>
        <div id="content">
          <h1>Login Form</h1>
          <form id="form1" name="form1" action="'. $Login ->LoggedIn(). '"  method="post">
            <p>
              <label for="username">Username: </label>
              <input type="text" name="username" id="username" />
            </p>
            <p>
              <label for="password">Password: </label>
              <input type="password" name="password" id="password" />
            </p>
            <p>
              <input type="submit" id="login" name="login" />
            </p>
          </form>
            <div id="message"></div>
        </div>';
} 
Joe Flynn
  • 6,908
  • 6
  • 31
  • 44
Max
  • 45
  • 1
  • 11

5 Answers5

0

You should use

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

instead of

if(isset($is_ajax) && $is_ajax)
Nicolás Torres
  • 1,385
  • 8
  • 13
0

Change this line:

$is_ajax = $_REQUEST['is_ajax'];

into:

$is_ajax = !empty($_REQUEST['is_ajax']);

And you can remove isset($is_ajax)

NB: For this to work, the javascript that performs AJAX request MUST add is_ajax=1 (or some other non-empty value) to the request! This does not happen off the box.

poncha
  • 7,726
  • 2
  • 34
  • 38
  • took away the problem but he will not "echo" the script so it can say it is logged into the system – Max Jun 26 '12 at 13:10
  • you have my javacript i top of topic – Max Jun 26 '12 at 13:11
  • @MaxTorstensson have you tried watching what is the real content of `$_REQUEST` when request hits the server? – poncha Jun 26 '12 at 13:13
  • I put var_dump earlier on. var_dump (isset ($ _REQUEST ['is_ajax'])), where s = said boolean false - – Max Jun 26 '12 at 13:22
  • @MaxTorstensson do `var_dump($_REQUEST)` and see what exactly **is** in `$_REQUEST`... maybe your request does not bring any data at all... – poncha Jun 26 '12 at 13:27
  • it says that the array is empty. but feels that it does not even call the php code – Max Jun 26 '12 at 13:37
  • @MaxTorstensson try constructing the data yourself (instead of passing an object) and see if anything changes – poncha Jun 26 '12 at 13:44
  • thanks, so damn strange that it works without classes and functions, and call the action "dologin.php" when i change to features and classer was dying ... – Max Jun 26 '12 at 13:49
  • I just add a bit of the code. it kills the entire call to ajaxen. – Max Jun 26 '12 at 14:37
0

The line

$is_ajax = $_REQUEST['is_ajax'];

This gets executed every time the LoggedIn() function is called, if it's AJAX or not (I'm guessing that you add the parameter is_ajax from the browser).

Instead use something like

if(isset($_REQUEST['is_ajax']))
giggsey
  • 933
  • 2
  • 11
  • 31
0

Undefined index: XYZ means, you have an array, and you request its item by index, but that index does not exists.

example:

$array = array("a"=>1,"c"=>3);
echo($array["b"]);

you can check the existence:

if(!isset($array["b"]))die("Missing XYZ!");
  • everything works perfectly if I remove the classer and functions and invokes dologin.php directly (singing my login kit) – Max Jun 26 '12 at 13:14
  • Not true. `$array = Array("a"=>null); var_dump(isset($array["a"]));` – poncha Jun 26 '12 at 13:14
  • @2astalavista `!isset($array["index"])` on arrays does not mean "index does not exist", it means "index does not exist OR is null" – poncha Jun 26 '12 at 13:26
0

The HTTP_X_REQUESTED_WITH header is not supported by all browsers, and servers. You should use a dedicated get or post request, like ?is_ajax=1

See the related thread: Does $_SERVER['HTTP_X_REQUESTED_WITH'] exist in PHP or not?

Community
  • 1
  • 1
winya
  • 151
  • 3