Javascript 使用PHP MySQL和Java动态下拉列表

Javascript 使用PHP MySQL和Java动态下拉列表,javascript,php,mysql,Javascript,Php,Mysql,使用MySQL插入查询在phpmysql和AJAX中完成动态下拉操作非常有效 将日期插入MySQL表的代码 <?php require('../conn/include.php'); require('quick.php'); $query="SELECT * FROM category"; $result=mysql_query($query); $project=$_POST['project']; $alttext=$_POST['alttext']; $relation=$_POS

使用MySQL插入查询在phpmysql和AJAX中完成动态下拉操作非常有效

将日期插入MySQL表的代码

<?php
require('../conn/include.php');
require('quick.php');
$query="SELECT * FROM category";
$result=mysql_query($query);
$project=$_POST['project'];
$alttext=$_POST['alttext'];
$relation=$_POST['state'];;

if(isset($_FILES['image'])) {
    $errors=array();
    $allowed_ext=array('jpg','png','jpeg','JPG');
    $filename=$_FILES['image']['name'];
    $name=stripslashes($filename);
    $type=strtolower(end(explode('.',$filename)));
    $size=$_FILES['image']['size'];
    $file_tmp=$_FILES['image']['tmp_name'];

if(in_array($type,$allowed_ext) ===false) {
    $errors[]= "<span class=\"notification n-error\">Extenstion Not Allowed</span>";
}
if($size > 1048576) {
    $errors[]= "<span class=\"notification n-error\">File must be less then 2mb</span>";
}if(file_exists('../../images/a/gallery/'.$filename)) {
   $errors[]= "<span class=\"notification n-error\">File $filname Already Exists in directory</span>";

}if(empty($errors)) {
    if(move_uploaded_file($file_tmp, '../../images/a/gallery/'.$filename)) {
        $insert="Insert into `my`.gallery(name,alttext,project,relation)VALUE('$name','$alttext','$project','$relation')";
        //echo $insert;
        $que=mysql_query($insert);
    echo "<span class=\"notification n-success\">File $filname Uploaded Sucessfully</span>";
    header('Refresh:3; url:gallery.php');
    }
}else {
    foreach($errors as $error) {
        echo $error,'<br/>';    
    }
}
}

?>
AJAX代码

<script language="javascript" type="text/javascript">
function getXMLHTTP() { //function 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(cate_id) {        
var strURL="findsect.php?country="+cate_id;
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;

            } else {
                alert("Problem while using XMLHTTP:\n" + req.statusText);
            }
        }               
    }           
    req.open("GET", strURL, true);
    req.send(null);
}       
}
</script>
第二个下拉列表或findsec.php的代码

<?php 
$country=$_GET['country'];
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('my');
$query="SELECT * FROM gallery_section WHERE related='$country'";
$result=mysql_query($query);

?>
<select name="state" onchange="getCity(<?php echo $country?>,this.value)">
<option>Select State</option>
<?php while ($row=mysql_fetch_array($result)) { ?>
<option value="<?php echo $row['title']?>"><?php echo $row['title']?></option>
<?php } ?>
</select>

感谢帮助我的尼克·王尔德。

我想你的意思是当第二个下拉列表的选项值是多个单词时。如果是这种情况,问题是你缺少报价;改用这个:

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

作为findsec.php的最后三行,Java在哪里here@NickWilde我知道,但看看标题?第二部分是Ajax。我想他指的是javascript
<?php 
$country=$_GET['country'];
$con = mysql_connect('localhost', 'root', ''); 
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('my');
$query="SELECT * FROM gallery_section WHERE related='$country'";
$result=mysql_query($query);

?>
<select name="state" onchange="getCity(<?php echo $country?>,this.value)">
<option>Select State</option>
<?php while ($row=mysql_fetch_array($result)) { ?>
<option value="<?php echo $row['title']?>"><?php echo $row['title']?></option>
<?php } ?>
</select>