How to work with Add-To-Cart functionality?

My code about filterable audio searching, here how can i add Add-To-Cart functionality?

code :
index.php
https://justpaste.it/1qec4

and my

data fetching file,
data_fetch.php

https://justpaste.it/57xxp

How can i add add-to-cart functionality.

Note : In the cart just voice_id, voice_name, not necessary qty & price.

My current output:

Would it just be similar code to that which runs your “Add to Play list” button?

Here is my updated fetech.php page,

<?php

//fetch_data.php

include('database_connection.php');

if(isset($_POST["action"]))
{
	$query = "
		SELECT * FROM voice_bank_data WHERE voice_status = '1'
	";

	// if(isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"]))
	// {
	// 	$query .= "
	// 	 AND product_price BETWEEN '".$_POST["minimum_price"]."' AND '".$_POST["maximum_price"]."'
	// 	";
	// }

	// Gender
	if(isset($_POST["gender"]))
	{
		$gender_filter = implode("','", $_POST["gender"]);
		$query .= "
		 AND voice_gender IN('".$gender_filter."')
		";
	}

	// Genres
	if(isset($_POST["genres"]))
	{
		$genres_filter = implode("','", $_POST["genres"]);
		$query .= "
		 AND voice_genres IN('".$genres_filter."')
		";
	}

	// Voice Modulation
	if(isset($_POST["voice_modulation"]))
	{
		$voice_modulation_filter = implode("','", $_POST["voice_modulation"]);
		$query .= "
		 AND voice_voice_modulation IN('".$voice_modulation_filter."')
		";
	}


	// Languages
	if(isset($_POST["languages"]))
	{
		$languages_filter = implode("','", $_POST["languages"]);
		$query .= "
		 AND voice_languages IN('".$languages_filter."')
		";
	}

	// Jingle Moods
	if(isset($_POST["jingle_moods"]))
	{
		$jingle_moods_filter = implode("','", $_POST["jingle_moods"]);
		$query .= "
		 AND voice_jingle_moods IN('".$jingle_moods_filter."')
		";
	}

	// IVR
	if(isset($_POST["ivr"]))
	{
		$ivr_filter = implode("','", $_POST["ivr"]);
		$query .= "
		 AND voice_ivr IN('".$ivr_filter."')
		";
	}

	$statement = $connect->prepare($query);
	$statement->execute();
	$result = $statement->fetchAll();
	$total_row = $statement->rowCount();
	$output = '';
	if($total_row > 0)
	{
		foreach($result as $row)
		{
			$output .= '
			<div class="col-sm-3 col-lg-4 col-md-3">
				<div style="border:1px solid #ccc; border-radius:5px; padding:10px; margin-bottom:16px; height:300px;">
					<audio controls controlsList="nodownload" style="padding: 10px 10px 10px 10px;margin-left: -21px;">
						<source src="audio_sample/'. $row['voice_audio_file'] .'" alt="" class="img-responsive">
					</audio>
					<p align="center"><strong> '. $row['voice_name'] .'</strong></p>

					<p style="font-size: 12px;">
					Id		      : '. $row['voice_id'].' <br />
					Name		      : '. $row['voice_name'].' <br />
					Gender		      : '. $row['voice_gender'].' <br />
					Genres 			  : '. $row['voice_genres'].' <br />
					Voice Modulation  : '. $row['voice_voice_modulation'].' <br />
					Languages		  : '. $row['voice_languages'].' <br />
					Jingle Moods	  : '. $row['voice_jingle_moods'].' <br />
					Ivr 			  : '. $row['voice_ivr'].' <br /> </p>
					<button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;">Add to PlayList </button>


				</div>

			</div>
			';
		} 
	}
	else 
	{
		$output = '<h3>No Data Found</h3>';
	}
	echo $output;
}
?>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){

            $.ajax({
                type: 'POST',
                url: 'hello.php',
                success: function(data) {
                    alert(data);
                    

                }
            });
   });
});
</script>

if i click add to playlist button the alert was shown based on the hello.php file [ just echo message ], here how can i save my id and name into the hello.php page like cart page, if i click add to playlist button i need my id and name save into the hello.php page.

OK, so from that it seems that your “add to playlist” functionality isn’t implemented either, save for popping up an alert when you click the button.

