[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Evaluating php form variable existance - 0 value

Posted on 2003-11-17
1
Medium Priority
?
235 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 240 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

656 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