0

I am trying to send the values of 7 jquery checkboxes to php via ajax. I am attempting to put the values in an array and serialize the array in ajax. Ultimately, I would like to use the values of the checkboxes as conditions in a MySQL WHERE clause. My ajax completes successfully but the value of the array is always null no matter what method I try using.

CODE: note: I've updated the code on here to reflect the suggested edits provided in the answers.

My HTML code:

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

My Ajax Code:

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

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.

$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(); 


?>

I have tried several different suggestions for sending the values to php but the value is always null.

Note: I included the other ajax call for revenueto and revenuefrom date. This call is successful and my table displays correctly based off those dates. I just can't seem the get actual values for my selected checkboxes from the same page as the dates.

2
  • Can you log serialized string revenuechbxarray? Commented Oct 14, 2016 at 13:47
  • I see 2 ajax calls, the first one where you send your checkboxes data doesnt have a success function Commented Oct 14, 2016 at 13:47

3 Answers 3

2

You are using a class selector while your checkboxes does not have class attributes

Sign up to request clarification or add additional context in comments.

Comments

0

some changes in your code step by step

html code

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

set checkbox name array and add common class revenue add and selecter user by javascript code

Ajax code

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

PHP code:

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

you can user this array json_encode to convert json string and set database.

1 Comment

thank you! I am able to see the value of my first checkbox in console.log() now. However, I can't see other checkboxes. Just one. Will json_encode allow me to get and use the values of each checkbox my my MySQL code? If so, can you give an example of json_encode?
0

To select the checkboxes by name:

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

1 Comment

Please add some explanation of why this code helps the OP. This will help provide an answer future viewers can learn from. See How to Answer for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.