-2

I have a table named tblstationerystock having three columns like :-

uin, orderdate, quantity.

There are multiple record on different orderdate against uin.

My table Structure is:-

enter image description here

i have a form in which there are two input drop down (uin and orderdate ) which takes input from table tblstationerystock.

If i select the uin in the first input drop down box i want the second dropdown box should show only those date which belong to that particular uin.

My Problem:-

But the second dropdown shows all the value all the time .

my code for form is

<?php
include('includes/config.php');
?>

<div class="form-group col-md-12">
<label> User Name<span style="color:red;">*</span></label>
<select class="form-control" name="user" id="uin" onchange="fnorderdate()" >
<option value=""> </option>
<?php 
$sql = "SELECT uin from  tblstationerystock group by uin order by uin asc ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{               ?>  
<option value="<?php echo htmlentities($result->uin);?>"><?php echo htmlentities($result->uin);?></option>
 <?php }} ?>
</select>
</div>


<div class="form-group col-md-12">
<label> User Name<span style="color:red;">*</span></label>
<select class="form-control" name="user" id="orderdate" >
<option value=""> </option>
<?php 
$sql = "SELECT orderdate from  tblstationerystock group by orderdate order by orderdate asc ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{               ?>  
<option value="<?php echo htmlentities($result->orderdate);?>"><?php echo htmlentities($result->orderdate);?></option>
 <?php }} ?>
</select>
</div>
<script>
function fnorderdate()
{
    uin=$('#uin').val();
    $.ajax({
        method:"post",
        url:"ajax.php"'
        data:{uin:uin},
        
        success:function(result)
        {
            $('#orderdate').html(result);
        
        

}
</script>

my ajax code is

<?php
include('includes/config.php');
if(isset($_POST[uin]))
{
    $uin=$POST['uin'];
    
    $select="select orderdate from tblstationerystock where uin='$uin' ";
    $query=mysqli_query($conn,$select);
    while($data=mysqli_fetch_assoc($query))
    {
        echo "<option value='".$data['orderdate']."'>".$data['orderdate']."</option>
    }
}
    


?>
KUMAR
  • 1,993
  • 2
  • 9
  • 26
roopa
  • 75
  • 7
  • Note that in the absence of any aggregating functions, a GROUP BY clause is never appropriate – Strawberry Nov 10 '20 at 06:17
  • 1
    And see about sql injection and the importance of prepared and bound queries (it's strange that you 'prepare' queries that need no preparation, and then don't prepare the one query that does!) – Strawberry Nov 10 '20 at 06:17
  • @Strawberry plz explain – roopa Nov 10 '20 at 06:25
  • @roopa have you solved your problem or not?? – KUMAR Nov 10 '20 at 06:33
  • @KUMAR not solved.Kindly help – roopa Nov 10 '20 at 06:35
  • @roopa okay please wait...... – KUMAR Nov 10 '20 at 06:35
  • @KUMAR thnx waiting............ – roopa Nov 10 '20 at 06:46
  • @roopa in first select uin list shows correctly?? – KUMAR Nov 10 '20 at 06:49
  • @KUMAR yes fist select list shows all the uin in table – roopa Nov 10 '20 at 06:50
  • @roopa have you solved your problem or not?? – KUMAR Nov 10 '20 at 07:44
  • @roopa please upvote my answer also. – KUMAR Nov 10 '20 at 09:36
  • @KUMAR i did that thx – roopa Nov 10 '20 at 09:58
  • **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/5741187) – Dharman Nov 10 '20 at 11:37

1 Answers1

-2

View Part:-

<?php
include('includes/config.php');
?>
<div class="form-group col-md-12">
<label> UIN<span style="color:red;">*</span></label>
<select class="form-control" name="uin" id="uin" >
<option value="" > </option>
<?php 
$sql = "SELECT uin from  tblstationerystock group by uin order by uin asc";
$query = $dbh->prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{  ?>  
<option value="<?php echo htmlentities($result->uin);?>">
<?php echo htmlentities($result->uin);?>
</option>
 <?php }} ?>
</select>
</div>


div class="form-group col-md-12">
<label> ORDER DATE<span style="color:red;">*</span></label>
<select class="form-control" name="orderdate" id="orderdate" >

</select>
</div>

jQuery / AJAX Code:-

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){

    $('#uin').on("change",function () {
        var uin = $(this).val();
        $.ajax({
            url: "ajax.php",
            type: "POST",
            data: "uin="+uin,
            success: function (response) {
                console.log(response);
                $("#orderdate").html(response);
            },
        });
    }); 

});

</script>

make a php file name it ajax.php in same directory

and put this code:-

<?php 
include('includes/config.php');
$uin = $_POST['uin'];
echo "<option>Select ORDER DATE</option>";
$sql = "SELECT orderdate from  tblstationerystock WHERE uin=$uin";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{               ?>  
<option value="<?php echo htmlentities($result->orderdate);?>">
<?php echo htmlentities($result->orderdate);?>
</option>
 <?php }}
 ?>
KUMAR
  • 1,993
  • 2
  • 9
  • 26