Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Arrays Ex. #3: Arrays from User Input

For this PHP exercise, create a form asking the user for input about the weather the user has experienced in a month of the user's choice. In separate text fields, request the city, month and year in question. Below that, show a series of checkboxes using the same weather conditions you used in Arrays Ex. #1. (Those values were: rain, sunshine, clouds, hail, sleet, snow, wind.) Set up the form to create an array from the checked items.

In the response section of your script, create an array using the city, month and year the user entered as values. Print the following response "In $city in the month of $month $year, you observed the following weather:", where $city, $month and $year are values from the array you created.

Next, loop through the $weather[] array you received from the user to send back a bulleted list with the user's responses.

 

Comments

I wish I knew about this site when I was begining php - It is far more engaging(and more fun)than the likes of Tizag and w3schools.

Really good work!!!

P.S. If you are a total newb reading this comment, take my advice - BOOKMARK THIS SITE.

Thanks, reno. Glad you like the site.

Hello,
this is my first comment,and at the beginning i would like to say that this the best site for practice.
i noticed i can't run the code at the line 48-50 and i think it's better to do something like that :

  1. $weather = $_POST['weather'];
  2. foreach($weather as $w){
  3.   echo "<li>$w</li>\n"; 
  4. }

The code you cite still works with PHP 5.3.10 (on our host's server), but apparently doesn't work right with more recent versions. We also agree that it's cleaner to assign the value to a variable so have changed the code in the answer script accordingly. Here was the old code, for people who come along later and wonder what this is about:

  1. echo "In $inputLocal[0] in the month of $inputLocal[1] $inputLocal[2], 
  2. you observed the following weather:<br /> <ul>";
  3.  
  4. foreach($_POST['weather'] as $w){
  5.   echo "<li>$w</li>\n";  
  6. }
  7.  
  8. echo "</ul>";

Even when i copy your code my browser wont show the part after else - wont show the solution..
It shows again empty form

any help appreciated
thanks for a great site

What version of PHP is your host running?

PHP Version 5.3.8

i am using XAMPP

i noticed that i had trouble in all exercises where is line {syntaxhighlighter OPTIONS}if (!isset($_POST)){{/syntaxhighlighter}...
had to find some type of workaround ..

thanks

I installed newest version of XAMPP, it comes with PHP Version 5.4.7 , now everything works ..
Thanks a lot !!.. off i go to do more exercises :))

First, I really dig what you've done here, the site. As a novice developer it really helps to have some things to work on. To get me thinking. So much more helpful than just tutorials.

Any how check this out:

  1. if (!isset($_POST['submit'])) { ?>
  2. 		<h2>Please take the survey</h2>
  3. 		<form name="form-data" action="array3.php" method="post">
  4. 			<p>City: <input type="text" name="city" value=""> &nbsp;
  5. 				Month: <input type="text" name="month" value="" > &nbsp;
  6. 				Year: <input type="text" name="year" maxlength="4" value=""></p>
  7. 				<p>Please choose the weather conditions you observed in your stated city. 
  8. 				<br />Choose all that apply. </p>
  9. 				<input type="checkbox" name="weather[]" value="rain">Rain &nbsp;
  10. 				<input type="checkbox" name="weather[]" value="sun">Sunshine &nbsp;
  11. 				<input type="checkbox" name="weather[]" value="clouds">Clouds &nbsp;
  12. 				<input type="checkbox" name="weather[]" value="hail">Hail <br />
  13. 				<input type="checkbox" name="weather[]" value="sleet">Sleet &nbsp;
  14. 				<input type="checkbox" name="weather[]" value="snow">Snow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  15. 				<input type="checkbox" name="weather[]" value="ice">Ice &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  16. 				<input type="checkbox" name="weather[]" value="wind">Wind <br /><br />
  17. 				<input type="submit" name="submit" value="GO!">
  18. 				</form>
  19. 		<?  } else {
  20. 			echo "<h2>Here are your answers:</h2>";
  21. 			foreach(array_keys($_POST) as $key){
  22. 				$$key = $_POST[$key];
  23. 				}
  24. 			echo "In $city in the month of $month $year you observed the following weather conditions:<br />";
  25. 			echo "<ul>";
  26. 			foreach($weather as $val){
  27. 				echo "<li>$val</li>";
  28. 			}
  29. 			echo "</ul>";
  30. 			echo "<input type=\"button\" onclick=\"history.back();\" value=\"Back\">";
  31.  
  32.  
  33. 			//var_dump($_POST);
  34. 		} 

The real challenge for me was breaking the data out from the $_POST array in a useful manner.
I finally (after much T&E) hit on using a variable variable, using the array key for the variable name.
But I didn't put them into an array, I was so focused on having my echo read like you had it in the instructions,
I just couldn't figure out how or why to put them into an array just to call them back out. and what I got seemed
pretty elegant to me, so I just went with it.

