2016-10-12 46 views
0

我想通過ajax將7個jquery複選框的值發送到php。我正在嘗試將值放入一個數組,並在ajax中序列化數組。最終,我想使用選中複選框的值,因爲條件是MySQL WHERE子句。當我嘗試在php中使用結果時,我的代碼失敗,並且該頁面中的表不會顯示。但是,如果我在該php頁面中註釋$ _POST行,我的表格會顯示。當在瀏覽器中檢查代碼時,我會在表中顯示500(內部服務器錯誤)。我也有AJAX顯示這是如下的錯誤結果:當通過ajax發送複選框值到php時,調用結果失敗php

[對象的對象]

CODE:

我的HTML代碼:

<label for="prestage_select">Prestage</label> 
    <input type="checkbox" name="revenue_checkboxes[]" id="prestage_select" value="Prestage"> 


    <label for="validation_select">Validation</label> 
    <input type="checkbox" name="revenue_checkboxes[]" id="validation_select" value="Validation"> 


    <label for="scheduling_select">Scheduling</label> 
    <input type="checkbox" name="revenue_checkboxes[]" id="scheduling_select" value="Scheduling"> 


    <label for="production_select">Production</label> 
    <input type="checkbox" name="revenue_checkboxes[]" id="production_select" value="Production"> 


    <label for="needsBOL_select">Needs BOL</label> 
    <input type="checkbox" name="revenue_checkboxes[]" id="needsBOL_select" value="Needs BOL"> 


    <label for="shpAcct2Close_select">Shipped: Account to Close</label> 
    <input type="checkbox" name="revenue_checkboxes[]" id="shpAcct2Close_select" value="Shipped: Acctg. To Close Out"> 


    <label for="movedToComplete_select">Moved to Complete for Selected Period</label> 
    <input type="checkbox" name="revenue_checkboxes[]" id="movedToComplete_select" value="Complete"> 

我的Ajax代碼:

