我一直在努力让这个脚本工作,我无法弄清楚我做错了什么。我是PHP和Javascript的新手,我正在尝试修改我在互联网上找到的脚本,以便在我的网站上获得级联下拉。这是一个相当简单的脚本,遗憾的是它不使用AJAX,所以它需要页面刷新,但如果我很难用这个,我不认为我应该继续使用AJAX。我遇到的问题是脚本应该在页面刷新时从菜单中选择一个,但它总是重置为SELECT ONE,默认值。它在原始脚本中工作,但我做了一些修改,所以我无法弄清楚为什么我的不坚持。原始脚本是:
<?php
//***************************************
// This is downloaded from www.plus2net.com //
/// You can distribute this code with the link to www.plus2net.com ///
// Please don't remove the link to www.plus2net.com ///
// This is for your learning only not for commercial use. ///////
//The author is not responsible for any type of loss or problem or damage on using this script.//
/// You can use it at your own risk. /////
//*****************************************
$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='';
$dbpassword='';
// name of database
$dbname='dd';
////////////////////////////////////////
////// DONOT EDIT BELOW /////////
///////////////////////////////////////
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
//////// End of connecting to database ////////
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Multiple drop down list box from plus2net</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}
</script>
</head>
<body>
<?
/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
http://www.plus2net.com/php_tutorial/register-globals.php
*/
@$cat=$_GET['cat']; // Use this line or below line if register_global is off
if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not.
echo "Data Error";
exit;
}
//@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off
///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");
///////////// End of query for first list box////////////
/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory");
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); }
////////// end of query for second subcategory drop down list box ///////////////////////////
echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example action=dd-check.php////
////////// Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['cat_id']==@$cat)
{echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////
////////// Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
////////////////// This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center>
</body>
</html>
我的剧本是:
<?php
$dbservertype='mysql';
$servername='localhost';
$dbusername='newuser';
$dbpassword='';
$dbname='supplydb';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword){
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());}
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Multiple drop down list box from plus2net</title>
<SCRIPT language=JavaScript>
function reload(form){
var val=form.category.options[form.category.options.selectedIndex].text;
self.location='dd.php?cat=' + val ;
}
function reload2(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].text;
var val2=form.subcat.options[form.subcat.options.selectedIndex].text;
self.location='dd.php?cat=' + val + '&cat2=' + val2 ;
console.log(val2);
}
</script>
</head>
<body>
<?
////// Grab Get after page refresh
$cat=$_GET['cat'];
$command="SELECT * FROM product_group WHERE type = 1";
$quer2 = mysql_query($command);
if(isset($cat) and strlen($cat) > 1){
$num = explode(' ',$cat);
$number = $num[0];
$strLen = strlen( $number );
for ( $i = 0; $i < $strLen; $i++ )
{$arr[] = $number{$i};}
$arr1 = $arr [0];
$arr2 = $arr [1];
$arr3 = $arr1.$arr2;
$command1="SELECT * FROM `product_group` WHERE `group` LIKE '".$arr3."%' AND type = '2'";
$quer = mysql_query($command1);
}
echo "<form method=post name=category action='dd-check.php'>";
///////////// Create First Drop Down Box
echo "<select name='category' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($query = mysql_fetch_assoc($quer2)){
if($query['group']==$cat){
echo "<option selected value='$query[group]'>$query[group]</option>"."<BR>";}
else {echo "<option value='$query[group]'>$query[group]</option>";}
}
echo "</select>";
////////////// Create Second Drop Down Box
echo "<select name='subcat' onchange=\"reload2(this.form)\"><option value=''>Select one</option>";
while($query2 = mysql_fetch_array($quer)){
if($query2['group']==$cat2)
{echo "<option selected value='$query2[group]'>$query2[group]</option>"."<BR>";}
else {echo "<option value='$query2[group]'>$query2[group]</option>";}
}
echo "</select>";
echo "</form>";
print_r ($cat);
?>
</body>
</html>
任何帮助指出我的傻瓜错误将会有所帮助。提前谢谢。
答案 0 :(得分:0)
想出来。对不起格式化问题。从现在开始,我将通过格式化程序运行所有代码。在原始代码中,他们要求我在我的数据库中甚至没有设置的ID字段。一旦我设置了字段并更正了代码,它就能完美运行。