Solved

Evaluating php form variable existance - 0 value

Posted on 2003-11-17
1
214 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now