我正在尝试构建我的第一个 jquery web 应用程序,但我遇到了一个障碍,似乎无法解决这个问题。

我有一个 PHP 页面和一个 HTML 页面。
HTML 页面有一个带有三重下拉列表的表单。
PHP 页面连接到数据库,但我不确定如何从 php 页面传递查询结果以填充 html/javascript 页面上的下拉列表。

到目前为止,这是我的代码。

HTML:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

$(document).ready(function() {
$("#selector").submit(function() {

    $.ajax({
       type: "GET",
       url: "DBConnect.php",
       success: function(msg){
         alert(msg);
       }
});

var select_car_make = $('#select_car_make').attr('value');
    var select_car_model = $('#select_car_model').attr('value');
    var select_car_year = $('#select_car_year').attr('value');
    alert("submitted");


    }); //end submit
});
</script>

<h1 style="alignment-adjust:center">Car information:</h1>
<hr  />
<div id="results">

<form action="get.php" id="selector" method="get" name="sizer">
<table width="451" height="70" border="0">

      <th width="145" height="66" scope="row"><label for="select_car_make"></label>
          <div align="center">
            <select name="select_car_make" id="select_car_make" onchange="">
            </select>
      </div></th>
    <td width="144"><label for="select_car_model"></label>
      <div align="center">
        <select name="select_car_model" id="select_car_model">
        </select>
      </div></td>
    <td width="140"><label for="select_car_year"></label>
      <div align="center">
        <select name="select_car_year" id="select_car_year">
        </select>
      </div></td>
  </tr>
</table>

<input name="completed" type="submit" value="submit" />
</form>

这是PHP页面:
<?php

$DBConnect = mysqli_connect("localhost", "root", "password", "testing")
or die("<p>Unable to select the database.</p>" . "<p> Error code " . mysqli_errno($DBConnect) . ": " . mysqli_error($DBConnect)) . "<p>";

echo "<p>Successfully opened the database.</p>";


$SQLString1 = " SELECT car_make FROM cars";
$QueryResult = mysqli_query($DBConnect, $SQLString1)

最佳答案

嘿贾斯汀,如果这是你第一次尝试,我会保持超简单。将选择框放在带有 ID 的内容中,例如 span 或 div。然后让你的 AJAX 响应只重写内容,这是一种简单明了的 AJAX 开始方式,例如:

$.ajax({
    type: "POST",
    url: "/call.php",
    data: "var=" + myData,
    success: function(response){
        $("#someId").html(response);
    }
});

在您的远程页面上,只需回显整个选择框:
echo "<select name='cars'>";
echo "<option value='".$value."'>".$name."</option>";
etc...

同样,这不是最好的方法,但肯定不是最坏的方法。

10-06 05:27
查看更多