Hi Friend,
I understand the problems that beginners face. But I want to ask one, thing, do you have a logic ready in your mind. Logic is how the code is going to execute which doesn't imply any language boundary. This can be a simple flowchart or simple english like points approach. Here's an example of the latter.
1. First, the page will display three select boxes -> Day, Month, Year. This implies that I have to use some html to display this.
2. When a user will select the values and hit Submit button (implies, I have a form element in HTML), the data is going to some PHP script.
My PHP script is the same page that generated the HTML form (say, index.php).
3. This implies that I should have a method to detect if a user has not just loaded a page but rather has sent the form data to it.
This also implies that a branching is used. i.e. if-else block is used.
Like, If submitted == True, Then
// Do Something to process the form data and display the result
Else // Display the form to let the user input the data
4. Now, if user had submitted the data to me using the form, I have to get that data too and store it in a temporary variable also.
This depends on the method via which the form is submitted. For starters, I would say, there are two methods GET and POST, via which
a form data is submitted. When you see form data submitted inside a url like http://example.com/process.php?data=somedata&data2=something
This is the example of GET method. While in POST method, the data is posted in different way and you will not see any query string in
the url.
5. I should be using GET method at the moment, so I will have a method in PHP to get this data. this is $_GET['data']. Whatever is inside
the single quote is what the name attribute of the form elements like Textboxes, Selectboxes etc. What you get is the value posted to you.
6. Now that we have stored all the variables in the page, we will be processing the details ... Here's an example (Highly Commented in PHP)
if (isset($_GET['submit'])) {
$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
$timestamp = strtotime($day.'/'.$month.'/'.$year);
$date = date('M/j/Y ', $timestamp);
echo $date;
echo '<br /> You have entered the date : '. $date;
}
else {
echo '
<form method="get" action="index.php">
<select name="day">
';
for ($i = 1; $i <= 31; $i++) {
echo '<option value="'. $i .'">'. $i .'</option>';
}
echo '</select> <br />
<select name="month">
';
$months = array (
1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec'
);
foreach ($months as $key=>$value) {
echo '<option name="'. $key .'">'. $value .'</option>';
}
echo '</select><br />
<select name="year">
';
$year = date('Y', time());
for ($i = $year - 20; $i <= $year + 20; $i++) {
echo '<option name="'. $i .'">'. $i .'</option>';
}
echo '</select>
<input type="hidden" name="submit" value="true" />
<input type="submit" value="Submit" />
</form>
';
}
?>
Hope that this simple example will help you solve the problem. Have a happy programming.
With Regards
Tushar Srivastava