How Do I Convert String Values From An Input Form Into Integ

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

How Do I Convert String Values From An Input Form Into Integ

Post by drayarms »

Well the topic may not sound very explicit. So I'll do my best to explain it here. I have a pull down menu as part of a form, which contains the months of the year. I'm trying to store the value of this form entry using the $_POST['month'] variable into a database but first, I need to convert the month values into their corresponding integer values( that is 1 for January, 2 for February etc), in order to easily do date calculations later down the road. Any ideas about how to do this? It may be helpful to include the code for the pull down menu.

Code: Select all


<p><tab> Date of Birth: <?php //Make the month pull down menu.
							//Array to store months. 
							$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); 
							print '<select name= "month">';
							foreach ($months as $key => $value) {
							    print "\n<option value=\"$key\">$value</option>";
							}
							print '</select>'; ?> </tab> 
 

							<tab>  <?php //Make the day pull down menu
							print '<select name= "day">';
							for ($day = 1; $day <= 31; $day++) {
							    print "\n<option value=\"$day\">$day</option>";
							}
							print '</select>'; ?> </tab>

							<tab><?php //Make the year pull down menu 
							print '<select name= "year">';
							$start_year = date('Y');
							for ($y = ($start_year - 18); $y >= 1900; $y--) {
							    print "\n<option value=\"$y\">$y</option>";
							}
							print '</select>'; ?> </tab> </p>


User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How Do I Convert String Values From An Input Form Into I

Post by AbraCadaver »

That's exactly what you're doing. The $key is the integer and the $value is the text name of the month. Do an echo $_POST['month'] and you will have a number.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply