简体   繁体   English

php中的依赖菜单使用java脚本方法?

[英]Dependant menus in php using java script method?

I am work on HTML and PHP on my project, but I have a problem:我在我的项目中使用 HTML 和 PHP,但我有一个问题:

I have a drop down list with countries and I want a nother drop down list to appear with (cities of this country) when user choose a country我有一个包含国家/地区的下拉列表,当用户选择一个国家/地区时,我希望另一个下拉列表显示(该国家/地区的城市)

I have the countries data base and cities data base ( sql )我有国家数据库和城市数据库(sql)

I try to use a java script method to do that but it didnt work我尝试使用 java 脚本方法来执行此操作,但没有成功

this is my code这是我的代码

First: this is the countries drop down list it is work good:第一:这是国家/地区下拉列表,效果很好:

<select name="SelectCountry"  id="SelectCountry" onchange="showCity()" >
            <?php 
            $Con= mysql_connect("localhost","root",""); 
if(!$Con) { die('Could not connect'.mysql_error());}
if(!mysql_selectdb("MyDB",$Con)){die(mysql_error());}
$sql = "SELECT *  FROM countries";
$result = mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result)){
       echo ("<option value=\"".$row['CountryID']."\">".$row['Name']."</option>");

}
mysql_close($Con);

Second, this is the java script function showCity() // didnt work any way !!其次,这是 java 脚本 function showCity() // 无论如何都不起作用!

<script>
             function showCity()
{

alert("in the function !!");  

Document.write(" <?php echo "<select 'SelectCity' ,'SelectCity'";
            echo "</select>";
            $theCountry=$_GET['SelectCountry'];  // get the country ID
            $Con= mysql_connect("localhost","root",""); 
if(!$Con) { die('Could not connect'.mysql_error());}
if(!mysql_selectdb("MyDB",$Con)){die(mysql_error());}
$sql = "SELECT * FROM cities WHERE  cities.Fips=(SELECT Fips FROM countries WHERE CountryID='$theCountry')"; // retrive the cities for the spicific country (work when I enter the ID manully in the sql query e.g CountryID='43')

$result = mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result)){
       echo ("<option value=\"".$row['Fips']."\">".$row['Fullname']."</option>"); // print all the cities in a menu (work when I enter the ID manully in the sql query e.g CountryID='43') 
}
mysql_close($Con);
             ");"; ?> ");
}
</script>

this method is to create a new dropdown list for the spicific country cities when the user change the country by using Onchange Event此方法是当用户使用Onchange事件更改国家/地区时,为特定国家/地区的城市创建一个新的下拉列表

I hope you will help me我希望你能帮助我

if there any Questions or misanderstod I am ready to answer or explain如果有任何问题或误解我准备回答或解释

thaaaanks all:) thaaaanks 所有:)

For your level of experience it would probably be best if all you do by onchange is to submit the form:对于您的经验水平,如果您通过 onchange 所做的只是提交表单,那可能是最好的:

<select name="SelectCountry"  id="SelectCountry" onchange="this.form.submit();" >

This is equivalent to pressing the submit button after selecting a country.这相当于选择国家后按下提交按钮。 (You are missing </select> in your excerpt by the way, although I expect you just didn't copy that here.) (顺便说一句,你在摘录中遗漏了</select> ,尽管我希望你只是没有在这里复制它。)

Then in PHP have something like... (note this code bit needs to be before the dropdown boxes so that the variable is there when you want to echo it)然后在 PHP 中有类似的东西......(注意这个代码位需要在下拉框之前,这样当你想要回应它时变量就在那里)

if ( isset($_GET['SelectCountry']) )
{
    $country = $_GET['SelectCountry'];
    $citySelect = "<select name='SelectCity'>";
    //query DB for cities of that country
    ...
    //now add the options from the query just like you did for the countries, except now use the cities
    ...
    $citySelect .= "</select>";
}
else
{
    $citySelect = ""; //to make sure the variable isn't undefined
}

Now you can just add underneath your CountrySelect:现在你可以在你的 CountrySelect 下面添加:

<? echo $citySelect; ?>

If a country was chosen before, the city menu will show.如果之前选择了国家/地区,则会显示城市菜单。

Note that this does not include even the lowest level of security, fool proofing and it can be very complicated to get a large form to work like this.请注意,这甚至不包括最低级别的安全性、防傻瓜功能,并且让大型表单像这样工作可能非常复杂。

$_GET['SelectCountry'] only works when you have the selects in a form tag and you click the submit button to reload the page (which creates the $_GET variable. A simple way to do this would be to add the country to your address URL and that will give you a $_GET too. $_GET['SelectCountry'] 仅当您在表单标签中进行选择并单击提交按钮重新加载页面时才有效(这会创建 $_GET 变量。一种简单的方法是将国家/地区添加到您的地址 URL,这也会给你一个 $_GET。

So inside the showCity() function, write this:所以在 showCity() function 里面,这样写:

 var i = document.getElementById("SelectCountry").selectedIndex;
 var countryValue = document.getElementById("SelectCountry").options[i].text;
 window.location.href = '/?SelectCountry='+countryValue;

This will redirect your web page to mysite.com/?SelectCountry=USA or something like that.这会将您的 web 页面重定向到 mysite.com/?SelectCountry=USA 或类似的页面。 Then your code will work.然后你的代码就会工作。 This isn't the best way to do it, but it should give you some results.这不是最好的方法,但它应该会给你一些结果。

This doesn't work because $_GET['SelectCountry'] is always going to be null. You're confused about the difference between client side and server side scripting.这不起作用,因为 $_GET['SelectCountry'] 始终为 null。您对客户端脚本和服务器端脚本之间的区别感到困惑。 Once you load the page for the first time, there is no GET variable yet, and so your Javascript is sitting on the browser with no cities.第一次加载页面后,还没有 GET 变量,因此您的 Javascript 位于没有城市的浏览器上。 Changing the country doesn't make the getCity() go back to the server.更改国家/地区不会使 getCity() go 返回到服务器。 It just looks at what it has already, which is nothing.它只是看看它已经拥有的东西,那什么都不是。 You need an AJAX function, which has the job of sending GET requests to the server and bringing back the results.您需要一个 AJAX function,它负责向服务器发送 GET 请求并返回结果。 Once it gets the list of cities, its going to want to know what you want it to do with that list.一旦获得城市列表,它就会想知道您希望它对该列表做什么。 You give it a function that makes a dropdown out of them.你给它一个 function 从它们中创建一个下拉列表。 This is known as a callback.这称为回调。 There are a lot of tutorials out there telling you how to make an AJAX function and where to put the callback.有很多教程告诉您如何制作 AJAX function 以及将回调放在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM