vendredi 28 octobre 2016

use ajax object value as string stored in php variable for IF statement

I have radio group selects a user can choose from that get sent to my php file through ajax, (only the selected option from each group gets their value passed). I can $_POST() grab those values, save them in php variables and print_r() those variables successfully as so:

print_r() result ex: (in browser page)

Array ( [0] => Customer ) Array ( [0] => Completed Workorders ) Array ( [0] => All Workorders )

I am also able to use two of my other variables (from other checkbox values stored as an array) successfully in a MySQL WHERE clause.

Now, I'm trying to build a series of IF statements that say if the php variable = "[one of two value options from the radio group]" then $whereclause = "Some kind of value".

MY HTML CODE:

<label for="revenueCustomer">Customer</label>
<input type="radio" name="revenueTblType[]" id="revenueCustomer" value="Customer">

<label for="revenueCategory">Revenue Category</label>
<input type="radio" name="revenueTblType[]" id="revenueCategory" value="Revenue Category">

<label for="revenueCompletedWO">Completed Workorders</label>
<input type="radio" name="revenueWO[]" id="revenueCompletedWO" value="Completed Workorders">

<label for="revenueStatusWO">Workorder Status</label>
<input type="radio" name="revenueWO[]" id="revenueStatusWO" value="Workorder Status">

<label for="revenueAllWO">All Workorders</label>
<input type="radio" name="revenueWODate[]" id="revenueAllWO" value="All Workorders">

<label for="revenueDateRangeWO">Workorder Date Range</label>
<input type="radio" name="revenueWODate[]" id="revenueDateRangeWO" value="Workorder Date Range">

My $_POST() CODE:

//Get Table Type.
if (isset($_POST['revenueTblType'])) {
  $revenueTblType = $_POST['revenueTblType'];
  print_r($revenueTblType);
};

//Get Report Type
if (isset($_POST['revenueWO'])) {
  $revenueWO = $_POST['revenueWO'];
  print_r($revenueWO);
};

//Get date include value.
if (isset($_POST['revenueWODate'])) {
  $revenueWODate = $_POST['revenueWODate'];
  print_r($revenueWODate);
};

//Get date range.
$revenuefromajax=$_POST['revenuefrom'];
$revenuetoajax=$_POST['revenueto'];

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

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

//Get selected Status Values.
if (isset($_POST['revenue_checkboxes'])) {
  $revenue_check = $_POST['revenue_checkboxes'];
};

My IF statements:

if ($revenueTblType == 'Customer') {
        $groupbyclause = "x.company";
        $columnheader = 'Customer';
        $columnname = 'company';
    }
    else if ($revenueTblType == 'Revenue Category') {
        $groupbyclause = "x.revenue";
        $columnheader = 'Revenue Category';
        $columnname = 'revenue';
    }
    else {
        $groupbyclause = "x.revenue";
        $columnheader = 'Revenue Category';
        $columnname = 'revenue';
    }


    if (($revenueWO == 'Completed Workorders') and ($revenueWODate == 'All Workorders')) {
        $whereclause = "x.stagestatus = 'Complete'";
    }
    else if (($revenueWO == 'Completed Workorders') and ($revenueWODate == 'Workorder Date Range')) {
        $whereclause = "(x.stagestatus = 'Complete') AND (x.shippeddate BETWEEN '".$revenuefrom."' AND '".$revenueto."')"; 
    }
    else if ($revenueWO == 'Workorder Status') {
         $whereclause = "x.stagestatus IN (". implode(',', array_map(function($item) {return '"' . $item . '"'; }, $revenue_check)) .")";
    }
    else {
        $whereclause = "x.stagestatus = 'Complete'";
    }

When I run through this code, the result is always the "else" result even though the values for the selected buttons come through successfully.

Do I have to do something to the ajax results so I can use the sent values in my IF statements?

All help is welcome.

Thank you!!!

Aucun commentaire:

Enregistrer un commentaire