我是PHP的新手。我有个问题。我有一个简单的HTML表单,当我从select / option列表中选择国家时,我希望PHP在我选择的国家/地区回显消息。此外,我希望PHP include函数列出特定于该国家/地区名称的表单(通过连接)。有人可以通过指出我出错的地方来帮助我。我是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
$country_selected = '';
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include ("inc/OutletsIn" . $country_selected .".php";)
?>
</pre>
感谢您在我之前的帖子中提供的帮助。我能够在PHP包含中回显和连接文件名。但我有一个新问题。我选择的选项值(国家/地区名称)不会显示在框中。我如何保持会话的选定值。请指教。 再次感谢你。
答案 0 :(得分:2)
首先检查是否已使用$_POST['country_chosen']
向isset()
提供了值。然后删除导致语法错误的额外分号(在include()
)语句中:
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");
}
包含某些内容(例如)inc/OutletsInIsrael.php
答案 1 :(得分:1)
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 */ }
答案 2 :(得分:0)
这是您的更新代码,只是复制和放大替换:
<强> 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>
新答案,只需替换选择框的代码:
<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>