Problem the page is refresh after click on button with Form Post

I am using PHP and form Post in HTML to activate the php when click the button but every time I am clicking on the button the page refresh and I don’t want it to refresh:

the code is:

<?php
include ('db_config.php');

$db_select = "SELECT * FROM led WHERE id = 1";

$result = $db->query($db_select);

$row = $result->fetch_assoc();

$LED = $row['status'];

echo $LED;
if (isset($_POST['btn_led_01'])){
	if ($LED=='ON'){
		$db_insert = "UPDATE led SET status='OFF' WHERE id=1";
		if (mysqli_query($db,$db_insert)){
			$add_record = "Add New Record";
		}
		else{
			echo "Error :" .$db_insert."<br>".mysqli_connect_error($db);
		}
	}else{
		$db_insert = "UPDATE led SET status='ON' WHERE id=1";
		if (mysqli_query($db,$db_insert)){
			$add_record = "Add New Record";
		}
	else{
			echo "Error :" .$db_insert."<br>".mysqli_connect_error($db);
		}
	}
}
?>

<html>
    
  <head>
   
 
  </head>  
<body>
 <h1>Climate Monitoring and Alarm for My Cute Room ;) </h1>
  <form method="post" action="">
<button type="submit" class="ledonoff" name="btn_led_01"><?php echo $LED;?></button>
      

	
  </body>

Hi, mohammadsaid1980.

Please be aware that we cannot use the code that you have posted. The intent of posting code is to give us a working example of how your page behaves. We can copy the code of a “working page” to a file on our computer and see exactly what you see and thereby understand the problem and help you resolve it.

Please notice that all of your links and resources, except for the fonts, are local to your computer. Your links to not contain web addressable URLs which means that they provice little or no information to us. This is also true of your PHP. Browsers cannot interpret PHP so we need to see the interpreted code, the HTML, that the browser uses. You can copy this interpreted code from your browser and paste it into a post on the forum just as you have done in your present post with the raw code.

For more information, please review our posting guidelines.

Thank you for your cooperation.

2 Likes

You’ll need to use Javascript then.

1 Like

Thanks for rpely :slight_smile: any example can guide me how to do it?

The following article should help to explain the principles

1 Like

I came back to thank you I solved it with jquery and javascript :grin:

2 Likes

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