简体   繁体   English

结果是串联成功的ajax

[英]result is concatenating in ajax success

This is my ajax call code...I call this function on body onload. 这是我的ajax调用代码...我在body onload上调用此函数。

function get_booking_id() {


    var baseurl = "<?php echo base_url().'index.php/controller_bookplot2/getbookingid'; ?>";

    $.ajax({ 
        url: baseurl,
        datatype:"html",
        success: function(html) {

            alert('booking'+html);
            $("#booking_id").val(html);
        }
    });
}

And this is my controller function.. 这是我的控制器功能。

function getbookingid() {

    $sql="select IFNULL(max(booking_id),0) as bookingID from booking";
    $query=$this->db->query($sql);

    if($query->num_rows() > 0) {

        foreach($query->result() as $row) {
                echo $x=$row->bookingID;
                echo $x+1;
        }
    }
}

So in alert when in database there is no value, it display 1, but next time it display 12 instead of 2. So I think it concatenating the result. 因此,在警报中,当数据库中没有值时,它显示1,但是下次显示12而不是2。因此,我认为它将结果串联起来。 I want to only 2 as alert not 12. What am I doing wrong??? 我只想2个警报而不是12个。我在做什么错?

Please remove echo for $x=$row->bookingID; 请删除$ x = $ row-> bookingID的回显; if you don't need in response. 如果您不需要回应。

Updated controller function code: 更新了控制器功能代码:

function getbookingid(){

    $sql="select IFNULL(max(booking_id),0) as bookingID from booking";
    $query=$this->db->query($sql);

    if($query->num_rows() > 0){


    foreach($query->result() as $row){
        $x=$row->bookingID;
        echo $x+1;
    }
    }
   }

When is no value you are getting the value of echo $x+1;, (1) when you return a value you are getting echo $x=$row->bookingID; 当没有值时,您将得到echo $ x + 1;的值;(1)返回值时,您将得到echo $ x = $ row-> bookingID; (the value is 1) and echo $x+1; (值为1)并回显$ x + 1; (the value is 2, the $x who has the value of 1 plus 1 is 2), so in the script you get 12. (值是2,值1加1的$ x是2),因此在脚本中得到12。

You can try removing the one of the echos: 您可以尝试删除回声之一:

function getbookingid() {

    $sql="select IFNULL(max(booking_id),0) as bookingID from booking";
    $query=$this->db->query($sql);

    if($query->num_rows() > 0) {

        foreach($query->result() as $row) {
                $x = $row->bookingID;
                echo $x;
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM