douhuai4155 2015-08-08 22:41
浏览 55
已采纳

将变量从php文件返回到ajax

I am finding some difficulty to solve this problem. What I am trying to do is to pass a variable that is selected from a Database to javascript function.

At this line $('.form-container a.startStampleDiv').click(function(), I am trying to start a new query to the database using the selected data from the old one. So what I need is to return the selected costumerId from the database to the ajax so I can pass it through. I hope you guys understand my issue and try to help me out.

Note: this code is working for me, the only missing thing is to return variable from php to ajax.

Here is the js code:

$('#searchCostumer-btn').click(function(){
var phonenumber = $('#phonenumber').val();
var phonenumberReg = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/;
var phonenumberError = Boolean(true);

if (phonenumber==""){
    $('#phonenumber').css("border", "1px solid red");
    phonenumberError= false;
}
else if(!phonenumberReg.test(phonenumber)){
    $('#phonenumber').css("border", "1px solid red");
    phonenumberError= false;
}
else{
    $('#phonenumber').css("border", "1px solid white");
}
if(phonenumberError===true){
    $.ajax({
        type:'POST',
        data:{phonenumber: phonenumber},
        url: 'php/searchCostumer.php',
        fail: function(data){
            alert('An internal Error has been occured please contact adminstrator.');
        },
        success: function(data){
            document.getElementById("search-Costumer").reset();
            $('#search-result').html(data);



            // Event handlar click on costumer
            $('.form-container a.startStampleDiv').click(function(){
                document.getElementById('stample-container').style.display = 'block';
                // here i want to pass the costumerID to another php file or code. So what i need to do is to pass the costumerId that is returned by the php file...


            });  
        }
    });
}

});

and here is the php code:

<?php
ob_start();
session_start();
include("figaroStampledb.php");

if(isset($_POST['phonenumber']))
{
    $phonenumber = trim(htmlentities($_POST['phonenumber']));

    try {
        $con = new PDO("mysql:host=$sql_login_host;dbname=$sql_login_db", $sql_login_user, $sql_login_pass);

        $selectCostumers = $con->prepare("SELECT * FROM fCostumers WHERE phonenumber=:phonenumber");
        $selectCostumers ->execute(array(':phonenumber' => $phonenumber));
        $resultCostumers = $selectCostumers->fetchAll(PDO::FETCH_ASSOC);

        print json_encode($resultCostumers);

        $count=$selectCostumers->rowCount();

        if($count>0){
            print '<script src="js/validation.js" type="text/javascript"></script>';
            foreach ($resultCostumers as $costumer)
            {                   
                print '<div><i class="glyphicon glyphicon-user"> </i><a href="#stample-container" class="startStampleDiv">' ." " .$costumer['firstname'].'</a></div><hr>';
                //$_SESSION['$costumerId'] = $costumer['id'];
            }
        }
        else{
            print'Kunden är icke registrerad';
        }
    }
    catch(PDOException $e) {
        trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
    }
}

?>

  • 写回答

1条回答 默认 最新

  • dongzhun1857 2015-08-08 23:32
    关注

    You can add this line along with the print statement in your php script..

    <input type="hidden" id="custmid" value="$your_custmerid_variable">
    

    So ajax response will also add it in html but it won't make any changes to html because its hidden..

    Than get its value where you mentioned using $("#custmid").value() ; and further send it to php page where you want using another ajax request.... Hope you will figure out how to do that...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数
  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)
  • ¥100 使用matlab解决含分段变量的优化问题
  • ¥15 matlab基于鲸鱼算法优化vmd
  • ¥20 PCB设计与制作问答
  • ¥20 课堂跑如何下载ppt
  • ¥15 STM32阿里云平台显示不了传感器采集的数据
  • ¥20 三菱FX系列PLC上位机串口下指令置位M64和M65,这两条指令分别是什么呢?
  • ¥15 有关结冰模拟程序咨询