hi.. newbie here. regarding post or get methods
Moderator: General Moderators
hi.. newbie here. regarding post or get methods
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
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
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Have you tried it?
Take the form, post it, and run this on the page:
Take the form, post it, and run this on the page:
Code: Select all
print_r($_POST);No.claws wrote:1. is it null ??
No.claws wrote:2. is a variable being null is same as $_POST['phone'] = '';
empty() does both at once.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'] != '')
{
You'd just need to use $_GET instead of $_POST. Same data though.claws wrote:4. does the scene change if i use GET instead of POST
Last edited by superdezign on Tue Aug 14, 2007 8:23 am, edited 1 time in total.
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
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.
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.
yeah.. thats true but my point is.It will be the same with GET, but you will see the name=value pairs in the URL.
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 ??
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.