使用ajax

时间:2016-08-09 11:46:37

标签: javascript php jquery ajax

这是我的 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-&gt; musteriAD; 但我需要更改2个输入,所以我需要这个变量。  如何在我的js页面中分配$ name和$ phone变量值?

1 个答案:

答案 0 :(得分:1)

返回一个你可以用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);
},