Page 1 of 1 [ 16 posts ] 

zacb
Veteran
Veteran

User avatar

Joined: 7 May 2012
Age: 30
Gender: Male
Posts: 1,194

27 May 2012, 10:49 am

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



Blownmind
Veteran
Veteran

User avatar

Joined: 18 Feb 2012
Age: 45
Gender: Male
Posts: 825
Location: Norway

27 May 2012, 12:06 pm

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. :wink:


_________________
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


sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 37
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

27 May 2012, 2:33 pm

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.


NeantHumain
Veteran
Veteran

User avatar

Joined: 24 Jun 2004
Age: 45
Gender: Male
Posts: 4,837
Location: St. Louis, Missouri

27 May 2012, 5:24 pm

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})?/.



zacb
Veteran
Veteran

User avatar

Joined: 7 May 2012
Age: 30
Gender: Male
Posts: 1,194

27 May 2012, 7:34 pm

So is this similar to gets or scanf in C? Basically they capture the input and send it to a variable?



sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 37
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

27 May 2012, 7:46 pm

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.


zacb
Veteran
Veteran

User avatar

Joined: 7 May 2012
Age: 30
Gender: Male
Posts: 1,194

27 May 2012, 7:54 pm

so $zipcode would be a post variable?



sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 37
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

27 May 2012, 8:08 pm

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.


zacb
Veteran
Veteran

User avatar

Joined: 7 May 2012
Age: 30
Gender: Male
Posts: 1,194

27 May 2012, 8:35 pm

so the $_POST is for sending data, and the other is a variable that stores it. (sorry for being a pain)



sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 37
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

27 May 2012, 8:42 pm

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.


zacb
Veteran
Veteran

User avatar

Joined: 7 May 2012
Age: 30
Gender: Male
Posts: 1,194

27 May 2012, 9:00 pm

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?



sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 37
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

27 May 2012, 9:57 pm

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.


zacb
Veteran
Veteran

User avatar

Joined: 7 May 2012
Age: 30
Gender: Male
Posts: 1,194

27 May 2012, 10:03 pm

Well I guess that is what I meant. But it is still an int , not a char or pointer.



Burzum
Veteran
Veteran

User avatar

Joined: 26 Apr 2011
Age: 33
Gender: Male
Posts: 1,205

27 May 2012, 10:50 pm

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?



zacb
Veteran
Veteran

User avatar

Joined: 7 May 2012
Age: 30
Gender: Male
Posts: 1,194

28 May 2012, 7:41 am

Kind of. I had to look it up. I am learning php and html side by side. Maybe I should do html first.



sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 37
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

28 May 2012, 8:47 am

Burzum wrote:
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?


yes, that's what I meant, thank you.

I suppose that's why I'm an engineer, not a developer.


_________________
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.