zacb Deinonychus


Joined: May 08, 2012 Posts: 360
|
Posted: Sun May 27, 2012 11:49 am Post subject: Question about php |
|
|
| Code: | // $_POST['zipcode'] holds the value of the submitted form parameter
// "zipcode"
$zipcode = trim($_POST['zipcode']);
// Now $zipcode holds that value, with any leading or trailing spaces
// removed
$zip_length = strlen($zipcode);
// Complain if the ZIP code is not 5 characters long
if ($zip_length != 5) {
print "Please enter a ZIP code that is 5 characters long.";
} |
I understand most of this, but I have one question. What exactly is $_POST? Thanks |
|
| Back to top |
|
Blownmind Phoenix


Joined: Feb 19, 2012 Age: 33 Posts: 823 Location: Norway
|
Posted: Sun May 27, 2012 1:06 pm Post subject: Re: Question about php |
|
|
| zacb wrote: | | I understand most of this, but I have one question. What exactly is $_POST? Thanks |
It's when you use a submit-button on a form, and the phpscript is called with that method. The info from the form can be gathered from $_POST. Google.com is your friend in need if you wonder about more.  _________________ AQ: 42/50 || SQ: 32/80 || IQ(RPM): 138 || IRI-empathytest(PT/EC/FS/PD): 10(-7)/16(-3)/19(+3)/19(+10) || Alexithymia: 148/185 || Aspie-quiz: AS 133/200, NT 56/200 |
|
| Back to top |
|
sliqua-jcooter Phoenix


Joined: Jan 26, 2010 Posts: 814 Location: Burke, Virginia, USA
|
Posted: Sun May 27, 2012 3:33 pm Post subject: |
|
|
That code is, incidentally, too simple. It's a good thing to do input validation - however this example excludes valid zipcodes, and potentially allows entries that do not conform to the zipcode pattern.
That code looks for any string of 5 characters, and rejects the input if they don't have exactly 5 characters (it already trims off leading and trailing whitespace). However, zipcodes don't contain letters, only numbers - so this code would allow an invalid zipcode entry of "abcde".
Also, 5 digit zipcodes are also technically "incomplete", as the postal service uses an expanded zipcode format of 5 digits, a hyphen, and 4 more digits. Thus, 22032-4010 is just as valid an input as 22032, and it is erroneous to exclude the input on that basis.
Taken a step further - your validation code could actually validate that the given zip code actually exists by doing a database lookup, but that is in many cases impractical.
I realize you are still likely learning, but it's important to keep in mind things like this to prevent learning bad/lazy habits. _________________ Nothing posted here should be construed as the opinion or position of my company, or an official position of WrongPlanet in any way, unless specifically mentioned. |
|
| Back to top |
|
NeantHumain Phoenix

![]()
Joined: Jun 25, 2004 Posts: 5119 Location: St. Louis, Missouri
|
Posted: Sun May 27, 2012 6:24 pm Post subject: |
|
|
| sliqua-jcooter wrote: | That code is, incidentally, too simple. It's a good thing to do input validation - however this example excludes valid zipcodes, and potentially allows entries that do not conform to the zipcode pattern.
That code looks for any string of 5 characters, and rejects the input if they don't have exactly 5 characters (it already trims off leading and trailing whitespace). However, zipcodes don't contain letters, only numbers - so this code would allow an invalid zipcode entry of "abcde".
Also, 5 digit zipcodes are also technically "incomplete", as the postal service uses an expanded zipcode format of 5 digits, a hyphen, and 4 more digits. Thus, 22032-4010 is just as valid an input as 22032, and it is erroneous to exclude the input on that basis.
Taken a step further - your validation code could actually validate that the given zip code actually exists by doing a database lookup, but that is in many cases impractical.
I realize you are still likely learning, but it's important to keep in mind things like this to prevent learning bad/lazy habits. |
So you're basically telling him to use a regular expression along the lines of /(\d{5})(\-\d{4})?/. |
|
| Back to top |
|
zacb Deinonychus


Joined: May 08, 2012 Posts: 360
|
Posted: Sun May 27, 2012 8:34 pm Post subject: |
|
|
| So is this similar to gets or scanf in C? Basically they capture the input and send it to a variable? |
|
| Back to top |
|
sliqua-jcooter Phoenix


