Solved

Evaluating php form variable existance - 0 value

Posted on 2003-11-17
1
229 Views
Last Modified: 2008-03-06
Hi,

I have just created a form such as a questionaire that emails a response to someone.
On this form I have several fields that are compulsory to fill in such as forename.

Currently I am checking to see if forename does not exists by using:

if (!forename) {
$ErrorNo = 1;
}

This code should set $ErrorNo to 1 when the forename text field of a form is left blank.

This works absolutly fine, but in the event someone puts the value 0 instead of a string into the forename text box on the form, the code above assigns $ErrorNo to equal 1.

In visual basic I think I would get round this by converting the variable to a string (toString) so integer 0 is recognised as the string 0. ('0')

I tried to create another variable txtforename to equal the following:
$txtforename = (string)  "" . $forename;

I hoped that I could then evalueate this variable using:
if (!txtforename) {
$ErrorNo = 1;
}

but I still get the same problem when $forename = 0

I know some of you may be thinking I shouldn't allow 0 as a name any way, but on my form I have fields that should allow this value and also I would like to solve this problem for future reference.

I would be greatful if you can help out with my problem - I've been stuck on it for quite a while now :(

Cheers,
Si.



0
Comment
Question by:BigSi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 13

Accepted Solution

by:
lozloz earned 60 total points
ID: 9763235
hi,

there are probably lots of ways to do this, you could either check whether the value == 0, or if it's an integer first

if (empty($forename)) {
  if($forename != 0) {
    $ErrorNo = 1;
  }
}

or

if (empty($forename)) {
  if(gettype($forename) != "integer") {
    $ErrorNo = 1;
  }
}

loz
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question