PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 9th, 2004, 05:01 PM
Scot Scot is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 29 Scot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 17 sec
Reputation Power: 0
Passing a value from a form field to my function

OK I am back. Who could you resist? You get some damn good suggestions in this forum. My current dilemma is I am trying to pass a variable from a form to a function. Has anyone out there got a snippet to share. I have an excellent function called "getAgeByDate" which calculates age. The function looks like this

<?php
function getAgeByDate ($iMonth, $iDay, $iYear, $nMonth=0, $nDay=0, $nYear=0) {
// simple mm/dd/yy(yy) to age conversion.
// Copyright (C)2001 Servability Ltd. Beerware!!
//Added the 3 lines below
global $iMonth;
global $iDay;
global $iYear;
////
If (!$nMonth) $nMonth = date("m");
If (!$nDay) $nDay = date("d");
If (!$nYear) $nYear = date("Y");
If ($iYear < 100) {
$iYear = 1900 + $iYear;
}
$baseyear = $nYear - $iYear - 1;
If ($iMonth < $nMonth OR ($iMonth == $nMonth AND $iDay <= $nDay)) {
// had birthday
$baseyear++;
}

return $baseyear;
}
?>
My current problem is I want to develop a simple form to pass the values $iMonth, $iDay, and $iYear to the function and have the resulting age display in my form. I just can't figure out how to feed the function these three variables and then how to have the result of the function display in my form. Any help would be hugely appreciated. Thanks in Advance!
I have been pulling my hair out on this for several days.
Thanks for any and all suggestions!!!!

Reply With Quote
  #2  
Old June 10th, 2004, 08:14 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Perhaps you should consider reading a basic tutorial. This is one of the most fundamental things you can do in PHP, and once you learn how, lots of doors will open for you. To get you started, consider the following partial code:

