使用ajax对象值作为存储在php变量中的字符串,用于IF语句

时间:2016-10-28 18:54:39

标签: php ajax if-statement

我有无线电组选择用户可以选择通过ajax发送到我的php文件,(只有每个组中的选定选项获得其值传递)。我可以$ _POST()获取这些值,将它们保存在php变量中,并将这些变量成功地保存在print_r()中:

print_r()结果ex :(在浏览器页面中)

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

我还可以在MySQL WHERE子句中成功使用我的其他两个变量(来自存储为数组的其他复选框值)。

现在,我试图构建一系列IF语句,说明php变量=" [来自无线电组的两个值选项之一]"那么$ whereclause ="某种价值"。

我的HTML代码:

// first radio group: user must select one or the other.
<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">



// Second radio group: user must select one or the other.
<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">



// Third radio group: user must select one or the other.
<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">

我的$ _POST()代码:

//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'];
};

我的IF声明:

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'";
    }

当我运行此代码时,结果始终是&#34; else&#34;结果即使所选按钮的值成功通过。

我是否必须对ajax结果执行某些操作,以便在IF语句中使用已发送的值?

欢迎所有帮助。

谢谢!!!

1 个答案:

答案 0 :(得分:0)

经过一些简单的编辑后,我能够成功执行我的代码。对于那些寻求类似问题帮助的人来说,这对我有用:

以前,我的收音机选择组名称为&#34; revenueTblType []&#34;,&#34; revenueWO []&#34;和&#34; revenueWODate []&#34;。但是,(愚蠢的我)无线电选择组不需要名称中的数组区别像复选框一样,因为只发送一个值(因为组中只能选择一个无线电选项。通过删除&#34; [ ]&#34;从每个名字,我的ajax能够带来每个值,而不是在数组中,我的if语句能够成功地测试条件。

此外,我必须做出的另一项改变是删除决赛&#34;否则&#34;每个if语句的条件。在测试期间,我会看到我的第一个表构建基于if条件。但是,一旦程序达到我不需要的其他条件,我的表就会改变。删除额外的代码后,显示正确的表格。

我的代码:

HTML:

// first radio group: user must select one or the other.
<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">



// Second radio group: user must select one or the other.
<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">



// Third radio group: user must select one or the other.
<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">

IF声明:

if ($revenueTblType == 'Customer') {
    $groupbyclause = "x.company";
    $columnheader = 'Customer';
    $columnname = 'company';
}
else if ($revenueTblType == 'Revenue Category') {
    $groupbyclause = "x.revenue";
    $columnheader = 'Revenue Category';
    $columnname = 'revenue';
}
/* Removed this section of code...
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)) .")";
}
/* Removed this section of code...
else {
    $whereclause = "x.stagestatus = 'Complete'";
}
*/

我希望这也有助于其他人!

谢谢。