php get value month,day,year

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
php_postgresql
Forum Newbie
Posts: 13
Joined: Sat Apr 19, 2008 9:37 am

php get value month,day,year

Post by php_postgresql »

Date of purchase: <?php datex(); $purchasemonth=$month; $purchaseday=$day; $purchaseyear=$year; ?>

i have the code above the function datepurchase are drowdowns that display month, day and year... i have no prob when i access the $month, $day and $year because these are the values directy from the name of the dropdown...but im storing them in another variable because other stuff will also use the same function... for example

Date of payment <?php datex(); $paymentmonth=$month; $paymentday=$day; $paymentyear=$year; ?>

when i submit the page and go to the other page....
it doesnt capture the values in $purchasemonth etc......
but it can capture the values $month.......etc....
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: php get value month,day,year

Post by Kadanis »

Sounds like you have the php setting register_globals (i think) turned on. This means you can call form objects like dropdowns directly from their name, in your example $month or $year.

However, normal variables like $purchaseyear do not carry over between pages unless you pass them somehow. GET, POST or SESSION are examples.

On a side note, it is not the safest/best way to rely on register_globals being on, as most server admin's turn this setting off on live web servers.

Really you should be getting the data out of the form using the GET or POST super-globals.

For example

Code: Select all

 
<!-- FORM CODE -->
<form id="dates" method="post" action="page.php">
<input name="name" value="" type="text" />
<select name="day"><option value="1">Mon</option><option value="2">Tue</option></select>
</form>
 

Code: Select all

 
#php code to retrieve form data
if (isset($_POST['name'])){  #this checks if the super-global is actually set i.e the form has been posted
    $name = $_POST['name']; #set the variable name to the value in the super-global
} else {
    $name = ''; #set the variable $name to a blank value.
}
 
#personally i prefer to right these lines like this 
$name = (isset($_POST['name'])) ? $_POST['name'] : '';
$day= (isset($_POST['day'])) ? $_POST['day'] : '';
 
 
Then to move the values around between pages, you could either pass them on the URL then use the $_GET super global to retrieve them or pass them in the $_SESSION

Eg1 On the URL:

Code: Select all

 
#page 1
header('location: http://www.yousite.com/dir/page2.php?day=' . $day . '&month=' . $month & .'&year=' . $year);
 
#page 2
$day = $_GET['day'];
$month = $_GET['month'];
 
Eg2 using the session

Code: Select all

 
#page 1
session_start();
 
$_SESSION['day'] = $day;
$_SESSION['month'] = $month;
$_SESSION['year'] = $year;
 
#page 2
session_start();
 
$day = $_SESSION['day']; 
$month= $_SESSION['month']; 
$year= $_SESSION['year']; 
 
 
Sorry for the wall-o-textTM hope this helps you out. :P
Post Reply