Jump to content


Photo

Making "0" a value?


  • Please log in to reply
5 replies to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 16 October 2006 - 01:39 PM

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

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

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

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 01:53 PM

$num = intval(0);
or
$num = (int) 0;


#3 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 16 October 2006 - 02:16 PM


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


Much better to use:

<?php

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

?>

SA PHP Archives - www.phparchives.za.org

#4 neoform

neoform
  • Members
  • PipPipPip
  • Advanced Member
  • 241 posts
  • LocationMontreal

Posted 16 October 2006 - 02:22 PM

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.
Newsique.com Social News Network

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 October 2006 - 02:23 PM

Just use is_numerical.

#6 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 16 October 2006 - 05:59 PM

is_numeric() will work, also this is an approach to detect "real" empty


<?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";
}

?>






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users