What is the best method to pass a value from one PHP page to another when a button is clicked?
When a button is clicked on one PHP page and you want to pass a value to another PHP page, you can use either GET or POST methods to send the value as a parameter in the URL or in the form data. Using GET method will append the value to the URL, making it visible to users, while using POST method will keep the value hidden in the form data.
// Page 1: Sending page with button
<form action="page2.php" method="get">
<input type="hidden" name="value" value="example_value">
<button type="submit">Send Value</button>
</form>
```
```php
// Page 2: Receiving page
if(isset($_GET['value'])){
$received_value = $_GET['value'];
echo "Received value: " . $received_value;
}
Related Questions
- How can the size of an array be compared within a loop to identify the last iteration in PHP?
- What are the best practices for handling user input, such as passwords, in PHP scripts to ensure security?
- What are the potential pitfalls of deleting user entries after a certain period of inactivity in PHP?