How can multiple cities be selected and displayed with their corresponding dates when the user clicks on a submit button in a PHP form?
To achieve this, you can use an HTML form with checkboxes for selecting multiple cities and a submit button. When the user clicks on the submit button, the form data is sent to a PHP script. In the PHP script, you can use a loop to iterate through the selected cities and display their corresponding dates.
<form method="post" action="display_cities.php">
<input type="checkbox" name="cities[]" value="New York"> New York<br>
<input type="checkbox" name="cities[]" value="Los Angeles"> Los Angeles<br>
<input type="checkbox" name="cities[]" value="Chicago"> Chicago<br>
<input type="submit" value="Submit">
</form>
```
In the `display_cities.php` script:
```php
<?php
if(isset($_POST['cities'])) {
$selectedCities = $_POST['cities'];
foreach($selectedCities as $city) {
// Get and display the corresponding date for each city
switch($city) {
case 'New York':
echo 'New York: January 1, 2022<br>';
break;
case 'Los Angeles':
echo 'Los Angeles: February 15, 2022<br>';
break;
case 'Chicago':
echo 'Chicago: March 30, 2022<br>';
break;
}
}
}
?>
Related Questions
- Is it necessary for query manipulation to occur on the database side in PHP?
- How can PHP developers efficiently manage and display different categories of data, such as appetizers, main courses, and desserts, on a webpage using variables and templates?
- What are the best practices for handling date and time input validation in PHP forms to prevent errors in database insertion?