Joined: Jan 26, 2010 Posts: 814 Location: Burke, Virginia, USA
|
Posted: Sun May 27, 2012 8:46 pm Post subject: |
|
|
| zacb wrote: | | So is this similar to gets or scanf in C? Basically they capture the input and send it to a variable? |
When the browser loads the page, the POST and/or GET data is passed to the php interpreter, which assigns that information to the $_POST $_GET, etc. variables. Those variables (and a few others) are available to the running program to use the information provided. _________________ Nothing posted here should be construed as the opinion or position of my company, or an official position of WrongPlanet in any way, unless specifically mentioned. |
|
| Back to top |
|
zacb Deinonychus


Joined: May 08, 2012 Posts: 360
|
Posted: Sun May 27, 2012 8:54 pm Post subject: |
|
|
| so $zipcode would be a post variable? |
|
| Back to top |
|
sliqua-jcooter Phoenix


Joined: Jan 26, 2010 Posts: 814 Location: Burke, Virginia, USA
|
Posted: Sun May 27, 2012 9:08 pm Post subject: |
|
|
| zacb wrote: | | so $zipcode would be a post variable? |
$zipcode is the variable used in the script, it's defined by the script itself. $_POST is the variable assigned to all HTTP POST values, it's used as an array, and it's assigned by the php interpreter.
That script assigns the value of the zipcode POST submission to the $zipcode local variable. _________________ Nothing posted here should be construed as the opinion or position of my company, or an official position of WrongPlanet in any way, unless specifically mentioned. |
|
| Back to top |
|
zacb Deinonychus


Joined: May 08, 2012 Posts: 360
|
Posted: Sun May 27, 2012 9:35 pm Post subject: |
|
|
| so the $_POST is for sending data, and the other is a variable that stores it. (sorry for being a pain) |
|
| Back to top |
|
sliqua-jcooter Phoenix


Joined: Jan 26, 2010 Posts: 814 Location: Burke, Virginia, USA
|
Posted: Sun May 27, 2012 9:42 pm Post subject: |
|
|
| zacb wrote: | | so the $_POST is for sending data, and the other is a variable that stores it. (sorry for being a pain) |
They're both variables. That script takes the value of $_POST['zipcode'], trims any surrounding whitespace, and assigns that value to $zipcode. _________________ Nothing posted here should be construed as the opinion or position of my company, or an official position of WrongPlanet in any way, unless specifically mentioned. |
|
| Back to top |
|
zacb Deinonychus


Joined: May 08, 2012 Posts: 360
|
Posted: Sun May 27, 2012 10:00 pm Post subject: |
|
|
| Gotcha. So it would be the same as saying in C: " int car= car+1" . Kind of the same thing? Just like car is still an int, zipcode is still $_POST code? |
|
| Back to top |
|
sliqua-jcooter Phoenix


Joined: Jan 26, 2010 Posts: 814 Location: Burke, Virginia, USA
|
Posted: Sun May 27, 2012 10:57 pm Post subject: |
|
|
| zacb wrote: | | Gotcha. So it would be the same as saying in C: " int car= car+1" . Kind of the same thing? Just like car is still an int, zipcode is still $_POST code? |
Not quite. PHP isn't strictly object-oriented, so it doesn't have a rigid concept of variable types. Basically, every variable in PHP is a string, unless you specifically make it an object.
it's more analogous to the following:
int car = 1;
int rac = car;
The value of the form submitted with the name "zipcode" (this is all HTML at this point), is passed to the web server from the browser, which passes that POST data to the php interpreter, which automatically assigns those values to an array, which it calls $_POST. This is all done before your script runs. Therefore, once your script runs, it does some slight manipulation of that raw POST data, and stores it into a local variable for convenience. _________________ Nothing posted here should be construed as the opinion or position of my company, or an official position of WrongPlanet in any way, unless specifically mentioned. |
|
| Back to top |
|
zacb Deinonychus


Joined: May 08, 2012 Posts: 360
|
Posted: Sun May 27, 2012 11:03 pm Post subject: |
|
|
| Well I guess that is what I meant. But it is still an int , not a char or pointer. |
|
| Back to top |
|
Burzum Indeed


Joined: Apr 27, 2011 Posts: 1205
|
Posted: Sun May 27, 2012 11:50 pm Post subject: |
|
|
| sliqua-jcooter wrote: |
PHP isn't strictly object-oriented, so it doesn't have a rigid concept of variable types. |
Neither is C. Did you mean it isn't statically typed?
To the OP: Do you know how the form tag works in HTML? |
|
| Back to top |
|
zacb Deinonychus


Joined: May 08, 2012 Posts: 360
|
Posted: Mon May 28, 2012 8:41 am Post subject: |
|
|
| Kind of. I had to look it up. I am learning php and html side by side. Maybe I should do html first. |
|
| Back to top |
|
|
|