在下拉列表中检索数据并将其再次保存在数据库中

时间:2015-06-12 07:54:55

标签: javascript php jquery html

我在寻找下列情况下相互关联的下拉列表:

如果选择学位,那么它只会显示具有该学位的大学,在选择大学后,它只显示大学有校区的城市。

现在选择了所有列表后,我想将答案保存在新表中,但无法这样做。文件附在下面。

的index.php

<?php 
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('commonapp');
$query="SELECT * FROM degree";
$result=mysql_query($query);
?>
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
    }
	
	function getState(degreeId) {		
		
		var strURL="findUniversity.php?degree="+degreeId;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('statediv').innerHTML=req.responseText;
						document.getElementById('citydiv').innerHTML='<select name="city">'+
						'<option>Select City</option>'+
				        '</select>';						
					} else {
						alert("Problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}		
	}
	function getCity(degreeId,universityId) {		
		var strURL="findCity.php?degree="+degreeId+"&uniid="+universityId;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('citydiv').innerHTML=req.responseText;						
					} else {
						alert("Problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
	}
</script>

<style type="text/css">
body {font-family: Arial, "Trebuchet MS";font-size: 17px;color: #52B6EB; }
a{font-weight: bold;letter-spacing: -1px;color: #52B6EB;text-decoration:none;}
a:hover{color: #99A607;text-decoration:underline;}
#top{width:43%;margin-top: 25px; height:60px; border:1px solid #BBBBBB; padding:10px;}
#tutorialHead{width:43%;margin-top: 12px; height:30px; border:1px solid #BBBBBB; padding:11px;}
.tutorialTitle{width:95%;float:left;color:#99A607}
.tutorialTitle  a{float:right;margin-left:20px;}
.tutorialLink{float:right;}
table
{
margin-top:70px;
border: 1px solid #BBBBBB;
padding:25px;
height: 35px;
}
</style>
</head>
<body>
<form method="post" action="admForm.php" name="form1">
<center>


<table width="45%"  cellspacing="0" cellpadding="0">
  <tr>
    <td width="75">Degree</td>
     <td width="50">:</td>
    <td  width="150">
	<select name="degree" onChange="getState(this.value)">
	<option value="">Select Degree</option>
	<?php while ($row=mysql_fetch_array($result)) { ?>
	<option value=<?php echo $row['id']?>><?php echo $row['degree']?></option>
	<?php } ?>
	</select>
	</td>
  </tr>
  <tr style="">
    <td>University</td>
    <td width="50">:</td>
    <td ><div id="statediv">
	<select name="university" >
	<option>Select University</option>
        </select></div></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td width="50">:</td>
    <td ><div id="citydiv">
	<select name="city" type="text">
	<option>City</option>
        </select></div></td>
  </tr>
  <tr>
  <td>
  <input class="buttom" name="Submit" value="Submit" type="submit">
	</td>
	</tr>
</table>
</center>
</form>
</body>
</html>

findUniversity

<?php $degree=intval($_GET['degree']);
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('commonapp');
$query="SELECT id,uniname  FROM uni WHERE degreeid='$degree'";
$result=mysql_query($query);

?>
<select name="university" onchange="getCity(<?php echo $degree?>,this.value)">
<option>Select University</option>
<?php while ($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id']?>><?php echo $row['uniname']?></option>
<?php } ?>

findCity.php

<?php $degreeId=intval($_GET['degree']);
$universityId=intval($_GET['uniid']);
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('commonapp');
$query="SELECT id,city FROM city WHERE degreeid='$degreeId' AND uniid='$universityId'";
$result=mysql_query($query);

?>
<select name="city">
<option>Select City</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<option value><?php echo $row['city']?></option>
<?php } ?>
</select>

0 个答案:

没有答案