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?
| SitePoint Sponsor |





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?

this of course assumes that the form is posted, I meanPHP Code:if (empty($_POST)) {
// the form is not posted
} else {
// the form was posted
}
A quick way to hide errors is to add a @ before $_POST when you call it, likeHTML Code:<form action="some_script.php" method="post">
PHP Code:$username = @$_POST['username'];
// or
$form_data_array = @$_POST;





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


This is due to an option in php.ini which tells it to throw back any notices and warnings. Undefined variables will throw out a notice, but it the script will still function.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)





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.


In that case, just do what ssttoo said. Put the @ sign before the $_POST bit, such as his example:Originally Posted by subnet_rx
PHP Code:$username = @$_POST['username'];
// or
$form_data_array = @$_POST;
-Melchior (Stephen Craton)





that's not working for some reason. What I have tried isOriginally Posted by melchiorus
short variable names are on btw.PHP Code:$array = 'item'=>@$_POST['item']


Just to clarify here...you just typed that in shorthand right? It should be like this, shouldn't it?Originally Posted by subnet_rx
What exactly is the error? Can you copy and paste it?PHP Code:$array = array('item' => $_POST['item']);
-Melchior (Stephen Craton)
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





Just make sure $_POST['item'] has value.
PHP Code:$item = (!empty($_POST['item'])) ? $_POST['item'] : null;





will that work in an array?

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 ...
This will first test whether the POST variable is set then (ONLY IF IT IS SET) check it against an empty value before assigning the value to $item.PHP Code:$item = (isset($_POST['item']) && !('' == $_POST['item'])) ? $_POST['item'] : NULL;
Regards,
DK
David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
Client and (unpaid) WHB Ambassador
Updated mod_rewrite Tutorial Article (setup, config, test & write
mod_rewrite regex w/sample code) and Code Generator





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.


Erm.... How about
or did I misunderstand the original question?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)


or maybe...
will limit your output array to just those fields you want to see.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...
if you add an "else" to the code, you can make sure that all keys are set to something, even if it is "null";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)





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