Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRafal Bachanek
28,766 PointsProblem with set selected atribute with function - PHP
I have index page with form (post method). After redirecting to action page i have the same form. One field is option field with string options: <8;22>. This option is saved in $startHour variable.
I would like that when i submit first form, the second form have selected the same option as first form.
I've used function to create options to form but it does'nt work :/ (i thin the if statement does'nt work and all options are without selected attribute)
<?php
function get_start_hours_html($starthour) {
$hour = "";
$option_value = 8;
while($option_value < 23) {
if ($option_value == $startHour) {
$hour = $hour . "<option value='" . $option_value . "'" . "selected" .">" . $option_value . "</option>";
} else {
$hour = $hour . "<option value='" . $option_value . "'>" . $option_value . "</option>";
}
$option_value++;
}
return $hour;
}
5 Answers
Ted Sumner
Courses Plus Student 17,967 PointsI don't understand why it is set up that way. Why don't you use the values from the first submission and assign them to variables or an array for whatever processing you need to do in the other file?
If you really need two forms, you could populate the second form with the $_GET values from the first file.
Rafal Bachanek
28,766 PointsI did so wtih text area from the form, but this is the select field with options, and i want that in the next page user have default selected option which he chosen on the first page.
Ted Sumner
Courses Plus Student 17,967 PointsFirst, my $_GET was wrong since you are using post. I think this should work if you add it to the spot to display as the default value:
<? echo $_POST[email] ?>
Of course you substitute the proper name for email.
Ted Sumner
Courses Plus Student 17,967 PointsI really don't understand what you are trying to do as an end result. Can you explain it to me?
Rafal Bachanek
28,766 Pointsok i think that better is GET metod that POST. i want that user after set parameters on first page to search something, could change this parameters on the form with results one of the parameters is select menu with the options, and when user chose option i would like that this option willl be selected (visually) on the second page)
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsEdited to format code