2016-09-26 79 views
-1

的HTML代碼凡在Ajax調用daterangepicker

<div class="col-md-4 input-group input-group-lg search"> 
    <span class="input-group-addon"><i class="fa fa-fw fa fa-calendar red"></i></span> 
    <input id="config" class="form-control placeholded" placeholder="Select the date to filter records" type="text" data-cid="<? echo $cid; ?>"> 
    </div> 

腳本代碼如何把這裏當前客戶端ID條款?

$(document).ready(function() { 

    $('.search i').click(function() { 
     $(this).parent().find('input').click(); 
    }); 
    updateConfig(); 

    function updateConfig() { 
     var options = {}; 
     // remove other code for space 
     $('#config').daterangepicker(options, function(start, end, label) { 
     var startDate = start.format('YYYY-MM-DD'); var endDate = end.format('YYYY-MM-DD'); 
     var cid = $(this).data("cid"); 
     passDate(startDate,endDate,cid); 
     }); 

    } 

    }); 

function passDate(startDate,endDate,cid) { 
$('.loader').show(); 
$.ajax({ 
     type: 'POST', // define the type of HTTP verb we want to use (POST for our form) 
     url: 'invoiceresult.php', // the url where we want to POST 
     data: 'startDate='+startDate+'&endDate='+endDate+'cid='+cid, // our data object 
    }) 
    // remove for space 
} 
} 

PHP的,這是在WHERE子句

if((!empty($_POST['startDate'])&&(!empty($_POST['endDate'])))) { // Check whether the date is empty   
    $startDate = date('Y-m-d',strtotime($_POST['startDate'])); 
    $endDate = date('Y-m-d',strtotime($_POST['endDate'])); 
    $cid = $_POST['cid']; 
    $sresult = $db->prepare('SELECT * FROM invoice WHERE (invoiceCreated BETWEEN "'.$startDate.'" and "'.$endDate.'") AND cid=:cidg'); // Execute the query 
    $sresult->execute(array(":cidg)=>$cid); 
    $num_rows = $sresult->fetchColumn(); //Check whether the result is 0 or greater than 0. 
    // remove other code for space 

SCREENSHOT enter image description here

這段代碼的結果是 「不確定的」 希望有人會幫。謝謝

+0

您正在搜索基於時間,哪裏是你的主網頁ID?時,將觸發什麼事件獲取基於ID的信息? – madalinivascu

+0

_「我只想獲得當前的client_id」_ - where _from_? – CBroe

+0

感謝您的回覆傢伙......它的截圖。 BTW。我只需要在ajax調用 –

回答

0

將數據屬性添加到一個元素,例如,輸入#config,用你的client_id。然後你可以在javascript中閱讀它並使用它的值。

HTML

<input id="config" class="form-control placeholded" placeholder="Select the date to filter records" type="text" data-client-id="client_id" />

JS

$('#config').daterangepicker(options, function(start, end, label) { var startDate = start.format('YYYY-MM-DD'); var endDate = end.format('YYYY-MM-DD'); passDate(startDate,endDate, document.getELementById("config").getAttribute("data-client-id")); });

+0

明白了,但是我需要在ajax調用數據中傳遞該ID:? $ .ajax({0}} {type:'POST',//定義我們想要使用的HTTP動詞的類型(POST爲我們的表單) url:'invoiceresult.php',//我們想要POST的url data:'startDate ='+ startDate +'&endDate ='+ endDate,//我們的數據對象 }) –

+0

是的。一種方式可能是數據:'startDate ='+ startDate +'&endDate ='+ endDate +'clientId ='+ client-id。另一種方法是使用像「var data = {startDate:startDate,endDate:endDate,clientId:client-id}」;而不是一個網址。 –

+0

我插入了這些代碼「passDate(startDate,endDate,document.getELementById(」config「)。getAttribute(」data-client-id「));」和「data:'startDate ='+ startDate +'&endDate ='+ endDate +'cid ='+ cid,」仍然不工作隊友...無論如何這裏的查詢:$ sresult = $ db-> prepare('SELECT * FROM invoice WHERE(invoiceCreated BETWEEN''。$ startDate。'「和」'。$ endDate。'「)AND cid =:cidg'); //執行查詢 $ sresult-> execute(array(「:cidg」=> $ cid)); –