如何从数据库中选择具有相同id值的多行值并在ajax中显示?

时间:2022-12-02 17:23:19

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

我已经为button编写了使用ajax向php文件发送值的代码,如下所示:

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

这是我编写的按钮的ajax代码。我的按钮代码是:

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

在href中单击此按钮时,我希望使用ajax向php文件发送id值。

data1.php

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.

使用id值,我想从具有相同id值的数据库中选择包类型和包价格的多行。在选择这些值的多行之后,我希望将其发送到主php文件,并显示从数据库中选择的所有行的所有值。

Can anyone suggest how to do this ?

谁能告诉我该怎么做吗?

2 个解决方案

#1


2  

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


   ?>

#2


1  

There are some basic things you should know first then you can easily rectify your errors.

有一些基本的东西你应该首先知道,然后你可以很容易地纠正你的错误。

This is not you have asked but as a programmer i will suggest you to go through it.

这不是你所要求的,但作为一个程序员,我建议你去做。

As going through your code

通过你的代码

var packagetype = package_type;

package_type is undefined. If you are using chrome inspect element and check the console you will see the error.

package_type是未定义的。如果您正在使用chrome inspect元素并检查控制台,您将看到错误。

Hope this will work.

希望这将工作。

#1


2  

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


   ?>

#2


1  

There are some basic things you should know first then you can easily rectify your errors.

有一些基本的东西你应该首先知道,然后你可以很容易地纠正你的错误。

This is not you have asked but as a programmer i will suggest you to go through it.

这不是你所要求的,但作为一个程序员,我建议你去做。

As going through your code

通过你的代码

var packagetype = package_type;

package_type is undefined. If you are using chrome inspect element and check the console you will see the error.

package_type是未定义的。如果您正在使用chrome inspect元素并检查控制台,您将看到错误。

Hope this will work.

希望这将工作。