j("#create_submit").click(function(){ 

      //Send Revenue Status Values to php using ajax.  
       j.ajax({ 
        method: 'POST', 
        url: 'revenue_report.php', 
        data: j('[name="revenue_checkboxes[]"]').serialize(), 
        success: function(response) { 
          j('#fieldset_ReportDiv').html(response); 
          } 
        }); 


     //send Revenue Date values to php using ajax. 
        var revenuefrom = j('#revenuefrom').val(); 
        var revenueto = j('#revenueto').val(); 
        j.ajax ({ 
         method: 'POST', 
         url: "revenue_report.php", 
         data: { revenuefromtext: revenuefrom, revenuetotext: revenueto }, 
         success: function(response) { 
          j('#fieldset_ReportDiv').html(response); 
          } 
        }); 

我的PHP代碼:

<?php 

    include('inc.php'); 


    //Get date range. 

    $revenuefromajax=$_POST['revenuefromtext']; 
    $revenuetoajax=$_POST['revenuetotext']; 

    $revenuefromstring = strtotime($revenuefromajax); 
    $revenuetostring = strtotime($revenuetoajax); 

    $revenuefrom=date("Y-m-d", $revenuefromstring); 
    $revenueto=date("Y-m-d", $revenuetostring); 


    //Get selected Status Values. 

    $revenuecheckboxes=$_POST('revenue_checkboxes'); //the page loads properly when this line is commented out. As soon as I remove the comment, it breaks. 





    //connect to the database 
    $conn = new mysqli($servername, $username, $password, $dbname); 

    // Check connection 
    if(mysqli_connect_errno()) { 
     printf('Could not connect: ' . mysqli_connect_error()); 
     exit(); 
    } 

    //echo 'MySQL Connected successfully.'."<BR>"; 


    $conn->select_db("some database name"); /////Database name has been changed for security reasons///////// 

    if(! $conn->select_db('some database name')) { 

     echo 'Could not select database. '."<BR>"; 
    } 

    // echo 'Successfully selected database. '."<BR>"; 

    //Select Data and Display it in a table. 


    $sql = "SELECT invoices.id, invoices.orderdate, invoices.stagestatus, FORMAT(TRIM(LEADING '$' FROM invoices.totalprice), 2) AS totalprice, clients.company, lineitems.invoiceid, FORMAT((lineitems.width * lineitems.height) /144, 2) AS sqft, lineitems.quantity AS qty, FORMAT((invoices.totalprice/((lineitems.width * lineitems.height) /144)), 2) as avgsqftrevenue, FORMAT((TRIM(LEADING '$' FROM invoices.totalprice)/lineitems.quantity), 2) AS avgunitrevenue 
    FROM clients 
    INNER JOIN invoices ON clients.id = invoices.clientid 
    INNER JOIN lineitems ON invoices.id = lineitems.invoiceid 
    WHERE invoices.orderdate BETWEEN '".$revenuefrom."' AND '".$revenueto."' 
    ORDER BY invoices.id DESC"; 


    $result = $conn->query($sql); 


    echo "<table id='revenueReportA' align='center' class='report_DT'> 
    <tr> 

    <th>Customer</th> 
    <th>SG</th> 
    <th>Revenue</th> 
    <th>SQ FT</th> 
    <th>AVG Revenue Per SQ FT</th> 
    <th>Number of Units</th> 
    <th>AVG Revenue Per Unit</th> 
    </tr>"; 


    if ($result = $conn->query($sql)) { 

     // fetch associative array 
     while ($row = $result->fetch_assoc()) { 


     echo "<tr>"; 
     echo "<td>" . $row['company'] . "</td>"; 
     echo "<td>" . $row['id'] . "</td>"; 
     echo "<td>" ."$". $row['totalprice'] . "</td>"; 
     echo "<td>" . $row['sqft'] ."&nbsp;&nbsp;". "ft<sup>2</sup>". "</td>"; 
     echo "<td>" ."$". $row['avgsqftrevenue'] . "</td>"; 
     echo "<td>" . $row['qty'] . "</td>"; 
     echo "<td>" ."$". $row['avgunitrevenue'] . "</td>"; 
     echo "</tr>"; 
     } 

      echo "</table>"; 

     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 



    //Free the result variable. 
    $result->free(); 

    } 


    //Close the Database connection. 
    $conn->close(); 


    ?> 

我已經嘗試了幾種不同的建議將值發送到php,但ajax保持失敗。

注意:我包括其他ajax調用revenueto和revenuefrom日期。這個調用是成功的,只要複選框ajax的數據被註釋掉,我的表就可以基於這些日期正確顯示。

注2:道歉,因爲我是新來的ajax。

謝謝!

回答

1

$_POST是不正確的。序列化的是輸入的名稱/值。在輸入名稱的[]將被自動地拾起$_POST爲數組的關鍵revenue_checkboxes

嘗試

$revenuecheckboxes = $_POST['revenue_checkboxes'];// should be an array of the checked checkboxes 
+0

有沒有人有任何其他建議的Ajax代碼? – rdimouro

+0

有什麼具體的建議? – charlietfl

+0

要查看是否有更好的方式發送信息或查看問題是否在嘗試創建初始數組? – rdimouro

0

你獲得陣列$revenuecheckboxes=$_POST('[name="revenue_checkboxes[]"]');的方法是無效的(不要去與這裏的JavaScript的方式; D)它應該爲$revenuecheckboxes=$_POST['revenue_checkboxes'];

PHP會自動發現它的數組,並將初始化'收支檢查框'與其陣列值

+0

我試着更改代碼段,但我仍然遇到同樣的問題。當我嘗試在瀏覽器中預覽響應時,它說它無法預覽響應,並且當我測試URL時,頁面已損壞。我認爲ajax調用本身可能存在問題,或者它的問題嘗試使複選框正確排列。但是,我對此不夠了解。 – rdimouro

+0

你在迴應什麼是你的迴應不明確在這裏...可能會在您的代碼後面的東西導致問題..go和編輯您的問題與您的PHP代碼以及... – RohitS