Value transfer like variable to another function

Hello, it’s posible to transfer value on button like variable in function and call this function ?

<div style= "height: 400px;
  width: 80%;
  background-color: powderblue;
  overflow:scroll;">
<?php

				include 'config.php';

$sql ="SELECT * FROM playlist ORDER BY name DESC";
$result = $conn->query($sql);

	if ( !empty($result->num_rows) && $result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
		$name = str_replace("\"","'", $row["name"]);
		$song = "playlist1/".$name;	
		$key = $row["id"];
		
				?>	 
				<form method="post">
				<button style= "width: 50px;" ><?php echo $row["point"] ?></button>
				<button style= "width: 50px;" ><?php echo $row["playtimes"] ?></button>
					<input type="submit" name="playnow"
							class="button" value="<?php echo $id;?>" />		
									
				</form>				
				</br>
				<?php			
		}
	}
?>
</div>

<?php
		function playnow(){
include 'config.php';
$sql = "UPDATE playlist SET playnow = 1 WHERE id = '$id'";

if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $conn->error;
}
}	
$conn->close();
?>

I want to send in function playnow value $id from button like $id in my function.

??

You were just given an example *by me) on how to do this? What has changed?

what is function playnow()… why is this a PHP function now? Isnt this for playing an audio clip… on the FRONT END?

in php create playlist with buttons. Everyone have id, name dir, point, vote and ‘playnow’.
Me need when i click button (select sound) to change in selected sound row ‘playnow’ from 0 to 1.

You are using PHP do this.

That is server side programming/code… it will NOT execute until the page is refreshed or submitted.

So you are at a crossroad.

Either you want to PLAY THE SOUND when the button is clicked…

OR you want to submit the page to do (whatever)… update some record of the sound being played.

IMHO… your best approach would be use an AJAX call to an external PHP script that does this ‘updating’ for you…WITHOUT interrupting the sound playback or the users experience.

1 Like

I will not play sound I want to change the database but I will see about AJAX. thanks.

??

then what are ‘buttons’ (that are clicked by the user)… and this ‘update’ query doing on the same page then?

One has to happen AFTER the other… no?

Also where is this #id coming from:

value="<?php echo $id;?>"
$sql = "UPDATE playlist SET playnow = 1 WHERE id = '$id'";

Why are you including (whatever it is) the config.php file twice?

  • Why are you closing the connection?

Seems like you are using:

$key = $row["id"]; 

to save the ID?

Thanks !!! You help me with AJAX :slightly_smiling_face:

Sponsor our Newsletter | Privacy Policy | Terms of Service