Forum Home
Press F1
 
Thread ID: 43524 2004-03-17 09:17:00 Off Topic: Any PHP/MYSQL gurus out there?? prayingmantis (5416) Press F1
Post ID Timestamp Content User
223298 2004-03-21 04:59:00 The "notall" might need to be set to "non-1" if everything is OK. (it might remember a 1-value from a previous error test.) It looks as if your code is just setting $notall to 1 then testing it. How does it get there?

I don't ever trust variables to be automatically set to zero (or anything at all) when they are declared. Assumptions like that [b]always[/b[ bite you. Burroughs large systems did a nice thing --- they set a tag to "uninitialised" when a memory word was allocated to a variable. If you used it before giving it a value, it gave an error. Nice machine. :D
Graham L (2)
223299 2004-03-24 04:36:00 **bump** prayingmantis (5416)
223300 2004-03-25 11:44:00 Hey prayingmantis,

Lost everything I posted here so I'll just give the answer :P

Setting $notall = 1; means that $notall == 1; will always result in true, so it'll always perform what it's told to do.

So here's the answer, I will comment to make it easier to understand:
[pre]
unset($_POST['submit']); //removes submit field from posted vars
foreach($_POST as $key=>$val) //loops through each posted var once
{
if(!$val) //captures empty fields as well as 0 (zero)
{
$notall = 1; //sets the variable suggesting empty field
}
}
if($notall == 1)
{
echo '
<font color="#FF0000">Please fill out all fields</font>'; //displays the error message if fields are not filled out
exit(); //exits the PHP script, no more processing script
}


Noel Nosivad
Noel Nosivad (389)
223301 2004-03-25 11:55:00 Just to add more onto my script,

I use foreach($_POST as $key=>$val) instead of just foreach($_POST as $val) for the ability to use the fieldname as well as the information with the fieldname. $key stores the fieldname & $val stores the posted information.

Say if you have echo "$key = $val
"; will display the fieldname = value on seperate lines. There's allsorts you can perform with knowing this information so I thought I'd add it as it's quite handy.

There is a better method than using (!val) but it requires more thinking, you can actually specify what characters are allowed to be use, limit the numbers of characters etc, but in this case I didn't, this is still good for capturing empty fields and unfortunately a single 0 (zero) character which is the downfall.

One thing prayingmantis is that I try to move with whatever is happening in that scripting/web language, so you could use $HTTP_POST_VARS in place of $_POST but PHP says it's going to be obsolete in the future and to avoid it use what they've replaced it with.

Another thing I noticed is the all capital HTML tags, the w3c will soon have it that these tags will have to be lowercase so you should get use to doing that too.


Cheers,


Noel Nosivad
Noel Nosivad (389)
223302 2004-03-25 12:04:00 Just realised,

Anything below an exit(); script will be missing if you view the page source, so there won't be a </body> </html> closing tag.

What you could do instead is...

echo '
[b]<font color="#FF0000">Please fill out all fields</font>[b]\n\n</body>\n\n</html>';
exit();

That way it'll display it before the script is stopped in it's tracks.


Noel Nosivad
Noel Nosivad (389)
223303 2004-03-25 12:08:00 Sorry my bad,

Use double quotes instead of single quotes when using escaped characters

echo "
[b]<font color=\"#FF0000\">Please fill out all fields</font>[b]\n\n</body>\n\n</html>";

Noel Nosivad
Noel Nosivad (389)
223304 2004-03-26 09:50:00 > Another thing I noticed is the all capital HTML tags,
> the w3c will soon have it that these tags will have
> to be lowercase so you should get use to doing that
> too.

Thanks, I have change the bit that was in caps.
prayingmantis (5416)
223305 2004-03-26 09:57:00 >
> unset($_POST['submit']); //removes submit field from
> posted vars
> foreach($_POST as $key= > $val) //loops through each
> posted var once
> {
> if(!$val) //captures empty fields as well as 0
> (zero)
> {
> $notall = 1; //sets the variable suggesting empty
> y field
> }
> }
> if($notall == 1)
> {
> echo '
<font color="#FF0000" > Please fill
> out all fields</font > '; //displays the
> error message if fields are not filled out
> exit(); //exits the PHP script, no more processing
> script
> }

Ok, I tried that & add the double quotes instead of single quotes where you said to add them

But I got this error message:

Parse error: parse error, unexpected '=', expecting ')' in c:\program files\apache group\apache\htdocs\form . php on line 54

Line 54 is:

foreach($_POST as $key=$val) //loops through each

Here is every thing that I coped & checked:

unset($_POST['submit']); //removes submit field from posted vars
foreach($_POST as $key=$val) //loops through each
posted var once
{
if(!$val) //captures empty fields as well as 0
(zero)
{
$notall = 1; //sets the variable suggesting empty y field
}
}
if($notall == 1)
{
echo "
<strong<font color="#FF0000" Please fill out all fields</font[/b]"; //displays the error message if fields are not filled out
//exit(); //exits the PHP script, no more processing script
}
? >

Any ideas, what is wrong?? Or what I did wrong? ?:|
prayingmantis (5416)
223306 2004-03-26 10:00:00 > //exit(); //exits the PHP script, no more processing
> script

PS: Yes, I did comment this bit out just for now, but I will re add it once I get this bug out, & yes I have put it back in to see if it helps or not, but it did not.
prayingmantis (5416)
223307 2004-03-26 10:01:00 Hey prayingmantis,

A lot of errors are made with just simple mistakes.

foreach($_POST as $key=$val)

should be

forecah($_POST as $key=>$val)

you missed the >


Noel Nosivad
Noel Nosivad (389)
1 2 3 4 5