Jump to content

Archived

This topic is now archived and is closed to further replies.

Perad

Making "0" a value?

Recommended Posts

I have this code to check to see if information is entered into the form...

[code]
if (empty($_POST['oppscore1'])) {
$os1 = FALSE;
$message .= '<p>You forgot to enter the Opponents Score ht!</p>';
} else {
$os1 = $_POST['oppscore1'];
}[/code]

Unfortunately when i enter "0" into the form field for Score, it gets rejected because it thinks 0(Zero) isn't a number/value.

Could someone help me correct this please.

Perad

Share this post


Link to post
Share on other sites
[quote author=Perad link=topic=111650.msg452590#msg452590 date=1161005954]

[code]
if (empty($_POST['oppscore1'])) {
$os1 = FALSE;
$message .= '<p>You forgot to enter the Opponents Score ht!</p>';
} else {
$os1 = $_POST['oppscore1'];
}[/code]

[/quote]

Much better to use:

[code]
<?php

if (isset($_POST['oppscore1'])) {
$os1 = FALSE;
$message .= '<p>You forgot to enter the Opponents Score ht!</p>';
} else {
$os1 = $_POST['oppscore1'];
}

?>
[/code]

Share this post


Link to post
Share on other sites
um, isset($_POST['field_name']) will return true if that form item exists..  not if it's been filled out.

You're better checking strlen(trim($_POST['field_name'])) to see how long the entered info is.. if someone types a 0 it'll be 1 char long.

Share this post


Link to post
Share on other sites
is_numeric() will work, also this is an approach to detect "real" empty

[code]

<?php

if(empty($_POST['oppscore1']) && !preg_match("/^[0]+$/", $_POST['oppscore1']))
{
  echo "is genuine empty and a 0 is not here";
}
else
{
  echo "is not empty and counting 0 as a value";
}

?>

[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.