我使用ajax在数据库中搜索值并填充下拉列表。这是我的ajax代码:
$output = '' ;
if (isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
$st = $_POST['st'];
$query = mysqli_query($link, "SELECT DISTINCT title FROM `wp_locations` WHERE state='".$st."' AND `title` LIKE '%".$searchq."%' LIMIT 10")or die("Could not search!");
if (!mysqli_query($link,$query))
{
echo("Error description: " . mysqli_error($link));
}
$count = mysqli_num_rows($query);
if($count == 0){
$output = '<option>No results!</option>';
}else{
while($row = mysqli_fetch_array($query)){
$suburb = $row['title'];
$postcode = $row['postcode'];
?>
<option value="<?php echo $suburb; ?>"><?php echo $suburb; ?> </option>
<?php
} // while
} // else
} // main if
在这里,我获取用户输入的区域并在数据库中搜索该区域中的郊区。这是我的PHP代码:
postcode
我想要做的是,将Suburb:* <input type="text" name="suburb" list="sbb" required size="30" value="<?php echo $suburb; ?>" onkeyup="searchq6()" id="output">
<datalist id="sbb" name="taskoption6" >
<option> </option>
</datalist>
Postcode:* <input type="number" name="postcode" required value="<?php echo $postcode; ?>" id="postcode">
值发送到另一个输入框。这就是我的html外观
$("#postcode").html(postcode);
只需添加public static void test(int x)
{
if (x > 1)
{
Console.WriteLine(x);
}
}
即可。如何从php文件中发送值?
答案 0 :(得分:0)
只需创建一个包含三个值的数组。 1.成功是真是假 2.如果成功则返回值的数据数组否则为null 3.成功价值为假的错误原因。
因此,您的一个错误结果将是:
$result = ['success' => false, 'data' => null, 'error' => 'error connecting db'];
您的成功结果将是:
$result = ['success'=> true, 'data'=> ['suburbs'=> $suburbs, 'pincode' => $pincode], 'error' => null];
现在将此结果数组发送为json。
echo json_encode($result);
然后解析comment by @MistahFiggins并填充你所在的reult。
答案 1 :(得分:0)
在PHP文件中:
// Output is array, will be send as JSON data
$output = array("suburbs"=>'', "postcodes"=>array());
if (isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
$st = $_POST['st'];
$query = mysqli_query($link, "SELECT DISTINCT title FROM `wp_locations` WHERE state='".$st."' AND `title` LIKE '%".$searchq."%' LIMIT 10")or die("Could not search!");
if (!mysqli_query($link,$query)) {
echo("Error description: " . mysqli_error($link));
}
$count = mysqli_num_rows($query);
if($count == 0){
$output["suburbs"] = '<option>No results!</option>';
}else{
while($row = mysqli_fetch_array($query)){
$suburb = $row['title'];
$postcode = $row['postcode'];
$output["suburbs"] .= "<option value=\"$suburb\">$suburb </option>";
$output["postcodes"][$suburb] = $postcode;
} // while
} // else
// Send result
echo json_encode($output);
} // main if
在HTML的AJAX部分:
// Make global variable that holds POSTCODES:
var postcodes = {};
function searchq6(){
var searchstate = $("input[name='region']").val();
var searchTxt = $("input[name='suburb']").val();
$.post("search-suburb.php", {searchVal: searchTxt, st:searchstate})
.done(function(response) {
var responseObj = jQuery.parseJSON(response);
$("#sbb").html(responseObj["suburbs"]);
postcodes = responseObj["postcodes"];
});
}
在HTML中:
Suburb:* <input type="text" onselect="change_post(this)" onchange="change_post(this)" name="suburb" list="sbb" required size="30" value="<?php echo $suburb; ?>" onkeyup="searchq6()" id="output">
<datalist id="sbb" name="taskoption6" >
<option> </option>
</datalist>
Postcode:* <input type="number" name="postcode" required value="<?php echo $postcode; ?>" id="postcode">
<script>
//var postcodes = { "1":"First", "2":"Second", "3":"Third", "4":"Fourth" };
function change_post(out) {
//console.log(postcodes[ out.value ]!=undefined);
if ( postcodes[ out.value ]!=undefined )
document.getElementById("postcode").innerHTML = postcodes[ out.value ];
}
</script>