我正在尝试为我的AJAX Jquery PhP请求发送一个Get方法值。在将值发送到PHP文件之前,它的值是我期待的值,但是当php文件读取GET值时,无论我在组合框中选择什么,它都会一直变为1。
PHP文件(UI)
<!DOCTYPE html>
<?php include ('includes/header.php'); ?>
<navigation>
<div id="nav-bar">
<ul class="nav nav-pills nav-justified">
<li role="p_resentation"><a href="index.php">Home</a></li>
<li role="p_resentation" class="active"><a href="#">Sales</a></li>
<li role="p_resentation"><a href="manufacturing.php">Manufacturing</a></li>
</ul>
</div>
</navigation>
<br />
<div class="row" id="row1">
<div class="col-sm-8" id="row1-sm8">
<div class="panel panel-default">
<div class="panel-heading" id="content-heading">
<select name="users" onchange="getvalue(this.value)" class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<option value="">Select a field:</option>
<option value="1">Product Name</option>
<option value="2">SO #</option>
<option value="3">Assembly Name</option>
<option value="4">Sales Person</option>
<option value="5">Customer Name</option>
<option value="6">PO / Remarks</option>
<option value="7">SO Status</option>
<option value="8">Delivery Status</option>
</select>
</div>
<div>
<table class="table">
<div class="panel-body" id="output">
</div>
</table>
</div>
</div>
</div>
<div class="col-sm-4" id="row1-sm4">
<div class="panel panel-default">
<div class="panel-heading" id="login-heading">Filters</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-12">
<br/>
<table class="table">
<tr>
<td>Field</td>
<td>Value</td>
</tr>
</table>
</div>
</div><br />
</div>
</div>
</div>
</div>
<?php include ('includes/footer.php'); ?>
<script>
function getvalue(str) {
if (str == "") {
document.getElementById("output").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4) {
document.getElementById("output").innerHTML = this.responseText;
}
};
alert(str);
xmlhttp.open("GET","_db/getvalue.php?q="+str,true);
xmlhttp.send();
}
}
</script>
这是我的Receiver PHP文件,它将处理数据
<html>
<body>
<table class="table">
<?php
include('../includes/links/js.php');
include('../includes/links/css.php');
include('/connection/connect.php');
$val = intval($_GET['q']);
$field = '';
$group = '';
if ($val = 1) {
$field = "(SELECT pp1.name_template FROM product_product pp1 WHERE boml.product_id = pp1.id) as BOML";
$group = "BOML";
} elseif ($val = 2) {
$field = "so.name as SO";
$group = "SO";
} elseif ($val = 3) {
$field = "sol.name as assembly";
$group = "assembly";
} elseif ($val = 4) {
$field = "split_part(u.login, '@', 1) as sales_person";
$group = "sales_person";
} elseif ($val = 5) {
$field = "c.display_name as Customer";
$group = "Customer";
} elseif ($val = 6) {
$field = "so.note as PO";
$group = "PO";
} elseif ($val = 7) {
$field = "so.state as Status";
$group = "Status";
} elseif ($val = 8) {
$field = "COALESCE(del.state, 'Waiting Availability') del_status";
$group = "del_status";
}
echo "$field";
echo "$val";
$result = pg_query("
SELECT ".$field."
FROM sale_order so
INNER JOIN sale_order_line sol
ON so.id = sol.order_id
INNER JOIN res_partner as c
ON so.partner_id = c.id
LEFT JOIN account_payment_term as ap
ON so.payment_term_id = ap.id
INNER JOIN product_uom as pu
ON sol.product_uom = pu.id
LEFT JOIN account_tax_sale_order_line_rel as acs
ON sol.id = acs.sale_order_line_id
LEFT JOIN account_tax as at
ON acs.account_tax_id = at.id
INNER JOIN product_template as pt
ON sol.product_id = pt.id
LEFT JOIN mrp_bom as bom
ON pt.id = bom.product_tmpl_id
LEFT JOIN mrp_bom_line as boml
ON bom.id = boml.bom_id
INNER JOIN res_users as u
On so.create_uid = u.id
Left Join (Select sp1.id, sp1.origin, sp1.state, spo1.qty_done, spo1.product_id, spo1.picking_id, sp1.date_done
From stock_picking as sp1
Full Join stock_pack_operation as spo1
On sp1.id = spo1.picking_id) as del
On so.name = del.origin
And sol.product_id = del.product_id
GROUP BY ".$group."
");
while($row = pg_fetch_row($result)){
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
答案 0 :(得分:1)
在PHP中,“=”是赋值而不是比较运算符(对于类型敏感的兄弟,“==”或“===”)。
一旦调用了第一个if
,您的实际值就会被覆盖。
您可能还会考虑PHP的switch ... case
结构,该结构专为此类情况而设计,并且仍能很好地处理字符串数据。
尝试:
<html>
<body>
<table class="table">
<?php
include('../includes/links/js.php');
include('../includes/links/css.php');
include('/connection/connect.php');
$val = intval($_GET['q']);
$field = '';
$group = '';
if ($val == 1) {
$field = "(SELECT pp1.name_template FROM product_product pp1 WHERE boml.product_id = pp1.id) as BOML";
$group = "BOML";
} elseif ($val == 2) {
$field = "so.name as SO";
$group = "SO";
} elseif ($val == 3) {
$field = "sol.name as assembly";
$group = "assembly";
} elseif ($val == 4) {
$field = "split_part(u.login, '@', 1) as sales_person";
$group = "sales_person";
} elseif ($val == 5) {
$field = "c.display_name as Customer";
$group = "Customer";
} elseif ($val == 6) {
$field = "so.note as PO";
$group = "PO";
} elseif ($val == 7) {
$field = "so.state as Status";
$group = "Status";
} elseif ($val == 8) {
$field = "COALESCE(del.state, 'Waiting Availability') del_status";
$group = "del_status";
}
echo "$field";
echo "$val";
$result = pg_query("
SELECT ".$field."
FROM sale_order so
INNER JOIN sale_order_line sol
ON so.id = sol.order_id
INNER JOIN res_partner as c
ON so.partner_id = c.id
LEFT JOIN account_payment_term as ap
ON so.payment_term_id = ap.id
INNER JOIN product_uom as pu
ON sol.product_uom = pu.id
LEFT JOIN account_tax_sale_order_line_rel as acs
ON sol.id = acs.sale_order_line_id
LEFT JOIN account_tax as at
ON acs.account_tax_id = at.id
INNER JOIN product_template as pt
ON sol.product_id = pt.id
LEFT JOIN mrp_bom as bom
ON pt.id = bom.product_tmpl_id
LEFT JOIN mrp_bom_line as boml
ON bom.id = boml.bom_id
INNER JOIN res_users as u
On so.create_uid = u.id
Left Join (Select sp1.id, sp1.origin, sp1.state, spo1.qty_done, spo1.product_id, spo1.picking_id, sp1.date_done
From stock_picking as sp1
Full Join stock_pack_operation as spo1
On sp1.id = spo1.picking_id) as del
On so.name = del.origin
And sol.product_id = del.product_id
GROUP BY ".$group."
");
while($row = pg_fetch_row($result)){
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>