Page 1 of 1
hi.. newbie here. regarding post or get methods
Posted: Tue Aug 14, 2007 7:58 am
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
Posted: Tue Aug 14, 2007 8:14 am
by superdezign
Have you tried it?
Take the form, post it, and run this on the page:
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.
Posted: Tue Aug 14, 2007 8:16 am
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.
Posted: Tue Aug 14, 2007 8:17 am
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.
Posted: Tue Aug 14, 2007 8:38 am
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 ??
Posted: Tue Aug 14, 2007 8:42 am
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.
Posted: Tue Aug 14, 2007 9:07 am
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.
Posted: Tue Aug 14, 2007 9:12 am
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.
Posted: Tue Aug 14, 2007 9:24 am
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.