PHP - Ajax Passing value to another php file

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

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

	$('.btn').on('click',function() {
		var voice_id 	= $("#voice_id");
		var voice_name  = $("#voice_name");

		$.ajax({
			type 	 : "POST",
			url  	 : "my_cart.php",
			datatype : "text",
			// data	 : {voice_id: voice_id, voice_name: voice_name },
			data	 : "voice_id="+voice_id+"&voice_name="+voice_name,


			success: function(data)
			{
				// console.log(data);
				console.log('success',data);
				
			}
		});
	});

</script>

Not sharted the details.

To be clear: PHP does not execute javascript. The local client executes javascript.

What… is your question? Looks like you’ve got everything you need. Might want to properly structure your JSON, but it will interpret correctly anyway.

Yes, you are right, but here i need to share my voice_id and voice_name to another php file,
here is my button :

	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>

and my output,

If click addtoplaylist button the id and name should be pass to my_cart.php, like add-to-cart

That says: “Anything with the class “button” on it.”

That class list does not contain the word “button”.

Now update with
$(‘.btn’).on(‘click’,function() {

ref : https://www.reddit.com/r/PHPhelp/comments/c881iv/php_ajax_passing_value_to_another_php_file/eskmi17?utm_source=share&utm_medium=web2x

And your (EDIT: insert the word javascript here.) code should work.

Do you have an actual question? or problem? Or… anything we’re supposed to actually be replying to?

These two lines:

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

say “get the element with id of voice_id and the element with id of voice_name, take the values, and put them into these variables.” You don’t have any elements with those id in the code you’ve shown and, if you did, it wouldn’t be appropriate because you loop through database query results, so you’d end up with duplicate ids, which isn’t allowed.

I can think of a few ways you could create unique ids within your loop, but I’m not sure of how you’d “tie” them to the button that is pressed. Maybe that would be better asked in the JavaScript section as it’s nothing to do with PHP, even though it’s PHP generating the JS code for your browser to execute.

I did mistake, pls check with my updated code.

All you’ve done is remove .val() from the end - you’re still trying to reference elements with ids that aren’t present in your page.

Here i need to share my voice_id and voice_name to my_cart.php, what i need to do?

As I said above, you need to figure out a way to have a unique element id for each value that you need to share, output those as part of your loop, so that your JS code can retrieve the two bits of information to pass into your PHP. As I also said, this is JavaScript really, so you might be better asking the question in the JS forum. Getting the values in the way that you do in your click() function is correct, you are just trying to retrieve values from page elements that you have not defined anywhere.

Normally you’d have something like this:

// inside your loop
echo '<span id="voice_id">' . $row["voice_id"] . '</span>';
echo '<span id="voice_name">' . $row["voice_name"] . '</span>';

and your code (or a variation of it) would be fine, but the id must be unique, so you can’t use that method here as you are displaying multiple results from your search. You could build an id using a counter, but I am not sure how you would tell the click() function which one to use. Someone who knows more about JavaScript might tell you the answer to that very quickly, along with a “nicer” way to do the code.

Slightly off-topic, but why do you need to send id and name to the PHP - can’t you retrieve the name using the id with a query? Or can the user change the name and you need to store the edited name?

1 Like

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