Trying to access array offset on value of type null php error

<form method="post">
		    <select name="late" class="login_btn" onchange="this.form.submit()">
		        <option <?php if($_SESSION['country']=='' || $_SESSION['country']=='Select Country') echo 'selected';?> >Select Country</option>
		        <option value="India" <?php if($_SESSION['country']=='India') echo 'selected';?>>India</option>
		        <option value="Others" <?php if($_SESSION['country']=='Others') echo 'selected';?>>Others</option>
		    </select>

Do you have an appropriate session_start() at the beginning of your code? Is there a session variable called ‘country’ defined? If not, look at isset() to check before trying to use the value.

Which line of your code is the error appearing on?

all lines starting from option

OK. And do you have session_start() in your code? The error message says that you’re trying to access an array offset (the ‘country’ array element) on a null value, which suggests that the $_SESSION array does not exist. Remember that session_start() must be before any browser output (even a new-line or single blank space) otherwise you’ll get a “headers already sent” error message.

You also would have to have… you know… set $_SESSION['country'] before you try and read it, even if the session has been started.

This error typically occurs when you try to access an array index on a variable that is null. You need to ensure that the variable is not null before accessing its elements. You can use conditional statements or null coalescing operator (??) to handle this situation.

There are a few issues here but lets start by NOT hard coding every line. That will become a headache real fast. Ultimately, your select options will be an array of options, possibly from a database so you should handle things as an array. Start by defining your array of options.

Because you have NAMED your select as “late”, I will define the array as $late_options and define known values.

$late_options = array('India','Others');

In your <select you can then loop through this array using foreach.
I generally dislike bouncing in and out of <php so I tend to write within <php and echo html using single quotes like so.

echo '<form method="post">
	<select name="late" class="login_btn" onchange="this.form.submit()">
		<option value="">Select Country</option>';
		foreach($late_options as $option): 
			echo '<option value="'.$option.'">'.$option.'</option>';
		endforeach;
	echo '</select>
</form>';
?>

You can see that as I loop through each of the $late_options they are then defined as $option, which is then redended as the option value and what is shown as the option.

Now what you have not shown is where you set the POST value to the SESSION value but you are using 2 different [KEY] names, i.e. “late” and “county” and as I would use that same KEY I will do that here.

It is a good practice to check for server request method, so we’ll start there then check if $_POST['late'] is not empty. As we are working with a list of options you can further check $_POST['late'] against the $late_options array to make sure the value being passed with POST is in the array and has a valid value. If it passes these conditions you can then set the post value to session.

<?php 
session_start(); 		
	 
$late_options = array('India','Others');	

if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['late']) && in_array($_POST['late'],$late_options)):
	$_SESSION['late'] = $_POST['late'];
endif;
?>

Now looking back at your form with dynamic options, you can check the session value against the option value and define a variable as selected or empty. Note in this case I like to use !empty() as handles both is set and that the array KEY is set, (which handles the undefined index error) and has a value.

$selected_option = (!empty($_SESSION['late']) && $_SESSION['late'] == $option ? ' selected="selected"' : '');

This variable $selected_option can then be added to your <option line.

echo '<option value="'.$option.'"'.$selected_option.'>'.$option.'</option>';

My sample now looks like this.

<?php 
session_start(); 		
	 
$late_options = array('India','Others');	

if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['late']) && in_array($_POST['late'],$late_options)):
	$_SESSION['late'] = $_POST['late'];
endif;	
?>
<html>
<body>

<?php
echo '<form method="post">
	<select name="late" class="login_btn" onchange="this.form.submit()">
		<option value="">Select Country</option>';
		foreach($late_options as $option): 
			$selected_option = (!empty($_SESSION['late']) && $_SESSION['late'] == $option ? ' selected="selected"' : '');
			echo '<option value="'.$option.'"'.$selected_option.'>'.$option.'</option>';
		endforeach;
	echo '</select>
</form>';	
?> 
					
</body>
</html>

Anyway, this is generally how I would do it.