Moving values, maybe in sessions?

Please help with this code.

In my HTML, I have to select as follows Country,region, city, suburb but I need filter on each trough a .js in PHP.

My HTML code for country and region:

I remove the “<” and “>” for the code to display

"

input type="text" class="form-control" id="amots" databasefield="country" name="country" placeholder="Country -  Required" required="required"  onKeyUp="search(this);" autocomplete="off"/
                 div id="layer2"></div
   
                 
input type="text" class="form-control" id="amotsa" databasefield="region" name="region" placeholder="Region / Province -  Required" required="required"  onKeyUp="searcha(this);" autocomplete="off"/
                 div id="layer3"></div"

My code in .js

function search(){
         var search_input_value = $("#amots").val();
         var databasefield = $("#amots").attr("databasefield");
	$.post( "searchexec.php" , { "search" : search_input_value , "databasefield" : databasefield }, function( data ) {
		$( "#layer2" ).html( data );
		//attach event handler
		$(".search_result").unbind("click");
		$(".search_result").bind("click",function(){			
			  $("#amots").val($(this).html());
		});		
	});
}
function searcha(){
         var search_input_value = $("#amotsa").val();
         var databasefield = $("#amotsa").attr("databasefield");
	$.post( "searchexec.php" , { "search" : search_input_value , "databasefield" : databasefield}, function( data ) {
		$( "#layer3" ).html( data );
		//attach event handler
		$(".search_result").unbind("click");
		$(".search_result").bind("click",function(){			
			$("#amotsa").val($(this).html());
		});		
	});
}

My Code in PHP file

f($search == 'country'){
   
    if (isset($_REQUEST['search']) && $_REQUEST['search'] != '') {
	//Add slashes to any quotes to avoid SQL problems.
	$search = $_REQUEST['search'];
        $sql = "SELECT id,country FROM country USE INDEX(country) where country like '" .$search . "%'";
        $result = mysqli_query($conn, $sql);
	while($row = mysqli_fetch_array($result))
	{		
		echo '<div id="'.$row['id'].'" class="search_result">'.$row['country'].'</div>';
	}
   }
}
if($search == 'region'){
   
    if (isset($_REQUEST['search']) && $_REQUEST['search'] != '') {
	//Add slashes to any quotes to avoid SQL problems.
	$search = $_REQUEST['search'];
        $sql = "SELECT id,region FROM region USE INDEX(region) where region like '" .$search . "% and countryid = '$countryid'";
        $result = mysqli_query($conn, $sql);
	while($row = mysqli_fetch_array($result))
	{		
		echo '<div id="'.$row['id'].'" class="search_result">'.$row['region'].'</div>';
	}
   }
}

My questions:

How do I get the ‘$countryid’ in the last select statement to filter the regions for that country?

Thank you very much.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

.

Thank you very much.

I am so new on this site but will take care. It makes perfect sense though.

Much appreciated.