lena lena - 26 days ago 5
Ajax Question

How to pick multiple rows of values of the same id value from the database and display in ajax?

I have written code for button to send a value to php file using ajax as show below :

<script>
$(document).ready(function(){
$(".open-AddBookDialog").click(function(){
var packageid = $(this).attr("data-id");
var dataString = 'packageid='+ packageid;
$.ajax({
type: "POST",
url: "data1.php",
data: dataString,
cache: false,
success: function(result1){
var packagetype = package_type;
alert(packagetype);
}
});
});
});
</script>


This is the ajax code for the button which i have written. My button code is :

<a data-toggle="modal" id="custId" data-name="<?php echo $arr["package_name"]; ?>" data-id="<?php echo $arr["id"]; ?>" href="#myModal" class="open-AddBookDialog btn btn-submit" OnClick='change(this);'>Book / Enquiry / Pay</a>


When clicking this button in href, I want to send a id value to a php file using ajax.

data1.php

<?php
include('database.php');
$id=$_POST['packageid'];
$query1 = mysqli_query($con,"select * from ayurveda_packagetypes where package_id = '$id'");
$arr = mysqli_fetch_array($query1);
echo $arr['package_type'];
echo $arr['package_price'];
mysqli_close($con);
?>


using the id value, I want to pick multiple rows of package type and package price from the database having the same id value.After picking the multiple rows of these values i want to send it to the main php file and display all the values of all the rows picked from the database.

Can anyone suggest how to do this ?

Answer
    <script>


          $(document).ready(function()
            {
                $(".open-AddBookDialog").click(function()
                    {
                        var packageid = $(this).attr("data-id");
                        var dataString =  'packageid='+ packageid;
                        $.ajax(
                            {
                                type: "POST",
                                url:  "data1.php",
                                data: dataString,
                                cache: false,
                                success: function(result)
                                {
                                    resultJson=jQuery.parseJSON(result);
                                    $.each(resultJson.packageDetails, function(i, item) {
                                        var packagetype = item.package_type;
                                        var package_price = item.package_price;
                                        alert("packagetype :- "+packagetype+"    ----- package_price :-"+package_price);
                                    }); 
                                }
                            });
                    });
            });


 </script>
 <?php

          include('database.php'); 
          $id=$_POST['packageid']; 
          $query1 = mysqli_query($con,"select * from ayurveda_packagetypes where package_id = '$id'"); 
          //$arr  = mysqli_fetch_array($query1); 
          while( $strPackageResult=mysqli_fetch_array($query1,MYSQLI_ASSOC) )
        {  $ArrPackage[]=$strPackageResult;  }
        if( isset($strPackageResult) ){  unset($strPackageResult);  }
        mysqli_free_result($query1);
        if( isset($query1) ){  unset($query1);  }
            $myResultPackageArray=array();
            if( isset($ArrPackage) && is_array($ArrPackage) && count($ArrPackage)>0 ) {
                $tempArray=array();
            foreach( $ArrPackage as $tempPackage )
            { 
                $tempArray['package_type']=$tempPackage['$tempPackage'];
                $tempArray['package_price']=$tempPackage['package_price'];
                $myResultPackageArray['packageDetails'][] =$tempArray;
            }  
        } 
         mysqli_close($con); 
         echo json_encode($myResultPackageArray);


   ?>