【发布时间】:2019-06-12 10:58:36
【问题描述】:
我想使用 Ajax 从文本输入中发送数据并将结果(来自数据库)设置到另一个输入中
我尝试使用 json_encode() 将结果解析为 json,由于某种原因它不起作用,作为解决方案,我在结果 php 页面中创建了一个 div,我得到了带有内容的 div
index.php
<div class="form-group col-4">
<label for=""> Code:<span style="color:red;">*</span></label>
<input type="text" class="form-control form-control-sm" id="codecli" name="codecli" onkeyup="showinfoclient()" required>
</div>
<div class="form-group col-8 " id="mess">
</div>
</div>
<div class="minfoclient">
<div class="form-row">
<div class="form-group col-6">
<label for=""> Lastname:<span style="color:red;">*</span></label>
<input type="text" class="form-control form-control-sm" id="lastname" name="lastname" value="" required>
</div>
<div class="form-group col-6">
<label for=""> Firstname:<span style="color:red;">*</span></label>
<input type="text" class="form-control form-control-sm" id="firstname" name="firstname" value="" required>
</div>
</div>
client.php if ($_REQUEST['idcli']) {
$idcli = strip_tags($_POST['idcli']);
$iduser = $_SESSION['user_id'];
$clients = $conn->query("SELECT * FROM `client` WHERE
`Current_user`=$iduser AND `id_client`='$idcli'");
while($client = $clients->fetch_assoc()){
?>
<div class="minfoclient">
<div class="form-row">
<div class="form-group col-6">
<label for=""> Lastname:<span style="color:red;">*</span></label>
<input type="text" id="nom" class="form-control form-control-sm namm" name="lastname" value="<?=$client['lastname'];?>">
</div>
<div class="form-group col-6">
<label for=""> Firstname:<span style="color:red;">*</span></label>
<input type="text" id="prenom" class="form-control form-control-sm" name="firstname" value="<?=$client['firstname'];?>">
</div>
</div> ...
此代码有效,但没有异常,因为它阻止了其他任务
function showeinfoclient(){
var iDClient = $('#codecli').val();
if (iDClient) {
$.ajax({
type:'POST',
url:'client.php',
data:'idcli='+iDClient,
success:function(data){
$('.minfoclient').html(data);
}
});
}
}
作为解决方案,我希望您提供一个简单的代码(AJAX、JSON 和 PHP)
【问题讨论】:
-
将
data传递为data: { idcli : iDClient}, -
@DevsiOdedra - OP 的做法是完全有效的(只要
iDClient中没有奇怪的字符) -
我实际上并没有真正明白你想要做什么。
client.php在哪里,它包含/输出什么?此外,您需要解释尝试代码时实际发生的情况。只是说“它没有用”并没有给我们任何继续。 -
请编辑您的问题以包含所有相关代码(包括您尝试解释实际发生的情况)。此外,您向我们展示了一些 HTML,然后向我们展示了
result.php,而您的 ajax 代码调用了client.php。我们需要正确解释您的代码、结构、问题和预期结果。 -
我编写的代码显示了 client.php 包含的所有内容 div 包含来自 sql 的数据 这个 div 替换了 index.php 中的 div 我想用 json_encode 替换这个代码