Siege21x Siege21x - 12 days ago 4
Javascript Question

PHP AJAX JQuery xmlhttp request Get method sends always 1

I am trying to send a Get method value for my AJAX Jquery PhP request. Before the value be sent to the PHP file it's value was the one i am expecting but by the time the php file reads the GET value it becomes 1 all the time no matter what i pick in my combo box.

PHP File (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>


Here is my Receiver PHP file that will process the data

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

Answer

In PHP, "=" is an assignment rather than a comparison operator ("==" or "===" for its type sensitive sibling).

So as soon as your first if is called, your actual value is overwritten.

You might also consider PHP's switch ... case structure which is designed for such situations and still plays nicely with string data.

Try:

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