通过ajax将复选框值发送到php导致null

时间:2016-10-14 13:44:54

标签: php jquery ajax

我试图通过ajax将7个jquery复选框的值发送到php。我试图将值放在一个数组中并在ajax中序列化该数组。最后,我想使用复选框的值作为MySQL WHERE子句中的条件。我的ajax成功完成,但无论我尝试使用什么方法,数组的值总是为null。

代码: 注意:我已更新此处的代码,以反映答案中提供的建议修改。

我的HTML代码:

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


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


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


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


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


<label for="shpAcct2Close_select">Shipped: Account to Close</label>
<input type="checkbox" name="revenue_checkboxes[]" id="shpAcct2Close_select" class="revenuechbxs" 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" class="revenuechbxs" value="Complete">

我的Ajax代码:

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

    //send Revenue Data values to php using ajax.
                  var revenuechbxarray = j('.revenuechbxs:checked').val();
                  var revenuefrom = j('#revenuefrom').val();
                  var revenueto = j('#revenueto').val();

                  j.ajax ({
                      method: 'POST',
                      url: "revenue_report.php",
                      data: { revenuefromtext: revenuefrom, revenuetotext: revenueto, revenuechbx: revenuechbxarray },
                      success: function( response ) {
                          j('#fieldset_ReportDiv').html(response);
                          }
                  });

             console.log(revenuechbxarray);

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

$revenue_check = $_POST['revenuechbx']; //
print_r($revenue_check); //displays result of one checkbox (the first selected on) but not more than one...


//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,但值始终为null。

注意:我包含了另一个针对revenueto的ajax调用和来自日期的收入。此调用成功,我的表根据这些日期正确显示。我似乎无法从与日期相同的页面中获取所选复选框的实际值。

3 个答案:

答案 0 :(得分:2)

当您的复选框没有类属性时,您正在使用类选择器

答案 1 :(得分:0)

您的代码中的一些变化

  

html代码

 <input class="revenue" type="checkbox" name="revenue_checkboxes[]" id="production_select" value="Production">

设置复选框名称数组,并按javascript代码添加公共类收入添加和选择用户

  

Ajax代码

var revenuechbxarray = $('.revenue:checked').val();
  

PHP代码:

$revenue_check = $_POST['revenuechbx']; // this is array

您可以使用此数组json_encode转换json字符串并设置数据库。

答案 2 :(得分:0)

按名称选择复选框:

j('[name=revenue_checkboxes]:checked').serialize()