Page 1 of 1

php get value month,day,year

Posted: Wed Apr 23, 2008 8:29 pm
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....

Re: php get value month,day,year

Posted: Thu Apr 24, 2008 2:25 am
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