doujiaoben28596 2015-04-23 01:26
浏览 57
已采纳

从MySQL获取时在json中获取额外的值

I am trying to fetch data from ajax call via json and access the data after successful ajax call. For the purpose, I wrote the following code :

<?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    require_once('../include/functions.php');

    // session_start();

    if( isset($_POST['bike_id']))
    {
        $rows = array();
        // echo " I am in isset";
        $bike_id = $_POST['bike_id'];
        // $modal_name = $_POST['modal_name'];
        // $json['insertModal'] = false;

        // json_decode($modal_name);
        json_decode($bike_id);

        $result = selectBike_Modal($bike_id);

        while ( $row = mysqli_fetch_array($result))
        {
            $rows[] = $row;
        }
        echo json_encode($rows);
    }
    else
    {
        echo "bike_id is not set";
    }
?>

Now I want to access it via javascript :

$('#choose_bike').on('blur',
    function (e) {
        e.preventDefault();
            var bike_id = $('#choose_bike').val();
        //     var modal_name = $('#modal_name').val();
        //     // alert("bike_id is " + bike_id);
            alert("On blur is clicked.");
            if ( bike_id == "Select A Bike")
            {
                $('#defaultError').addClass("alert alert-danger");
                $('#defaultError').text("Please choose a Bike");

            }
            else
            {
                // alert("Bike is : " + bike_id);
                $.ajax(
                {
                    url:'../admin/backend/getModal.php',
                    type:'POST',
                    data: { bike_id: bike_id},
                    dataType:"json",

                    success:function(data)
                    {
                        alert(" i am in success");
                        var obj = JSON.parse(data);
                        alert("object is " +obj);
                    },
                    error : function() { console.log(arguments); }
                 }
               );
            }

    });

I have two issues :

  1. I am not getting anything after :

    alert(" i am in success");

Means the following is not working :

 var obj = JSON.parse(data);
 alert("object is " +obj);
  1. The php response is as follows:

[{"0":"5","bike_modal_id":"5","1":"shahjahan","bike_modal_name":"shahjahan","2":"30","bike_id":"30"},{"0":"6","bike_modal_id":"6","1":"ram","bike_modal_name":"ram","2":"30","bike_id":"30"}]

I am getting

"0":"5","bike_modal_id":"5"

extra. Please help me to parse the json correctly in javascript and getting only required ajax values.

Edit :

Select bike_model looks like :

function selectBike_Modal($bike_id)
    {
        include("connectvars.php");
        $query = "select * from bike_model where bike_id = '$bike_id'";
        $result = mysqli_query($dbc,$query)
        or die('error in fetching');

        // if ( mysqli_num_rows($result))
        //  return 0;
        // else
            return $result;
        // if ( () )
        // {
        //  return $result;
        // }
        // else
        // {
        //  return 0;
        // }

    }
  • 写回答

1条回答 默认 最新

  • dslh32311 2015-04-23 02:06
    关注

    issue 1
    Since you set dataType:"json" in your ajax properties, parsing your returned data with JSON.parse() is not needed, as your returned data is already an object. You can access the objects by simply doing data[0].bike_modal_name.

    issue 2
    Your issue of duplicate values, ie. "0":"5","bike_modal_id":"5" is because by default mysqli_fetch_array() fetches a result row both as an associative and a numeric array. You can either change

    while ( $row = mysqli_fetch_array($result)) 
    

    to

    while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    

    or to

    while ( $row = mysqli_fetch_assoc($result))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记