NewToPHP
NewToPHP

Reputation: 3

How to temporarily hold the option value I selected in session?

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.

<pre>

    <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";)
?>
</pre>

Thank you for your help on my earlier post. I am able to echo out and concatenate file name in PHP include. But I have a new problem. 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.

Upvotes: 0

Views: 1215

Answers (3)

Harshal
Harshal

Reputation: 3622

Here is your updated code, just copy & replace :

component_project_in_countries.php

<pre>

    <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 
if(isset($_POST['country_chosen']))
{
    $country_selected = '';
    $country_selected = $_POST['country_chosen'];
    echo "You have chosen to view outlets in " . $country_selected;
    include ("inc/OutletsIn" . $country_selected .".php");// here you have added `;` on wrong place.
}
?>
</pre>

New answer, Just replace the code of select box :

 <select name="country_chosen" onchange="document.form1.submit()" id="country">
            <option value="">All Middle Eastern Countries</option>
            <option value="Iraq" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Iraq')) echo 'selected' ?> >Iraq</option>
            <option value="Kuwait" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Kuwait')) echo 'selected' ?> >Kuwait</option>
            <option value="Bahrain" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Bahrain')) echo 'selected' ?> >Bahrain</option>
            <option value="Saudi" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Saudi')) echo 'selected' ?> >Saudi Arabia</option>
            <option value="Qatar" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Qatar')) echo 'selected' ?> >Qatar</option>
            <option value="Oman" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Oman')) echo 'selected' ?> >Oman</option>
            <option value="Yemen" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Yemen')) echo 'selected' ?> >Yemen</option>            
            <option value="Jordan" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Jordan')) echo 'selected' ?> >Jordan</option>
            <option value="Israel" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Israel')) echo 'selected' ?> >Israel</option>
        </select>

Upvotes: 0

George
George

Reputation: 36784

First check that a value has been given to $_POST['country_chosen'] with isset(). Then remove that extra semi-colon that is causing a syntax error (in your include()) statement:

if(isset($_POST['country_chosen'])){
    $country_selected = $_POST['country_chosen'];
    echo "You have chosen to view outlets in " . $country_selected;
    include("inc/OutletsIn" . $country_selected .".php");
}

Includes something (like) inc/OutletsInIsrael.php

Upvotes: 2

Huey
Huey

Reputation: 5220

if(!empty($_POST['country_chosen'])){
    $country_selected = $_POST['country_chosen'];
    echo "You have chosen to view outlets in " . $country_selected;
    require "inc/OutletsIn".$country_selected.".php";
    //no brackets required
} else { /* throw error */ }

Upvotes: 1

Related Questions