0

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 ?

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
lena
  • 143
  • 1
  • 13
  • you ajax call is all good the problem is with you PHP code. It's only fetching first record only. Check this http://stackoverflow.com/questions/6481806/how-to-select-multiple-rows-from-mysql-with-one-query-and-use-them-in-php – Aman Rawat Nov 11 '16 at 06:04
  • its not even displaying the first row in ajax code...I m not even getting those values too.... – lena Nov 11 '16 at 06:07
  • @lena look at below , I have added the the full code for you. – Pranav MS Nov 11 '16 at 06:09

2 Answers2

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


   ?>
Pranav MS
  • 2,235
  • 2
  • 23
  • 50
  • It will fail because the json root is array so you need to iterate `resultJson` on success. Otherwise it will give error – Aman Rawat Nov 11 '16 at 06:15
  • its alerting as packagetype :- "+undefined+" ----- package_price :-"+undefined. y is it like this ? – lena Nov 11 '16 at 06:15
  • success: function(result) { $.each(resultJson.error, function(i, item) { var packagetype = item.package_type; var package_price = item.package_price; alert("packagetype :- "+packagetype+" ----- package_price :-"+package_price); }); } Replace the sucess function with this and try again.Sorry for my mistake – Pranav MS Nov 11 '16 at 06:19
  • I have edited the answer please check now. Sorry for ma mistake. – Pranav MS Nov 11 '16 at 06:23
  • @lena now please try . – Pranav MS Nov 11 '16 at 06:31
  • the alert inside the success function is working for you ? – lena Nov 11 '16 at 06:36
  • @lena again i have edited the answer please carefully make changes in your page and try again. If again the error is coming please have a look at console and paste that error too. – Pranav MS Nov 11 '16 at 06:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127862/discussion-between-lena-and-pranav-m-s). – lena Nov 11 '16 at 06:38
0

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.

Hope this will work.

Community
  • 1
  • 1
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40