代码已更新
如果值在成功时从ajax传递到textbox,那么如何将该值与MySQL where子句一起使用?
Ajax代码:
var recstr1 = "Ramesh Kumar";
data={txtname:recstr1};
$.ajax({
type: 'POST',
url: 'getprojectnamerep.php',
data: data,
datatype: "html",
success: function(data){
console.log(data);
$("#txtname").html(data);
},
error: function(data){ alert("Something went wrong !"); }
});Mysql查询:
<?php $name = $_POST['txtname'];
select * from customer where custname = '$name';当我运行这段代码并检查Chrome浏览器中的"Elements“时,它显示的值如下:
<input type="text" id="txtname" class="form-control" readonly="" name="txtname">Ramesh Kumar</input>而它应该如下所示:
<input type="text" id="txtname" class="form-control" readonly="" name="txtname" value="Ramesh Kumar">由于值没有传入value='‘,这就是var_dump()返回空的原因。
我哪里错了,请指导和帮助?
getprojectnamerep.php
<?php session_start(); ?>
<?php
$rpname = $_POST['txtname'];
echo $rpname;
?>发布于 2018-03-06 15:12:16
更改一些js代码
var recstr1 = "Ramesh Kumar";
data={txtname:recstr1};
$.ajax({
type: 'POST',
url: 'getprojectnamerep.php',
data: data,
datatype: "html",
success: function(data){
console.log(data);
//$("#txtname").html(data);
**$("#txtname").val(data);**
},
error: function(data){ alert("Something went wrong !"); }
});发布于 2018-03-06 15:13:10
要设置文本框的值,需要使用val()
试试这个:
$("#txtname").val(data);发布于 2018-03-06 15:14:06
你应该使用
用$("#txtname").val(data);代替$("#txtname").html(data);
这就是问题所在。
var recstr1 = "Ramesh Kumar";
data={txtname:recstr1};
$.ajax({
type: 'POST',
url: 'getprojectnamerep.php',
data: data,
datatype: "html",
success: function(data){
console.log(data);
// $("#txtname").html(data);
$("#txtname").val(data);
},
error: function(data){ alert("Something went wrong !"); }
});https://stackoverflow.com/questions/49125194
复制相似问题