2016-04-22 28 views
1

用戶選擇日期,月份和年份,然後按一個按鈕以顯示基於他選擇的日期的數據庫值 我不知道代碼中的問題在哪裏php基於onclick函數的輸入類型日期

<style> 
.button {border-radius: 8px;} 
.button1 {font-size: 20px;} 
</style> 
<form> 

إختر التاريخ : <input type="date" name="bday" id="dat"><br> 

    <button type=button class="button button1" onclick="document.getElementById('data').innerHTML = myFunction(document.getElementById('dat').value)">إظهار الإستهلاك اليومي</button> 
<hr> 
<span id="data" ></span> 
</form> 

<script>// <![CDATA[ 
    function myFunction($x) { 
     $current_user=get_current_user_id();// to fetch the right data for the right user 

    // ياخذ اليوم والشهر والسنه الي اختارها اليوزر 
global $wpdb; 
$d=date('j');//define variable to store the day 
$m=date('m');//define variable to store the month 
$y=date('Y');//define variable to store the year 
$daily_amount= $wpdb->get_var("SELECT daily_amount FROM arduino_period_use where ID=$current_user and day=d and month=$m and year=$y "); 
//print_r($daily_amount); 
     } 

// ]]></script 
+0

我覺得你是如此的困惑PHP(服務器端腳本)和JavaScript(客戶端腳本)也許研究一些PHP - JS - 阿賈克斯 –

+0

在您的查詢,'天= d',應該是'天= $ d' –

+0

我使用WordPress – user6159635

回答

1

或許,如果你有一些錯誤檢查,你會得到告訴你的SQL語法錯誤:

$daily_amount= $wpdb->get_var("[..snip..] and day=d and month=$m and year=$y "); 
                ^---- missing $ 

你不可能在你的DB一個d場,所以查詢將失敗未知的現場錯誤。

+0

我修改$ d,並試圖再一次沒成功代碼 – user6159635

+0

,那麼你需要解釋如何** **此代碼不起作用。 –

+0

我選擇的日期,但當我點擊按鈕不顯示結果 – user6159635

0

問題是PHP是一種服務器端語言,並不像JavaScript那樣以完全相同的方式操作,它看起來好像你有兩個困惑的東西。

查找到Ajax請求使用JavaScript和服務器端PHP代碼進行交流訪問數據庫記錄

+0

我要問的問題 – user6159635

+0

當用戶選擇日期可以將日期存儲在$ d中,將月份存儲在$ m中並將年份存儲在$ y中 – user6159635