All the &nbsp; are just for layout, it bugged me that things weren't relatively lined up,
that's why I added ice to the list too, just for balance.

I commented out the var_dump on the $_POST at the end, that was there so I could see what was coming
through and how it was arranged. It really helped in the T&E phase to figure out a solution.

Thanks again for the great exercises!*/

I am kind of new to PHP and I copied the same code but its not working.

Actually any form is not responding. Anybody have any suggestions please.

I am using 5.3.13 php version.

Norma

Post the code here and i'll try my best to help you :)
don't forget about the formatting.

  1. <html>
  2. <body>
  3. <h1>How's your weather</h1>
  4. <form action="index.php" method="POST">
  5. <p>Please enter your information:</p>
  6. <label>City: </label><input type="text" name="city"/>
  7. <label>Month: </label><input type="text" name="month"/>
  8. <label>Year: </label><input type="text" name="year"/>
  9. <p>Please choose the kinds of weather you experienced from the list below. 
  10. <br>Choose all that apply.</p>
  11. <input type="checkbox" name="weather[]" value="sunshine" />Sunshine<br />
  12. <input type="checkbox" name="weather[]" value="clouds" />Clouds<br />
  13. <input type="checkbox" name="weather[]" value="rain" />Rain<br />
  14. <input type="checkbox" name="weather[]" value="hail" />Hail<br />
  15. <input type="checkbox" name="weather[]" value="sleet" />Sleet<br />
  16. <input type="checkbox" name="weather[]" value="snow" />Snow<br />
  17. <input type="checkbox" name="weather[]" value="wind" />Wind<br />
  18. <input type="checkbox" name="weather[]" value="cold" />Cold<br />
  19. <input type="checkbox" name="weather[]" value="heat" />Heat<br /><br />
  20. <input type="submit" name="submit" value="Go" />
  21. </form>
  22. </body>
  23. </html>
  24.  
  25. <?php
  26.  
  27. if(isset($_POST['submit']) && !empty($_POST['city']) && !empty($_POST['month']) && !empty($_POST['year'])){
  28. 	$city = $_POST['city'];
  29. 	$month = $_POST['month'];
  30. 	$year = $_POST['year'];
  31.  
  32. 	$weather = $_POST['weather'];
  33. 	foreach($weather as $w){
  34.  
  35. 	echo 'In '.$city.' in the month of '.$month.' '.$year .', you observed the following weather: '.$w;
  36. 	}
  37. }else{
  38. 	echo 'Please fill all empty fields and check weather.';
  39. }
  40.  
  41. ?>

  1. <?php
  2. 	if (isset($_POST['city']) && isset($_POST['month']) && isset($_POST['year'])) {
  3. 		$city = $_POST['city'];
  4. 		$month = $_POST['month'];
  5. 		$year = $_POST['year'];
  6.  
  7. 		if (empty($city) === true || empty($month) === true || empty($year) === true) {
  8. 			echo "Please fill out all the fields";
  9. 		}else {
  10.  
  11.  
  12. 			if (isset($_POST['weather'])) {
  13. 			$weather = $_POST['weather'];
  14. 				echo "In $city in the month of $month $year, you observed the following weather:";
  15. 				echo "<ul>";
  16. 					foreach ($weather as $value) {		
  17. 						echo "<li>" . $value . "</li>";
  18. 					}
  19. 				echo "</ul>";
  20. 			exit();
  21. 			}else {
  22. 				echo "Please choose what weather you are experiencing";
  23. 			}
  24. 		}
  25. 	}
  26. ?>
  27.  
  28.  
  29. <!DOCTYPE html>
  30. <head><title>Variable Data Types</title>
  31.  
  32. </head>
  33. <body>
  34. 	<h4>Please fill out all the information needed.</h4>
  35. 	<form action = "" method = "POST">
  36. 		<label>City: <input type = "text" name = "city" /></label>
  37. 		<label>Month: <input type = "text" name = "month" /></label>
  38. 		<label>Year: <input type = "text" name = "year" /></label>
  39.  
  40. 	<p>Please check what kind of weather you experienced from the list below.</p>
  41. 		<label><input type = "checkbox" name = "weather[]" value = "rain" />Rain</label></br>
  42. 		<label><input type = "checkbox" name = "weather[]" value = "sunshine" />Sunshine</label></br>
  43. 		<label><input type = "checkbox" name = "weather[]" value = "clouds"/>Clouds</label></br>
  44. 		<label><input type = "checkbox" name = "weather[]" value = "hail" />Hail</label></br>
  45. 		<label><input type = "checkbox" name = "weather[]" value = "sleet" />Sleet</label></br>
  46. 		<label><input type = "checkbox" name = "weather[]" value = "snow" />Snow</label></br>
  47. 		<label><input type = "checkbox" name = "weather[]" value = "wind" />Wind</label></br>
  48. 		<input type = "submit" value = "Submit" />
  49. 	</form>
  50.  
  51. </body>
  52. </html>