|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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!!!! |
|
#2
|
||||
|
||||
|
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:
__________________
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. |
|
#3
|
|||
|
|||
|
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!
|
|
#4
|
||||
|
||||
|
$_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. |
|
#5
|
|||
|
|||
|
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> |
|
#6
|
||||
|
||||
|
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:
|
|
#7
|
|||
|
|||
|
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 |
|
#8
|
||||
|
||||
|
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.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Passing a value from a form field to my function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|