Jump to content

Can't get chosen $variables <select> tags by user to display on results page (whilst using 'for' and 'foreach' loops)

Recommended Posts

Below is a calendar I have setup. Here I've used the 'for' loop with the $day variable and the 'foreach' loop with the $month/$year variables. I did originally have this setup as a simple function, but thought originally it was this that which could have been causing the problem.(calendar.php)---------------------------------------------------------------------------------------------------<form action="resultsform.php" method="post">$year = range (2007, 2010);$month = array ('January' => 'January', 'February' => 'February', 'March'=>'March', 'April' => 'April', 'May'=>'May', 'June'=>'June', 'July'=>'July', 'August'=>'August', 'September'=>'September', 'October'=>'October', 'November' =>'November', 'December'=>'December');$day = range (1,31);echo '<select name="day">'; for ($day = 1; $day <=31; $day++) { echo"\n<option value=\"day\">$day</option>"; }echo"</select>";echo'<select name="month">'; foreach($month as $value) { echo"\n<option value=\"month\">$value</option>"; }echo '</select>';echo '<select name="year">'; foreach($year as $value) { echo"\n<option value=\"year\">$value</option>"; }echo '</select>';</form>---------------------------------------------------------------------------------------------------I can get the calendar to display on the form page. However I cannot get what has been selected on the the calendar to then display on the resultsform.php page. I've tried something like the following below to get the results to display, but it does not work(resultsform.php)--------------------------------------------------------------------------------------------------$birthdate = "$day $month $year";echo $birthdate;---------------------------------------------------------------------------------------------------I think I'm missing a rule perhaps in the calendar, but I've made other <select> tags in forms display on another page with no problem. Although these did not involve using 'for' and 'foreach' loops. Has anyone got any ideas on how to solve what is probably something fairly easy (lol)?Any advice ideas are greatly appreciated :)

Link to post
Share on other sites
Add the $ at the beginning of the month, day, year values like this.echo"\n<option value=\"$year\">$value</option>";
Hmmm, I've given it a try, but no joy.I made a slight adjustment to the calendar in the $month array and added ....(' '=>'Month', 'January' => 'January'.....).. and then added the following if statement into my resultsform.php page... if ($month == ' '){ $problem = TRUE; echo'<p>No Month has been entered.</p>'; }Having done this, I've found through testing, the $month result is not being sent.
Link to post
Share on other sites

First off, this is really unneccessary:$month = array ('January' => 'January', 'February' => 'February', 'March'=>'March', 'April' => 'April', 'May'=>'May', 'June'=>'June', 'July'=>'July', 'August'=>'August', 'September'=>'September', 'October'=>'October', 'November' =>'November', 'December'=>'December');You can just do this:$month = array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');You don't gain anything by having the same values for the key and the value, it doesn't add anything.And this line:echo"\n<option value=\"day\">$day</option>";should be this:echo"\n<option value=\"$day\">$day</option>";This line:echo"\n<option value=\"month\">$value</option>";should be this:echo"\n<option value=\"$value\">$value</option>";And this line:echo"\n<option value=\"year\">$value</option>";should be this:echo"\n<option value=\"$value\">$value</option>";Also, you don't need this line at all:$day = range (1,31);

Link to post
Share on other sites
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...