只显示ajax返回数组中的一个值

时间:2018-03-30 15:05:55

标签: javascript php jquery json ajax

如何只从数组中显示一个东西。现在它显示了整个数组,但我只想从数组中显示原因。因为其余部分非常重要。我添加了index.php来显示我在那里做的事情。我希望它只显示数据的原因。

test.js

public function getAllUsers(){
    $users = $this->fetchAll(null, 'company_name DESC');

    $result = array();
    foreach($users as $user){
        $userArray = $user->toArray();
        $result [$userArray['id']]= $userArray['company_name'];
    }

    return $result;
}

的index.php

$(document).ready(function() {

var date = "";
var begin = "";
var tijdsduur = "";
var aantal = "";


$('#datum').change(function() {
    date = $("#datum").val();
    console.log(date);
});
$('#beginTijd').change(function() {
    begin = ($(this).val());
    console.log(begin);
});
$('#Tijdsduur').change(function() {
    tijdsduur = ($(this).val());
    console.log(tijdsduur);
});
$('#aantalSloepen').change(function() {
    aantal = ($(this).val());
    console.log(aantal);
    $.ajax({
        type: "POST",
        url: "index.php",
        data: {
            date: date,
            begin: begin,
            eind: tijdsduur,
            quantity: aantal
        },
        success: function(data) {
            $('#testajax').html(data);
            console.log(data);
        }
    });
});
});

的console.log console.log

1 个答案:

答案 0 :(得分:0)

设置dataType:' json'在ajax请求中

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">

    $.ajax({
        type: "POST",
        url: "indextest.php",
        dataType: 'json',
        data: {
            date: '',
            begin: '',
            eind: '',
            quantity: ''
        },
        success: function(data) {
            console.log(data.date);
        }
    });

</script>

并在php中

<?php 

$data = array('date' => '10-10-2010','date2' => '10-10-2010');

echo json_encode($data);

 ?>