Jump to content

Making "0" a value?


Perad

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
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.