PHP Code:
//If there are post variables (as from a form)
if($_POST){
    print 
getAgeByDate($_POST["iMonth"]);
}
else{
    print 
"<form><input type=\"text\" name=\"iMonth\"><input type=\"submit\" value=\"Submit\"></form>";

__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old June 10th, 2004, 12:34 PM
Scot Scot is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 29 Scot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 17 sec
Reputation Power: 0
I have read a multitude of tutorials but none that provided any real help with this piece of code I am trying to create Thats why I posted here. In the past people have been very helpful. I do not aspire to be a top gun programmer just need some guidance because I am not a programmer. I appreciate the code snippet but it really doesn't elighten me as to how to get to where I want to go. It seems that it pulls the arguments into the function using using the POST function. If you could remark the code a little bit better perhaps it would waken my dull mind. Thanks Again!

Reply With Quote
  #4  
Old June 10th, 2004, 02:02 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
$_POST isn't a function, but is rather an array. It contains the values sent from a form that uses the POST method. If you submit a form, the values in its fields just automatically exist in $_POST, with values corresponding to the field names. So you can get at the value of a form field named "myfield" by using the syntax $_POST["myfield"]. Similarly, there's a $_GET array that contains items sent through a form that uses the GET method (or links with key=value parameters in the URL).

I didn't mean to suggest that you were dull, but I have a really hard time believing that anybody can have read any tutorial about creating a small Web app without getting this basic stuff down. If no such tutorial exists, somebody should definitely write one. There's really no way to apply further relevant comments to the code snippet I provided. We're checking for POST variables and either printing the form or printing the age depending on whether or not we have them. I'll attempt a further clarification.

If the $_POST array exists, we know that input has been posted from a form, so we attempt to process it by passing the relevant variables to the function. Else we know we need to print the form. That's all my code snippet does.

To further elaborate, I'd need to know precisely what your form looks like. For example, you make reference to having three fields to hold date info but you don't mention a field to hold the calculated age. Is it the case that you have another field within the same form to contain this info or do you actually just wish to print the age without reprinting the form? If the former, you'd do something like take the $_POST vars (if any), and set a variable ($age, maybe) to the value returned by the function (in my code above, rather than printing the return, you assign it to the variable). Then you print your form, providing $age as the value for the age field. If the form's been posted, the field will display the calculated value; else it'll display nothing.

Reply With Quote
  #5  
Old June 10th, 2004, 04:04 PM
Scot Scot is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 29 Scot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 17 sec
Reputation Power: 0
Here is the form. And yes I do want the resultant age to appear in a field as opposed to just printing out to the page. Thanks for your patience and your help!

<form>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
Month Person was born<input type="text" name="iMonth" size="20"><BR>
Day Person was born<input type="text" name="iDay" size="20"><BR>
Year Person was born<input type="text" name="iYear" size="20"><BR>
Calculated Age<input type="text" name="result" value="<?echo getAgeByDate($_POST["iMonth"],$_POST["iDay"],$_POST["iYear"])?>"size="3"><BR>

<input type="submit" value="Calculate Age">

</head>
<body>

</body>
</html>

Reply With Quote
  #6  
Old June 11th, 2004, 08:16 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I made a few changes to your code, and it works fine. The first problem is that your HTML syntax is screwed. This causes some weird issues with how the form is processed. (It was sending my variables in the query string rather than in the post request, for example.) Note that you've got no beginning <html> tag, an extra <form> tag, and no ending </form> tag. You've also got your form in the (unopened) <head> of your document rather than in the body. I've switched things around so that the HTML surrounding your form is valid.

Then I tweaked your function. There's no need to declare the parameters global, since you're passing them to the function. I suspect you did this in an attempt to get around the bizarre issues your HTML syntax was causing. So I've removed those. Additionally, on the initial page load, I was getting 104 as the calculated age because your function was executing with blank values, since there was no $_POST data to pass on the first page load. Now I check each required parameter to make sure it's a number. If not, I just return blank.

Note also that I combined the two files into one. You may already have been doing this, but I wanted to note it just in case.

PHP Code:
<?php
                                                                                                                                                   
function getAgeByDate ($iMonth$iDay$iYear$nMonth=0$nDay=0$nYear=0) {
    
// simple mm/dd/yy(yy) to age conversion.
    // Copyright (C)2001 Servability Ltd. Beerware!!
    
if(!is_numeric($iMonth) || !is_numeric($iDay) || !is_numeric($iYear)){
        return;
    }
    If (!
$nMonth$nMonth date("m");
    If (!
$nDay$nDay date("d");
    If (!
$nYear$nYear date("Y");
    If (
$iYear 100) {
        
$iYear 1900 $iYear;
    }
    
$baseyear $nYear $iYear 1;
    If (
$iMonth $nMonth OR ($iMonth == $nMonth AND $iDay <= $nDay)) {
        
// had birthday
        
$baseyear++;
    }
                                                                                                                                                   
    return 
$baseyear;
}
                                                                                                                                                   
?>
                                                                                                                                                   
<html>
<head>
</head>
<body>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
Month Person was born<input type="text" name="iMonth" size="20"><BR>
Day Person was born<input type="text" name="iDay" size="20"><BR>
Year Person was born<input type="text" name="iYear" size="20"><BR>
Calculated Age<input type="text" name="result" value="<?echo getAgeByDate($_POST["iMonth"],$_POST["iDay"],$_POST["iYear"])?>"size="3"><BR>
                                                                                                                                                   
<input type="submit" value="Calculate Age">
</form>
                                                                                                                                                   
</body>
</html>

Reply With Quote
  #7  
Old June 11th, 2004, 10:17 AM
Scot Scot is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 29 Scot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 17 sec
Reputation Power: 0
Wow Thank You Thank YOu Thank You! I sorry to be such a newbie. But coding this stuff is obviously not one of my strenghts yet! But I am going to keep plugging away at it. I will study what you've done in an attempt to reuse the same procedure again. You will probably get a snicker out of the fact that I have been working on trying to get this piece of code working for several weeks. Sometimes I think I am hopeless. Again Thank You! Thank You! Thank You! Have an Awesome Day!
Scot

Reply With Quote
  #8  
Old June 11th, 2004, 10:25 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
We all get hung up on issues. I can't count the number of times I've stared at an obvious error for hours before finally realizing what a stupid mistake I had made. Glad I could help out in this case.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Passing a value from a form field to my function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT