duandi8852752 2014-01-13 11:46
浏览 31
已采纳

如何临时保存我在会话中选择的选项值?

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.

  • 写回答

3条回答 默认 最新

  • dtr87341 2014-01-13 12:00
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?