第二个下拉列表完全为空

时间:2017-10-01 20:41:52

标签: javascript php html ajax drop-down-menu

计划是根据第一个选择的项目填充第二个下拉菜单。我开发了下面的代码,但不幸的是,在第一个菜单中选择了一个项目后,第二个仍为空。你能帮帮我解决这个问题吗?提前谢谢。

PS。如果您想直接查看问题,可以访问我的测试网站,网址为:click here

的index.php

If

prova2.php

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
<script src="jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
    $('#departures').on('change',function(){
        var depname = $(this).val();
        if(depname){
            $.ajax({
                type:'POST',
                url:'prova2.php',
                data:'dep_name='+depname,
                success:function(html){
                    $('#arrivals').html(html); 
                }
            }); 
        }else{
            $('#departures').html('<option value="">Select country first</option>'); 
        }
    });
});
</script>

<form action='index.php' method='post'>
<h2>Select Departure:</h2>
<select id="departures" name="departures" class="form-control" onchange="ajaxfunction(this.value)">
        <option value="">--- Select Departure ---</option>

                    <?php
                        require('prova1.php');
                        $sql1 = "SELECT * FROM departures"; 
                        $sql2 = "SELECT * FROM arrivals"; 
                        $result1 = $mysqli->query($sql1);
 						while($row1 = $result1->fetch_assoc()){
					?>   
					<option value="<?php echo $row1["dep_name"]; ?>"><?php echo $row1["dep_name"]; ?></option>
					<?php } ?>

</select>
<br>

<h2>Select Arrival:</h2>
<select id="arrivals" name="arrivals" class="form-control">
        <option value="">--- Select your Arrival ---</option>
</select>
<br>
<h2>Select # of passengers</h2>
<select name="passengers" class="form-control">
        <option value="">--- # of passengers ---</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
</select>
<br>

<h2>Select # of bags</h2>
<select name="bags" class="form-control">
        <option value="">--- # of bags ---</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
</select>
<br>
<input type='submit' name='submit' id='submit' value='Get Selected Values' />
</form>

<?php
	if(isset($_POST['submit'])){
	$selected_val1 = $_POST['departures'];  
	$selected_val2 = $_POST['arrivals'];
	$selected_val3 = $_POST['passengers'];
	$selected_val4 = $_POST['bags'];    
	if ($selected_val3 < 4 AND $selected_val4 < 4){
	echo "You will drive with a taxi!"; 
	$query3 = "SELECT * FROM taxilist WHERE dep_name = '".$selected_val1."' AND arr_name = '".$selected_val2."'";
	} else {
	echo "You will drive with a van!";
	$query3 = "SELECT * FROM vanlist WHERE dep_name = '".$selected_val1."' AND arr_name = '".$selected_val2."'";
	}
	require('prova1.php');
	echo "<br>The price from " .$selected_val1. " to " .$selected_val2. " is: ";
	$result3 = $mysqli->query($query3);
		while($row3 = $result3->fetch_assoc()){
		echo $row3['price'];
		}
	}
?>

1 个答案:

答案 0 :(得分:0)

<强> prova2.php

ajax中的变量是dep_name。使用real_escape_string来转义字符串中的特殊字符,以便在SQL语句中使用。

<?php
require('prova1.php');

$dep_name  = isset($_POST["dep_name"]) ? $_POST["dep_name"] : '';

// escape variables for security
$dep_name = $mysqli->real_escape_string($dep_name);

if (!empty($dep_name)){
    //Get all state data
    $query = $mysqli->query("SELECT * FROM states WHERE arr_name = '".$dep_name."'");

    //Count total number of rows
    $rowCount = $query->num_rows;

    //Display states list
    if($rowCount > 0){
        $Return = '<option value="">Select arrival</option>';
        while($row = $query->fetch_assoc()){ 
            $Return .= '<option value="'.$row['arr_name'].'">'.$row['arr_name'].'</option>';
        }
    }else{
        $Return = '<option value="">Arrival not available</option>';
    }

    echo $Return;
}
?>