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...

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能