Reputation: 1887
Alright... so on my forms, I am setting all the fields to something like this:
name="formdata['name']"
and name="formdata['active']"
.
Of course, that means whatever is entered in those fields should be stored in $_POST['formdata']['name']
and $_POST['formdata']['active']
.
I do my values that way because I pass the 'formdata' to a couple of functions I have written (which work as they should.) Now let's say I'm using this to edit an item or add a new item -- the name will go through those functions as it should and the item will save with its name, but 'active' will never save as it should. The entry field is a checkbox -- if it's checked, the value is "1".
If I print_r($_POST['formdata'])
after entering 'Name' and checking 'active', I get this: Array ( ['name'] => Name ['active'] => 1 )
.
Looks fine, right? But when I do the following:
if (!isset($_POST['formdata']['active']) echo "Error 1";
if (empty($_POST['formdata']['active']) echo "Error 2";
if ($_POST['formdata']['active'] != 1) echo "Error 3";
They all return errors! I am baffled by this. Am I overlooking something very simple? I have thought about this for at least 2 hours now.
Upvotes: 2
Views: 3044
Reputation: 48887
Remove the single quotes from the input names in your HTML so it reads:
name="formdata[active]"
Adding the single quotes would mean you would have to access the array in PHP as:
$_POST['formdata']['\'active\'']
or
$_POST['formdata']["'active'"]
which is highly inconvenient.
Upvotes: 4
Reputation: 4701
missing parenthesis:
if (!isset($_POST['formdata']['active'])) echo "Error 1";
if (empty($_POST['formdata']['active'])) echo "Error 2";
Upvotes: 0