hi.. newbie here. regarding post or get methods

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

hi.. newbie here. regarding post or get methods

Post by claws »

hai everyone..

I am a newbie.

my doubt is..

1. I have a form and i am using post method to submit the form.
2. say the text field with name="phone"
3. now if the user do not enter anything in that text field and he submits it
then:

what will be $_POST['phone'] ????

1. is it null ??
2. is a variable being null is same as $_POST['phone'] = '';
3. say. for me phone is must. i want to show error. if its not entered.
if i use isset($_POST['phone']) is it sufficient
or should i check like this
if(isset($_POST['phone']))
{
if($_POST['phone'] != '')
{

4. does the scene change if i use GET instead of POST
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Have you tried it?

Take the form, post it, and run this on the page:

Code: Select all

print_r($_POST);
claws wrote:1. is it null ??
No.
claws wrote:2. is a variable being null is same as $_POST['phone'] = '';
No.
claws wrote:3. say. for me phone is must. i want to show error. if its not entered.
if i use isset($_POST['phone']) is it sufficient
or should i check like this
if(isset($_POST['phone']))
{
if($_POST['phone'] != '')
{
empty() does both at once.
claws wrote:4. does the scene change if i use GET instead of POST
You'd just need to use $_GET instead of $_POST. Same data though.
Last edited by superdezign on Tue Aug 14, 2007 8:23 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If I were you, I'd look up empty

It's very useful in instances like this. Even if it is not empty you still would probably want to do additional checks such as only numeric characters if number required, money format is money required, not just space if a value is required (trim useful here) etc after.
Last edited by CoderGoblin on Tue Aug 14, 2007 8:17 am, edited 1 time in total.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

If phone is left empty the $_POST['phone'] will be empty ( = ''; )

isset($_POST['phone']) will be true if you submit text field with name phone, even with no value. You may be interested of empty();


It will be the same with GET, but you will see the name=value pairs in the URL.
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

Post by claws »

It will be the same with GET, but you will see the name=value pairs in the URL.
yeah.. thats true but my point is.
say if 'firstname' and 'lastname' submitted then.
it will be
http://url/?name=somename&lastname=somename

but there will be no phone=23423523523 in the url
so its like undeclared variable.
since there is nothing like declaration in php.
the variable is unassigned.

so
the variable $_POST['phone'] should be either NULL or unset.

but how come the variable is set ??
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

claws wrote:but there will be no phone=23423523523 in the url
What...? Have you tried it? If it's an empty value, the value still exists.
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

Post by claws »

oh!!!!!!!!!!!
yeah.. its tue..
and i have tried print_r($_POST);
its printing all the variables even they are not entered.
so even if the client doesnt enter anything.. the value will be intiated to an empty string like ='';

but this is not the case with all the fields..

i faced this problem because i was accessing the values of SELECT,CHECKBOX,RADIO type input fields. for fields of this type the variable remains unset.
or rather is set only if its checked or selected.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Of course, if checkbox is not checked it is not posted with the form.

You can use isset() for checkboxes/radio buttons and !empty() for text inputs.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

The way they work is that they do have to have a value to be posted. However, all text input defaults to an empty value (unless specified to default to something else). Dropdown <select>s default to their first value (unless another one is specified as selected). Checkbox defaults to not being checked (meaning it doesn't 'exist'), and the radio buttons default to not having a selection.

Checkboxes only send the checked boxes because they are the only ones that matter, as the other technically don't exist at that point. Radio buttons are tricky, and the best way to deal with them is to always have a default value.
Post Reply