Re: Form helper issues "Invalid argument supplied for foreach()"



On Mar 31, 1:04 am, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
George Maicovschi wrote:
On Mar 30, 7:54 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
mejpark wrote:
On Mar 30, 2:47 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
mejpark wrote:>
I've only modified the defaults array--the rest of the code is in
tact. Here it is:
Your problem is your code is checking the value of
$_POST['_submit_check'] but not checking to see if any value is set.
The first time the page is called, this will not be set.

Failing code:

if ($_POST['_submit_check']) {

Should be:

if (isset($_POST['_submit_check'])) {

Or, better yet.

if (isset($_POST['_submit_check']) && $_POST['_submit_check'] == 1) {

Normally I think O'Reilly books are pretty good - but your problems with
code makes me wonder about this one...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================

I wouldn't use an syntax like :
if (isset($_POST['_submit_check']) && $_POST['_submit_check'] == 1)

Because, as I was saying earlier if the request method is GET it WILL
throw an E_NOTICE error. Better keep your logs only for errors,
right? :-)

No, this will NOT throw an E_NOTICE. The isset() call will fail, so the
later test will never be evaluated. Try it and see.

Should use if ($_SERVER['REQUEST_METHOD']=='POST') instead, and if you
really want to validate $_POST['_submit_check'] you should do it like
this:
if ($_SERVER['REQUEST_METHOD']=='POST)
if (!empty($_POST['_submit_check']))

My two cents.

Which tells nothing about HOW the page was posted. It could have been a
POST request from an entirely different page - which doesn't have the
appropriate values set. It also can't tell which of several different
SUBMIT buttons might have been pressed.

It is always better to check for specific button than to just check the
REQUEST_METHOD. I find very little use for the latter. Knowing the
request method by itself is virtually useless.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================

Yeah, I might have jumped to conclusions on that E_NOTICE thing but I
rather first detect the request method and only after that validate
the submit button used, in terms of speed. == is faster than isset().

Anyway, the !empty() could be ==1 or any other thing, it was just an
example of how it could be done.
.