第二个函数忘记了所选值


Second function has forgotten the selected value

我正在尝试使用 ajax 函数从数据库中获取数据。但是代码的某些部分不起作用。$car_id, witch 是下拉列表中的选定值,在函数的第二部分中被遗忘。

首先。我有一个下拉菜单,用户可以在其中选择汽车:

<select name="car_id" id="car_id" onChange="getCar(this.value)">
    <option value="0">Select</option>
    <option value="1">Volvo XC90</option>
    <option value="2">Saab 95</option>
    <option value="3">Mercedes SLK</option>
    <option value="4">Audi TT</option>
</select>

选择(onChange事件)后,我的脚本运行以下函数:

<script> 
function getCar(selectedItem) {
    jQuery.ajax({ 
        url: 'get1.php', 
        method: 'POST', 
        data: {'car_id' : jQuery('#car_id').val()},
        success: function(response){ 
            jQuery('#car_id').val(response); 
            jQuery('#car_brand').val(response);
            getCar2()
            }, 
        error: function (request, status, error) { 
            alert(request.responseText); 
            }, 
        }); 
    } 
</script>

此函数运行以下 SQL 查询: SELECT car_brand FROM cars WHERE car_id = $car_id

该函数从数据库中获取car_brand,并将其发布到#car_brand文本字段中。这部分代码工作正常。但如您所见,该函数调用第二个函数:

<script> 
function getCar2(selectedItem) {
    jQuery.ajax({ 
        url: 'get2.php', 
        method: 'POST', 
        data: {'car_id' : jQuery('#car_id').val()},
        success: function(response){ 
            jQuery('#car_id').val(response); 
            jQuery('#car_model').val(response);
            }, 
        error: function (request, status, error) { 
            alert(request.responseText); 
            }, 
        }); 
    } 
</script>

这次函数尝试获取car_model。该函数需要将其发布到#car_model文本字段中。但这行不通。当我查看Firebug控制台时,我看到第二个函数getCar2忘记了$car_id。

当我选择值 3 时,第一个函数运行 SQL 语句: SELECT car_brand FROM cars WHERE car_id = 3第二个功能: SELECT car_model FROM cars WHERE car_id =

如何解决这个问题?

更新 1:

<?php    
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname) ;
// Check connection
if ($conn->connect_error) {
    die('Connection failed: ' . $conn->connect_error) ;
}else {
    $car_id = isset($_POST['car_id'])?$_POST['car_id']:'';
    $car_brand = isset($_POST['car_brand'])?$_POST['car_brand']:'';
    $query = 'SELECT * FROM cars WHERE car_id="' . mysqli_real_escape_string($conn, $car_id) . '"';    
    $res = mysqli_query($conn, $query) ;
  if (mysqli_num_rows($res) > 0) {
     $result = mysqli_fetch_assoc($res) ;
    echo $result['car_brand'];   
  }else{
     $result = mysqli_fetch_assoc($res) ;
    echo "";  
  }    
} 
?>

从getCar中删除jQuery('#car_id').val(response);。 无需更新它,因为它的一切都准备好了car_id

<script> 
    function getCar(selectedItem) {
        jQuery.ajax({ 
            url: 'get1.php', 
            method: 'POST', 
            data: {'car_id' : jQuery('#car_id').val()},
            success: function(response){ 
                jQuery('#car_brand').val(response);
                getCar2()
                }, 
            error: function (request, status, error) { 
                alert(request.responseText); 
                }, 
            }); 
        } 
    </script>

首先,您将#car_id重新分配给函数 getCar() 中的新值,并带有以下行:jQuery('#car_id').val(response);

您正在为两个 dom 元素分配与第一个函数 ajax 回调中response相同的值。我不知道这是否是你打算做的。您正在通过 selectedItem 变量向 getCar() 函数传递select元素的实际值。如果您确实需要重新分配 #car_id 的值,则应将 selectedItem 变量传递给 getCar2 函数,该函数仍应包含最初选择的值,并在 getCar2 函数中使用它。

否则,从 getCar 函数中删除行jQuery('#car_id').val(response);,也可以删除变量对两个函数的传递,因为您没有在其他任何地方使用它们。