Hello Good day everyone, anyone can help me with error " Undefined index: is_ajax"

I've encountered it. Im new in AJAX and im trying to use it on my login page.

This is the code for my main.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.latest.min.js" > </script>
<script type="text/javascript">
$(document).ready(function() {

	$("#login").click(function() {

		var action = $("#form1").attr('action');
		var form_data = {
			username: $("#username").val(),
			password: $("#password").val(),
			is_ajax: 1
		};

		$.ajax({
			type: "POST",
			url: action,
			data: form_data,
			success: function(response)
			{
				if(response == 'success')
					$("#form1").slideUp('slow', function() {
						$("#message").html("<p class='success'>You have logged in successfully!</p>");
					});
				else
					$("#message").html("<p class='error'>Invalid username and/or password.</p>");
			}
		});

		return false;
	});

});
</script>

</head>

<body>
<p>&nbsp;</p>
<div id="content">
  <h1>Login Form</h1>
  <form id="form1" name="form1" action="doLogin.php" 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>
</body></html>

and this is my code in doLogin.php

<?php

	$is_ajax = $_REQUEST['is_ajax'];
	if(isset($is_ajax) && $is_ajax)
	{
		$username = $_REQUEST['username'];
		$password = $_REQUEST['password'];

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

?>

My error is on doLogin.php. The Line 3.

$is_ajax = $_REQUEST['is_ajax'];

Anyone can help me? Thanks a lot

Dani AI

Generated

Short expert note: that PHP notice means your script tried to read a request key that doesn't exist. It happens when the AJAX payload never included is_ajax (or JavaScript never ran), or when the form was submitted normally by the browser so the extra field wasn't added. is right to recommend checking for the key before using it; here are reliable ways to diagnose and fix it.

Quick troubleshooting checklist

  • Open the browser console and fix any JavaScript errors; a broken script means the click handler never runs.
  • In DevTools → Network inspect the POST to doLogin.php and confirm is_ajax=1 is in the request payload. If it is not present, the server will show that undefined index.
  • Confirm jQuery actually loads (correct path or CDN fallback). If JS may not run, use a non-submit button or bind to the form submit event and call preventDefault() so the normal form submit never happens when JS is active.

Safer server-side handling (example)

// prefer explicit POST checks and input filtering
$is_ajax = filter_input(INPUT_POST, 'is_ajax', FILTER_VALIDATE_INT);
if ($is_ajax === 1) {
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $password = filter_input(INPUT_POST, 'password', FILTER_UNSAFE_RAW);
    // authenticate and echo result
    echo 'success';
} else {
    // either missing flag or non-AJAX fallback
    http_response_code(400);
    echo 'missing_flag';
}

Best-practice notes

  • Use $_POST (not $_REQUEST) when expecting a POST.
  • Don't suppress notices to hide the bug—fix missing-key checks instead.
  • For detecting AJAX reliably also check $_SERVER['HTTP_X_REQUESTED_WITH'] === 'xmlhttprequest' (not a security measure, just a convenience).
  • Return structured responses (JSON + proper HTTP codes) so the client can handle errors cleanly.

Following these steps will eliminate the notice and make the flow robust whether JavaScript runs or not.

is_ajax is not being sent from your form
so $_REQUEST is undefined
where is $_REQUEST coming from
also to prevent errors like that

$is_ajax = $_REQUEST['is_ajax'];
if(isset($is_ajax) && $is_ajax) {

should be

if(isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax']) {
$is_ajax = $_REQUEST['is_ajax'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.