这是我的js页面和tcCheck.php页面。
<script>
function checkTC() {
$("#loaderTC").show();
jQuery.ajax({
url: "check/tcCheck.php",
data:'tc='+$("#tc").val(),
type: "POST",
success:function(data){
//WORKING CODE
document.getElementById('name').value = data;
//NEEDED CODE
document.getElementById('name').value = "NAME VARIABLE"
document.getElementById('phone').value = "PHONE VARIABLE"
},
error:function (){}
});
}
</script>
tcCheck.php
<?php
require_once("../config.php");
include("../girisbilgi.php");
$query = $link->query("SELECT * FROM musteri where musteriTC='" . $_POST["tc"] . "' and userSaID='".$userSaID."'");
if($veri =$query->fetch_object())
{
//WORKING CODE
echo $veri->musteriAD;
/NEEDED
$name = $veri->musteriAD;
$phone = $veri->musteriTel;
}
?>
tcCheck.php可以正常工作。如果我更改tcCheck.php结果,例如echo $ veri-> musteriAD,我可以更改输入。
但是我需要更改2个输入,因此我需要此变量。
如何在我的js页面中分配$ name和$ phone变量值?
最佳答案
返回一种可以用javascript轻松解析的格式-我会推荐json:
if($veri =$query->fetch_object()){
//set correct header
header('Content-Type: application/json');
//echo out json
echo json_encode(['name'=>$veri->musteriAD, 'phone'=>$veri->musteriTel]);
//done, stop processing
die();
}
javascript
success:function(data){
$('#name').val(data.name);
$('#phone').val(data.phone);
},