SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
Thread: !$_post
-
Aug 18, 2004, 11:47 #1
- Join Date
- Aug 2001
- Location
- Hattiesburg, MS
- Posts
- 1,085
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
!$_post
I'm trying to grab all values from a form and put them into an array, but it's generating errors if the POST value isn't there. How do I get around this?
-
Aug 18, 2004, 12:06 #2
- Join Date
- Jan 2004
- Location
- LA, California
- Posts
- 123
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:if (empty($_POST)) {
// the form is not posted
} else {
// the form was posted
}
HTML Code:<form action="some_script.php" method="post">
PHP Code:$username = @$_POST['username'];
// or
$form_data_array = @$_POST;
-
Aug 18, 2004, 12:14 #3
- Join Date
- Aug 2001
- Location
- Hattiesburg, MS
- Posts
- 1,085
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yeah, the second solution is what I am looking for, the form is being posted, that's my first check, but not all the variables on the form are filled out. So it throws an undefined error
-
Aug 18, 2004, 13:49 #4
- Join Date
- Jun 2004
- Location
- Indiana
- Posts
- 283
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by subnet_rx
The thing is, if the form has the field and the right name, you couldn't get undefined index errors since the value would be blank, that is $_POST['field'] = '';.
Check to make sure the field is there and with the correct name. If that doesn't work, try using $HTTP_POST_VARS since shorttags might also be off.-Melchior (Stephen Craton)
-
Aug 18, 2004, 14:00 #5
- Join Date
- Aug 2001
- Location
- Hattiesburg, MS
- Posts
- 1,085
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
the fields are automatically generated from a database based on previous selections, so in fact, the field may not be there. Should I maybe put in hidden values as filler? I thought there would be a much cleaner way to handle it in PHP though.
-
Aug 18, 2004, 14:16 #6
- Join Date
- Jun 2004
- Location
- Indiana
- Posts
- 283
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by subnet_rx
PHP Code:$username = @$_POST['username'];
// or
$form_data_array = @$_POST;
-Melchior (Stephen Craton)
-
Aug 18, 2004, 14:45 #7
- Join Date
- Aug 2001
- Location
- Hattiesburg, MS
- Posts
- 1,085
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by melchiorus
PHP Code:$array = 'item'=>@$_POST['item']
-
Aug 18, 2004, 17:43 #8
- Join Date
- Jun 2004
- Location
- Indiana
- Posts
- 283
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by subnet_rx
PHP Code:$array = array('item' => $_POST['item']);
-Melchior (Stephen Craton)
-
Aug 18, 2004, 18:26 #9
- Join Date
- Apr 2004
- Location
- Germany
- Posts
- 90
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Don't hide errors by using @
But back ontopic:
If you don't know whether a field was submitted by the form, use isset() or empty() before you access that field. Always check if the data you want to use actually is there
(in fact, I think error_reporting(E_ALL) should really be used while developing, but that's another story...)remember my name - you'll scream it later
-
Aug 18, 2004, 20:00 #10
- Join Date
- Sep 2002
- Location
- The Restaurant at The End of The Universe
- Posts
- 1,423
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just make sure $_POST['item'] has value.
PHP Code:$item = (!empty($_POST['item'])) ? $_POST['item'] : null;
-
Aug 18, 2004, 20:51 #11
- Join Date
- Aug 2001
- Location
- Hattiesburg, MS
- Posts
- 1,085
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
will that work in an array?
-
Aug 18, 2004, 21:20 #12
- Join Date
- Feb 2002
- Location
- Auckland
- Posts
- 14,692
- Mentioned
- 20 Post(s)
- Tagged
- 3 Thread(s)
subnet,
Jump uses almost the identical solution I use (except that his does not allow either a value of 0 or an empty value (an empty string)). Give this a go ...
PHP Code:$item = (isset($_POST['item']) && !('' == $_POST['item'])) ? $_POST['item'] : NULL;
Regards,
DKDavid K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
Client and (unpaid) WHB Ambassador
mod_rewrite Tutorial Article (setup, config, test & write
mod_rewrite regex w/sample code) and Code Generator
-
Aug 19, 2004, 08:13 #13
- Join Date
- Sep 2002
- Location
- The Restaurant at The End of The Universe
- Posts
- 1,423
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah, a key can point to null in an array. If you don't mind having null values. It's kind of vague what you are really trying to accomplish but that will work. Ideally, everything in an array has a value. Even if you create a placemarker like 'none' or 'N/A'. Then just check if it's != 'none' when outputing values. I'm sure you can also just check if it's != null also.
-
Aug 19, 2004, 08:16 #14
- Join Date
- Nov 2003
- Location
- England
- Posts
- 293
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Erm.... How about
PHP Code:if($_POST){
foreach($_POST as $k=>$v){
$myNewArray[$k] = $v;
}
}
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)
-
Aug 19, 2004, 08:21 #15
- Join Date
- Nov 2003
- Location
- England
- Posts
- 293
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
or maybe...
PHP Code:$keysToGet = array ("username","image","favourite_pie","Monkey","Eyes");
foreach($keysToGet as $k){
if($_POST[$key]){
$myNewArray[$key] = $_POST[$key];
}
}
your array $myNewArray would then be something like...
PHP Code:$myNewArray = array (
"username" => "posted_user_name",
"image" => "posted url to image",
"Eyes" => "yes thanks"
)
Your mind is like a parachute. It works best when open.
(HH The Dalai Lama)
-
Aug 19, 2004, 08:36 #16
- Join Date
- Aug 2001
- Location
- Hattiesburg, MS
- Posts
- 1,085
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm trying to do a shopping cart actually and so my next page tries to set every value on the form page to a value through the code I posted above. But, if they don't select an item in a category, then I get an error. I think I'm going to add in a choice to "Select None" so it will be a little more clear to the consumer and I'll be able to pass a null value to the array.
Bookmarks