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 ?

3
  • you ajax call is all good the problem is with you PHP code. It's only fetching first record only. Check this stackoverflow.com/questions/6481806/… Commented Nov 11, 2016 at 6:04
  • its not even displaying the first row in ajax code...I m not even getting those values too.... Commented Nov 11, 2016 at 6:07
  • @lena look at below , I have added the the full code for you. Commented Nov 11, 2016 at 6:09

2 Answers 2

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


   ?>
Sign up to request clarification or add additional context in comments.

8 Comments

It will fail because the json root is array so you need to iterate resultJson on success. Otherwise it will give error
its alerting as packagetype :- "+undefined+" ----- package_price :-"+undefined. y is it like this ?
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
I have edited the answer please check now. Sorry for ma mistake.
@lena now please try .
|
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.

1 Comment

This is the basics of the programming. All the programmers should be aware about this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.