-1

I am facing unknown issue and don't know why code not running as I desire , the following code I am trying to make table with ID showgrd it's contents changes when month and day select tag specified and when click on button with ID reqbtn.

I tried but table contents didn't change when I clicked the button, but I noticed that the query runs and I displayed the contents in another div. Now I am trying these contents to be displayed in the table which it's contents already displayed with different select statement:

<?PHP
$execqr;
if($_POST['sepcefict']=='avail')
{
    switch($_POST['days'])
    {
        case "defd" : $_POST['days']="";
        break;
    }
    $execqr="SELECT sum(ordersreq.qual*strg_catgs.Disrate) as subtotal , storage.codename,storage.ID,storage.Scale_rate from ordersreq INNER JOIN strg_catgs INNER JOIN storage INNER JOIN orders on ordersreq.Cat_ID=strg_catgs.catID where strg_catgs.elemID = storage.ID and strg_catgs.descr='cwe' and ordersreq.link=orders.link and orders.Ordate like '2023-$_POST[months]-$_POST[days]%' GROUP BY codename;"; 

}
elseif(!$_POST['sepcefict'])
{
    $execqr="SELECT sum(ordersreq.qual*strg_catgs.Disrate) as subtotal ,  storage.codename,storage.ID,storage.Scale_rate from ordersreq INNER JOIN strg_catgs INNER JOIN storage on ordersreq.Cat_ID=strg_catgs.catID where strg_catgs.elemID = storage.ID and strg_catgs.descr='cwe' GROUP BY codename; ";
}

?>
<div id='Container' dir='rtl'>
<h1> جــرد الـمـخـزن  </h1>
<select id='months'>
<option  value='defm' selected disabled> اختر الشهر </option>
<?PHP
for($i=1;$i<13;$i++)
{
    ?>
    <option value='<?PHP if(strlen($i)>"1") {echo $i;} else {echo "0".$i;} ?>'> <?PHP echo $i; ?>  </option>
    <?PHP
}
?>
</select>
<select id='days'>
<option value='defd' selected disabled> اختر اليوم </option>
<?PHP
for($z=1;$z<32;$z++)
{
    ?>
    <option value='<?PHP if(strlen($z)>"1") {echo $z;} else {echo "0".$z;} ?>'> <?PHP echo $z; ?>  </option>
    <?PHP
}
?>
</select>
<button id='reqbtn' class='btns' onclick='shdet(0,1)' style="width:130px;height:50px;"> الــجــرد </button>
<h2> الـجـرد الـفـورى </h2>
<?PHP 
$getused=mysqli_query($conn,$execqr) or die("<p id='sd'>".print_r(mysqli_error($conn))."</p>");
?>
<table id='showgrd' class='ordtb' dir='rtl' border='1'>
<tr>
<th>
اسم الصنف
</th>
<th>
الاستخدام الكلي
</th>
</tr>
<?PHP

while($getdata=mysqli_fetch_array($getused))
{
    
    switch($getdata['Scale_rate'])
    {
        case "si" : $getdata['Scale_rate']="جـرام";
        break;
        case "qu" : $getdata['Scale_rate']="وحـده";
        break;
    }
    ?>
    <tr id='shgrdtr'>
    <td><?PHP echo $getdata['codename']; ?> </td>
    <td> <?PHP echo $getdata['subtotal']." ". $getdata['Scale_rate']; ?>  <span onclick='shdet(<?PHP echo $getdata['ID']; ?> , 0)' style="color:cyan;margin-right:20px;"> التفاصيل </span> </td>
    </tr>
    <?
}
echo "<table>";


<script>
function shdet(elemid, datedef) {
  var params;

  if (document.getElementById("months").value != "defm") {
    var months = document.getElementById("months").value;
    var days = document.getElementById("days").value;
    params += "&sepcefict=avail&months=" + months + "&days=" + days;

  }
  var nreq = new XMLHttpRequest();
  nreq.onreadystatechange = function() {
    if (document.getElementById("grdtbl")) {
      document.getElementById("grdtbl").remove();
    }
    if (nreq.readyState == "4" && nreq.status == "200") {
      var filterresp = nreq.responseText;

    }
  }
  nreq.open("POST", "http://localhost:8084/cashier/main.php?pg=grd", true);
  nreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  nreq.send(params);
  //alert(params);
  $(document).ready(function(e) {
    $(" #showgrd ").load("http://localhost:8084/cashier/main.php?pg=grd #showgrd #tr");
  });
}
</script>

I tried to make sure that query runs with ajax request and it runs

Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • 1
    You're not displaying the response in `gridtbl` in the `onreadystatechange` function. You're just setting the variable `filterresp`, which you don't use for anything. – Barmar Aug 15 '23 at 21:51
  • @Barmar i am trying to load contents with jquery at last line if you notice when ajax send to server variable with specified date another select statement query will run and different result – youssef kroma Aug 15 '23 at 22:01
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Aug 15 '23 at 22:03
  • @Dharman thanks i appreciate Your Advice i am new at studying web developing by the way it is local project and i will take Your Advice – youssef kroma Aug 15 '23 at 22:09
  • Why are you using both XMLHttpRequest and `.load()`? The first one doesn't do anything useful. – Barmar Aug 16 '23 at 15:02
  • You're creating duplicate ID `showgrd` with `.load()`. It doesn't replace with the fetched element, it nests it inside, so you get `
    ...`
    – Barmar Aug 16 '23 at 15:05

0 Answers0