Hi everyone,

I am new to PHP. I have a problem. I have a simple HTML form where when I select the country from select / option list provided, i want PHP to echo message on country I selected. Also I want PHP include function to list the form specific to that country name (by concatanating). Can someone please help me by pointing where I went wrong. I am new to PHP, self taught and have no idea if this approach is correct. Thanks in advance for anticipated help.

<form name="form1" method="post" action="component_project_in_countries.php">            
<label for="country">View outlets in which country? </label>
<select name="country_chosen" onchange="document.form1.submit()" id="country">
    <option value="">All Middle Eastern Countries</option>
    <option value="Iraq">Iraq</option>
    <option value="Kuwait">Kuwait</option>
    <option value="Bahrain">Bahrain</option>
    <option value="Saudi">Saudi Arabia</option>
    <option value="Qatar">Qatar</option>
    <option value="Oman">Oman</option>
    <option value="Yemen">Yemen</option>          
    <option value="Jordan">Jordan</option>
    <option value="Israel">Israel</option>
</select>
</form>

<br>

<?php 
$country_selected = '';
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include ("inc/OutletsIn" . $country_selected .".php";) //Want to concatanate country name and .php to the end
?>

Dani AI

Generated

Good progress so far — spotted the stray semicolon and pointed out a simpler action target — now the remaining issue is making the select “sticky” so the chosen country remains visible after submit (or persisting it across pages). Two safe, practical patterns:

  • Server-side sticky form: rebuild the <select> from an array and add the selected attribute when the option equals the submitted value. Escape output with htmlspecialchars() to avoid XSS.
  • Persist to session when the choice must survive navigation: call session_start(), save the submitted value, and use that session value when rendering the form.

Example: build options and keep the selected value (uses a different field name than the original posts):

<?php
// render time: pick POST, then SESSION, then empty
$selected = $_POST['country_sel'] ?? $_SESSION['country_sel'] ?? '';

$countries = [
  'Iraq','Kuwait','Bahrain','Saudi Arabia','Qatar','Oman','Yemen','Jordan','Israel'
];

echo '<select name="country_sel" id="country_sel">';
foreach ($countries as $c) {
  $safe = htmlspecialchars($c, ENT_QUOTES, 'UTF-8');
  $sel = ($c === $selected) ? ' selected="selected"' : '';
  echo "<option value=\"$safe\"$sel>$safe</option>";
}
echo '</select>';
?>

To persist selection across pages:

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['country_sel'])) {
  $_SESSION['country_sel'] = $_POST['country_sel'];
}
$selected = $_SESSION['country_sel'] ?? '';
?>

Security note (important): never include files directly from raw user input. Use a whitelist map from allowed country keys to filenames and include only mapped files. That prevents path traversal and remote‑inclusion risks. Also provide a non-JS fallback (submit button) and prefer action="" (post to same page) for clarity.

Recommended Answers

All 3 Replies

Well it seems like this should be working. Only thing that I see is wrong (in case you haven't found it yet yourself) is the misplacement of your semicolon on line 23. It shouldn't be include ("inc/OutletsIn" . $country_selected .".php";) but include ("inc/OutletsIn" . $country_selected .".php");.

Thank you for pointing that out. I managed to figure out and get it working. BUt i have a new porblem. The option value i selected (country name) does not get displayed in the box. How can i hold the chosen value for the session. Please advise. Thank you again.

Hai;

Try this

<form name="form1" method="post" action="#">            
<label for="country">View outlets in which country? </label>
<select name="country_chosen" onchange="document.form1.submit()" id="country">
    <option value="">All Middle Eastern Countries</option>
    <option value="Iraq">Iraq</option>
    <option value="Kuwait">Kuwait</option>
    <option value="Bahrain">Bahrain</option>
    <option value="Saudi">Saudi Arabia</option>
    <option value="Qatar">Qatar</option>
    <option value="Oman">Oman</option>
    <option value="Yemen">Yemen</option>          
    <option value="Jordan">Jordan</option>
    <option value="Israel">Israel</option>
</select>
</form>

<br>

<?php 
$country_selected = '';
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include ("inc/OutletsIn" . $country_selected .".php"); //Want to concatanate country name and .php to the end
?>
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.