当在php中调用结果时,通过ajax向php发送复选框值失败


Send checkbox values to php via ajax fails when calling results in php

我试图通过ajax将7个jquery复选框的值发送到php。我试图将值放在数组中,并在ajax中序列化数组。最后,我想使用选中的复选框的值作为MySQL WHERE子句的条件。当我尝试在php中使用结果时,我的代码失败了,并且该页中的表不会显示。但是,如果我注释掉php页面中的$_POST行,就会显示我的表。在浏览器中检查代码时,我得到一个500(内部服务器错误),该表应该显示在这里。我还让ajax显示错误结果,如下所示:

(对象对象)

代码:

我的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">

My Ajax Code:

    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);
                          }
                  });

My PHP Code:

    <?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和revenuefromdate。这个调用是成功的,我的表根据这些日期正确显示,只要ajax复选框的数据被注释掉。

注释2:抱歉,因为我是ajax的新手。

谢谢!

说明$_POST不正确。被序列化的是输入的名称/值。输入名称中的[]将被$_POST自动拾取为键revenue_checkboxes

的数组。

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

你获取数组 $revenuecheckboxes=$_POST('[name="revenue_checkboxes[]"]');的方式是无效的(这里不要用javascript的方式;D)它应该像$revenuecheckboxes=$_POST['revenue_checkboxes'];

php会自动发现它是一个数组,并将'revenuecheckboxes'初始化为数组,并使用它的值