I would suggest a browse down through the forum or general search - implementing a shopping cart function from scratch is a bit of a wide-ranging topic for a forum question. Forum questions are normally for helping out when you’ve done most of the coding and are having specific issues or to get ideas on how to implement a specific part, rather than the whole thing from scratch.

What’s your level of programming experience? That is, how familiar are you with PHP in general, and data handling? It’s not a difficult thing to do from what I can see, you already know what fields you need to store, so it’s just a case of designing somewhere to store them and writing the code. You could start by altering your pop-up to show the data that you want to store, rather than just the fixed message of your test code.

1 Like

If i try,

button :
<button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" onclick="addCart('. $row["voice_id"] .' ); ">Add to PlayList </button>

ajax :

function addCart(voice_id) {
	var voice_id   = $("#voice_id").val();
    var voice_name = $("#voice_name").val();
$.ajax({
            type: 'POST',
            url: 'cart.php',
            dataType: 'html',
            data : {
				voice_id: voice_id, 
				voice_name: voice_name}, // pass id to the php
            success: function(data) {
                // alert(data);


            }
        });
}

cart.php

<?php
$voice_id = '';
$voice_name = '';
// $voice_id = $_POST['voice_id'];
// echo $voice_id;
if(isset($_POST['voice_id']) && isset($_POST['voice_name']))
{
    $voice_id   = $_POST['voice_id'];
    $voice_name = $_POST['voice_name'];
}

echo "$voice_id ."&".  $voice_name";

?>

I want, if i click add to playlist button. id and name display to the cart.php like cart page, now nothing ill happen on button click.

if i am totally wrong make me pardon, i am in learning stage.

Is the data being passed into, and out of, cart.php? Add some alerts in your JS to see what is happening.

You might also want to browse around the JavaScript section of the forum and see whether anything in there helps.

These two lines

var voice_id   = $("#voice_id").val();
var voice_name = $("#voice_name").val();

rely on you having page elements with an id of voice_id and voice_name respectively, which I don’t see that you have in the code you posted. In any case, it wouldn’t be appropriate to add them as ids because your initial screen shots shows you have more than one item in your list, and ids have to be unique.

As you pass in voice_id to the addCart() function, why don’t you just use that value? There’s no need to pass the id and the name into your PHP code, as the PHP can easily retrieve the name from the database if it needs it.

My updated code :

Button:
<button type="button" class="btn btn-primary" type="submit" onclick="addCart('. "'".$row['voice_id']."'".','."'". $row['voice_name']."'".');" style="padding: 5px 83px 5px 83px;">Add to PlayList </button>

same page ajax :

<script type="text/javascript">
  function addCart(voice_id, voice_name){

            $.ajax({
                type: 'POST',
                url: 'cart.php',
                data: {voice_id:voice_id,
					   voice_name:voice_name},
                dataType: 'html',
                success: function(data) {
                    // alert(data);
                    

                }
            });
   }
    $(document).ready(function(){
        
});

cart.php :

<?php 
$voice_id = ''; 
$voice_name = ''; 


if(isset($_POST['voice_id']) && isset($_POST['voice_name'])) 
{ 
$voice_id = $_POST['voice_id']; 
$voice_name = $_POST['voice_name']; 
} 
var_dump($voice_id); 
echo $voice_id ."&". $voice_name; 

?>

If click button nothing will happen?

and my cart.php screenshot:

How to show voice_id and voice_name on cart.php like cart?

If you call cart.php directly from the browser, it’s not going to have any values to display, because they are passed in by your JS call. I can’t see your screen shot as it’s too small, but it looks as if it is showing empty values, but also the address bar shows you opened it directly, and the summary at the bottom suggests that it was called with “GET”, so of course the $_POST array will be empty.

I tried your code in a short page and it worked correctly for me. The only thing I changed is that I removed your references to $row for the values, and put hard-coded values in, because my test code doesn’t connect to a database. So, I suspect that’s the problem.

The first question would then be - if you right-click, “view source” and look at your button code, does it have the correct values that are coming from the $row result set? And are all the quotes in the correct place? My test code line looks like this:

<button type="button" class="btn btn-primary" type="submit" onclick="addCart('7','5');" style="padding: 5px 83px 5px 83px;">Add to PlayList </button>

and when I add back in your alert(), my test PHP code just does a var_dump($_POST) and it shows the values I expect when I click the button.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.