0

希望你能帮忙,因为我开始拔头发了:) 似乎有很多关于这个的链接,但我无法让它们中的任何一个工作,所以我只想用外行的方式问.

我有一个数据库......它有区域、区域、经理、员工的字段

我有一个前端表单,里面有选择框......

当您选择区域时,我需要区域选择框来动态填充数据库中的相关区域而不刷新页面

Then when the Area selct option is chosen the Manager one needs to populate. 等等。

毫无疑问,这需要一个 ajax/Jquery 解决方案,正如我所说,有很多关于此的文章,但我无法让它们工作。在今天之前,我什至从未尝试过 AJAX,所以如果这是一个完全菜鸟要问的问题,我深表歉意。

任何帮助或指导将不胜感激。谢谢!

好的,我的 Jquery 有这个:

$(document).ready(function() {  
   $('#Region').change(function() {
  // remove all options in Area select
 $('#Area').html('');
  // find the new Region selected
  var selected_region = $('#Region').val();
  // get new options from server and put them in your Area select
  $('#Area').get('Ajax/getArea.php?Region=' + selected_region);

});
});

这适用于我的 PHP:

<?php

// get province id passed in via `post` or `get`
$region = $_REQUEST['Region'];

// get the districts in that province
$query = "SELECT DISTINCT AREA FROM Sales_Execs WHERE Region ='$region'";

// link to your database
$link = mysqli_connect("localhost", "root", "", "Quality_Monitoring");

// execute your query
$result = mysqli_query($link, $query);

// parse the options
while($row = mysqli_fetch_assoc($result)) {
 $options = "<option value=\"".$row['AREA']."\">".$row['AREA']."</option>\n  ";
}


// send options
echo $options;

?>

仍然没有喜悦......谁能发现我错过了什么?

4

1 回答 1

0

试试这个,我在代码中包含了 3 个不同的部分:

1) the PHP code
2) The jQuery
3) The select box container

:: Your PHP file (call it getArea.php)
$selectbox = '<select name="region" onchange="jQuery.selectRegion(this.value)">';

$region = $_REQUEST['Region'];      /* Make sure you escape this */

$query = "SELECT DISTINCT AREA FROM Sales_Execs WHERE Region ='$region'";

$link = mysqli_connect("localhost", "root", "", "Quality_Monitoring");

$result = mysqli_query($link, $query);
$options = '';
while($row = mysqli_fetch_assoc($result)) {
    $options .= '<option value="' . $row['AREA'] . '">' . $row['AREA'] . '</option>';
}

echo $selectbox;
echo $options;
echo '</select>';
    exit;

:: Your jquery

jQuery.selectRegion = function selectRegion(regionId)
{
$.get('ajax/getArea.php?region='+regionId,function(data){
    $('#select_container').html(data);
});
}

:: The select box container
<div id="select_container">
<select name="region" onchange="jQuery.selectRegion(this.value)">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>
</div>

希望这可以帮助

于 2012-08-01T20:49:44.030 回答