gpt4 book ai didi

php - 根据下拉列表中选定的值填充 '<input>' 值

转载 作者:行者123 更新时间:2023-12-01 02:28:56 25 4
gpt4 key购买 nike

我正在尝试显示 <input>值基于使用 jquery ajax 的下拉列表的选定值。

这是我的表单的标记 -

<div class="form-group">
<label for="" class="control-label">District</label>
<div class="element">
<select class="form-control" id="district">
<?php echo $option; ?>
</select>
</div>
</div>

<div class="form-group">
<label for="" class="control-label">Province</label>
<div class="element">
<input type="text" class="form-control province" placeholder="Province" value="" disabled>
</div>
</div>

只是我需要在从上面的下拉列表中选择一个地区时显示省份输入的值。

这是我的 ajax 代码 -

$("#district").change(function(event) {
var id = $("#district").val();
//alert(data);

/* Send the data using post and put the results in a div */
$.ajax({
url: "./includes/process_province.php",
type: "post",
data: id,
success: function(){
alert('Submitted successfully');
},
error:function(){
alert("failure");
}
});

// Prevent default posting of form
event.preventDefault();
});

当我从下拉列表中选择一个地区时,它会出现此警报 alert('Submitted successfully'); 。但我不知道如何在我的文本字段中显示 PHP 处理值。

这是我的PHP代码来自proccess_province.php

// Require the configuration file before any PHP code:
require('./configuration.inc.php');

// Need the database connection:
require('../'.MYDB);

if(isset($_POST['id'])) {

$province_id = (int)$_POST['id'];

// Select exsiting data for an user and display them in the form for modify
$prep_stmt = " SELECT province
FROM province
WHERE province_id = ?";

$stmt = $mysqli->prepare($prep_stmt);

if ($stmt) {
// Bind "$userId" to parameter.
$stmt->bind_param('i', $province_id);
// Execute the prepared query.
$stmt->execute();
$stmt->store_result();

// get variables from result.
$stmt->bind_result($province);
// Fetch all the records:
$stmt->fetch();

$db_province = filter_var($province, FILTER_SANITIZE_STRING);
//echo $province;

} else {
echo 'Database error';
}

echo $db_province;
}

希望有人能帮助我。谢谢。

最佳答案

由于您在 ajax 文件中回显了省份的值,因此您需要根据该响应设置该值。试试这个:

     $.ajax({
url: "./includes/process_province.php",
type: "post",
data: id,
success: function(data){
$('#province').val(data);
alert('Submitted successfully');
},
error:function(){
alert("failure");
}
});

还有你的标记:

<div class="form-group">
<label for="" class="control-label">Province</label>
<div class="element">
<input id="province" type="text" class="form-control province" placeholder="Province" value="">
</div>
</div>

关于php - 根据下拉列表中选定的值填充 '&lt;input&gt;' 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28231729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com