By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,248 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,248 IT Pros & Developers. It's quick & easy.

PHP - Ajax Passing value to another php file

aveeva
P: 27
Here is my PHP, i want to share voice_id and voice_name to another php file using ajax,

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //fetch_data.php
  3.  
  4. include('database_connection.php');
  5.  
  6. if(isset($_POST["action"]))
  7. {
  8.     $query = "
  9.         SELECT * FROM voice_bank_data WHERE voice_status = '1'
  10.     ";
  11.  
  12.     // if(isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"]))
  13.     // {
  14.     //     $query .= "
  15.     //      AND product_price BETWEEN '".$_POST["minimum_price"]."' AND '".$_POST["maximum_price"]."'
  16.     //     ";
  17.     // }
  18.  
  19.     // Gender
  20.     if(isset($_POST["gender"]))
  21.     {
  22.         $gender_filter = implode("','", $_POST["gender"]);
  23.         $query .= "
  24.          AND voice_gender IN('".$gender_filter."')
  25.         ";
  26.     }
  27.  
  28.     // Genres
  29.     if(isset($_POST["genres"]))
  30.     {
  31.         $genres_filter = implode("','", $_POST["genres"]);
  32.         $query .= "
  33.          AND voice_genres IN('".$genres_filter."')
  34.         ";
  35.     }
  36.  
  37.     // Voice Modulation
  38.     if(isset($_POST["voice_modulation"]))
  39.     {
  40.         $voice_modulation_filter = implode("','", $_POST["voice_modulation"]);
  41.         $query .= "
  42.          AND voice_voice_modulation IN('".$voice_modulation_filter."')
  43.         ";
  44.     }
  45.  
  46.  
  47.     // Languages
  48.     if(isset($_POST["languages"]))
  49.     {
  50.         $languages_filter = implode("','", $_POST["languages"]);
  51.         $query .= "
  52.          AND voice_languages IN('".$languages_filter."')
  53.         ";
  54.     }
  55.  
  56.     // Jingle Moods
  57.     if(isset($_POST["jingle_moods"]))
  58.     {
  59.         $jingle_moods_filter = implode("','", $_POST["jingle_moods"]);
  60.         $query .= "
  61.          AND voice_jingle_moods IN('".$jingle_moods_filter."')
  62.         ";
  63.     }
  64.  
  65.     // IVR
  66.     if(isset($_POST["ivr"]))
  67.     {
  68.         $ivr_filter = implode("','", $_POST["ivr"]);
  69.         $query .= "
  70.          AND voice_ivr IN('".$ivr_filter."')
  71.         ";
  72.     }
  73.  
  74.     $statement = $connect->prepare($query);
  75.     $statement->execute();
  76.     $result = $statement->fetchAll();
  77.     $total_row = $statement->rowCount();
  78.     $output = '';
  79.     if($total_row > 0)
  80.     {
  81.         foreach($result as $row)
  82.         {
  83.             $output .= '
  84.             <div class="col-sm-3 col-lg-4 col-md-3">
  85.                 <div style="border:1px solid #ccc; border-radius:5px; padding:10px; margin-bottom:16px; height:300px;">
  86.                     <audio controls controlsList="nodownload" style="padding: 10px 10px 10px 10px;margin-left: -21px;">
  87.                         <source src="audio_sample/'. $row['voice_audio_file'] .'" alt="" class="img-responsive">
  88.                     </audio>
  89.                     <p align="center"><strong> '. $row['voice_name'] .'</strong></p>
  90.  
  91.                     <p style="font-size: 12px;">
  92.                     Id              : '. $row['voice_id'].' <br />
  93.                     Name              : '. $row['voice_name'].' <br />
  94.                     Gender              : '. $row['voice_gender'].' <br />
  95.                     Genres               : '. $row['voice_genres'].' <br />
  96.                     Voice Modulation  : '. $row['voice_voice_modulation'].' <br />
  97.                     Languages          : '. $row['voice_languages'].' <br />
  98.                     Jingle Moods      : '. $row['voice_jingle_moods'].' <br />
  99.                     Ivr               : '. $row['voice_ivr'].' <br /> </p>
  100.                     <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;">Add to PlayList </button>
  101.  
  102.                 </div>
  103.  
  104.             </div>
  105.             ';
  106.         } 
  107.     }
  108.     else 
  109.     {
  110.         $output = '<h3>No Data Found</h3>';
  111.     }
  112.     echo $output;
  113. }
  114. ?>
  115.  
  116. <script>
  117.  
  118.     $('.btn').on('click',function() {
  119.         var voice_id     = $("#voice_id");
  120.         var voice_name  = $("#voice_name");
  121.  
  122.         $.ajax({
  123.             type      : "POST",
  124.             url       : "my_cart.php",
  125.             datatype : "text",
  126.             // data     : {voice_id: voice_id, voice_name: voice_name },
  127.             data     : "voice_id="+voice_id+"&voice_name="+voice_name,
  128.  
  129.  
  130.             success: function(data)
  131.             {
  132.                 // console.log(data);
  133.                 console.log('success',data);
  134.  
  135.             }
  136.         });
  137.     });
  138.  
  139. </script>
  140.  
