【问题标题】:How to get/pass a variable into ajax so the chart can show up correctly如何获取/传递一个变量到 ajax 以便图表可以正确显示
【发布时间】:2018-01-18 03:26:57
【问题描述】:

我有一个代码,可用于使用 chartjs 显示图表。

$test = $_REQUEST['pass_date'];     

$res = pg_query("SELECT d.date, count(i.booking_id) from (select to_char(date_trunc('day', (date '$test' + offs)), 'yyyy-mm-dd') as date from generate_series(0,6,1) as offs) d left outer join booking i on d.date=to_char(date_trunc('day', i.booking_from), 'yyyy-mm-dd') where product_name = 'EG' group by d.date order by d.date asc;");

//execute query
$result = pg_fetch_all($res);

//loop through the returned data
$data = array();
foreach ($result as $row) {
    $data[] = $row;
}   

print json_encode($data);

此代码用于从数据库中获取数据并显示日期并计算该日期的预订量。 pass_date 变量来自索引,使用 input 像这样

<form id="form1" method="post">
<div class="input_1">
    <input type="text" name="pass_date" >
    <span class="text-muted" style="font-size:10px">YYYY-MM-DD</span>                                
</div>
<input type="submit" onclick="submitForm('home-guards.php')" value="Home Guards" class="btn green" style="margin-top:20px"></input>
<input type="submit" onclick="submitForm('event-guards.php')" value="Event Guards" class="btn green" style="margin-top:20px"></input>

    <?php 
        if(isset($_POST['submit'])){
            $tfDate = $_POST['pass_date'];   
        }
        if(isset($tfDate)){ 
            echo $tfDate;
        }
    ?></form>

例如,如果我将 pass_date 插入“2017-08-20”,结果将是这样的

[{"date":"2017-08-20","count":"3"},{"date":"2017-08-21","count":"2"},{"日期":"2017-08-22","count":"1"},{"date":"2017-08-23","count":"1"},{"date":"2017- 08-24","count":"3"},{"date":"2017-08-25","count":"2"},{"date":"2017-08-26","计数":"3"}]

我有这样的 javascript 和 ajax 文件

$(document).ready(function(){
$.ajax({
    url : "data_chart_booking_daily_eg.php",
    type : "POST",      
    success : 
    function(data){
        console.log(data);

        var inv_date = [];
        var jumlah = [];            

        for(var i in data) {
            inv_date.push(data[i].date);
            jumlah.push(data[i].count);             
        }

如果我将 $test 更改为特定日期,例如“2017-08-20”,则图表运行良好,但如果我使用 $test,则图表不会出现所以现在我不知道如何获取/传递 $test 变量到 ajax 以便图表可以正确显示。谢谢你的帮助,对不起我的英语不好

【问题讨论】:

    标签: javascript php html ajax postgresql


    【解决方案1】:

    那是jQuery?您没有在 POST 中发送任何数据。

    首先,修改一下表单,使取值更容易。这里我在输入字段中添加了id="pass_date" 属性:

    <form id="form1" method="post">
    <div class="input_1">
        <input type="text" id="pass_date" name="pass_date" >
        <span class="text-muted" style="font-size:10px">YYYY-MM-DD</span>
    

    然后在您的 POST 中发送:

    $.ajax({
        url : "data_chart_booking_daily_eg.php",
        type : "POST",
        data: {pass_date: $('#pass_date').val()}, //this will get value of input field that
                                                  //has id="pass_date" and send
                                                  //as $_POST['pass_date']
        success : 
        function(data){
    

    【讨论】:

    • 谢谢你的回答,它有效。但我想要的是 pass_date 的值来自索引,所以当用户提供一些输入时,来自 pass_date 的值将是用户输入
    • 我已经更新了我的答案,包括从 HTML 表单输入框中检索值。
    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2020-10-01
    • 2021-11-23
    • 2017-11-28
    相关资源
    最近更新 更多