3 Weeks Ago #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,283
so what is the exact problem you have with your code? as far as i can see in that bunch of code you try to retrieve the values from an element with jQuery's id selector - but in the entire html i dont see you creating that element with such an id.
3 Weeks Ago #2

aveeva
P: 27
Simple, my output:

Expand|Select|Wrap|Line Numbers
  1. if($total_row > 0)
  2.     {
  3.         foreach($result as $row)
  4.         {
  5.             $output .= '
  6.             <div class="col-sm-3 col-lg-4 col-md-3">
  7.                 <div style="border:1px solid #ccc; border-radius:5px; padding:10px; margin-bottom:16px; height:300px;">
  8.                     <audio controls controlsList="nodownload" style="padding: 10px 10px 10px 10px;margin-left: -21px;">
  9.                         <source src="audio_sample/'. $row['voice_audio_file'] .'" alt="" class="img-responsive">
  10.                     </audio>
  11.                     <p align="center"><strong> '. $row['voice_name'] .'</strong></p>
  12.  
  13.                     <p style="font-size: 12px;">
  14.                     Id              : '. $row['voice_id'].' <br />
  15.                     Name              : '. $row['voice_name'].' <br />
  16.                     Gender              : '. $row['voice_gender'].' <br />
  17.                     Genres               : '. $row['voice_genres'].' <br />
  18.                     Voice Modulation  : '. $row['voice_voice_modulation'].' <br />
  19.                     Languages          : '. $row['voice_languages'].' <br />
  20.                     Jingle Moods      : '. $row['voice_jingle_moods'].' <br />
  21.                     Ivr               : '. $row['voice_ivr'].' <br /> </p>
  22.                     <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;">Add to PlayList </button>
  23.  
  24.                 </div>
  25.  
  26.             </div>
  27.             ';
  28.         } 
  29.     }
  30.     else 
  31.     {
  32.         $output = '<h3>No Data Found</h3>';
  33.     }
  34.     echo $output;
  35. }
  36.  
i want my output voice_id and voice_name post to another php pag,
in the time of click button, so i added into button,


Sorry this line not added.
Expand|Select|Wrap|Line Numbers
  1. <button type="button" class="btn btn-primary" style="padding: 5px 83px 5px 83px;" data-voice-id="'.$row["voice_id"].'" data-voice-name="'.$row["voice_name"].'">Add to Playlist</button>
  2.  
and my ajax :

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3.     $('.btn').on('click',function() {
  4.         // var voice_id     = $("#voice_id");
  5.         // var voice_name  = $("#voice_name");
  6.  
  7.         var voice_id = $(this).data("voice-id");
  8.         var voice_name = $(this).data("voice-name");
  9.  
  10.         $.ajax({
  11.             type      : "POST",
  12.             url       : "my_cart.php",
  13.             datatype : "text",
  14.             data     : { voice_id: voice_id, voice_name: voice_name },
  15.             // data     : "voice_id="+voice_id+"&voice_name="+voice_name,
  16.  
  17.  
  18.  
  19.             success: function(data)
  20.             {
  21.                 // console.log(data);
  22.                 //console.log('success',data);
  23.                 //$('#result').html(response);
  24.  
  25.             }
  26.         });
  27.     });
  28.  
  29. </script>
  30.  
and my my_cart.php

Expand|Select|Wrap|Line Numbers
  1. <?PHP
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7. // Get details from fetch_data.php
  8.  
  9. $voice_id = '';
  10. $voice_name = '';
  11.  
  12. if(isset($_POST['voice_id']) && isset($_POST['voice_name']))
  13. {
  14.     $voice_id   = $_POST['voice_id'];
  15.     $voice_name = $_POST['voice_name'];
  16. }
  17.     echo "$voice_id & $voice_name";
  18.  
  19. ?>
  20.  
How to post my voice_id and voice_name if click add to playlist button,

FYI -> https://snag.gy/DHQTZ6.jpg

How to retrive the post value and display like

https://snag.gy/bWsU6z.jpg
2 Weeks Ago #3

gits
Expert Mod 5K+
P: 5,283
your button doesnt have a data-property - thats what you try to retrieve with:

Expand|Select|Wrap|Line Numbers
  1. var voice_id = $(this).data("voice-id");
  2. var voice_name = $(this).data("voice-name");
your properties are called:

data-voice-id
data-voice-name

on that button. So you need to use the correct way to get the values.
2 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.