473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

ISSET() question

The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Please let me know how to use isset() correctly.

Thanks ahead.

<?php
if(!isset($_POST['varA'])||!isset($_POST['varB'])){
?>
<form action="isset.php" method="POST">
<table><tr>
<td>Value 1:</td><td><input type="text" name="varA" value=""></td></
tr>
<tr><td>Value 2:</td><td><input type="text" name="varB" value=""></
td></tr>
<tr><td colspan="2"><input type="submit" value="submit"></td></tr>
</table>
</form>
<?php
}

else {
$x=$_POST['varA'];
$y=$_POST['varB'];

if($x>$y){
echo "Value 1:".$x." is larger";
}else

echo "Value 2:".$y." is larger";
}

?>
Feb 28 '08 #1
10 2348
major a écrit :
Apparently isset() is not working, because it thinks that a blank text
field is set to something.
Exactly, "blank" is still a value. isset just verify that the variable
*is set*, whatever its content.

Regards,
--
Guillaume
Feb 28 '08 #2
On 28 fév, 08:51, major <extreme...@gmail.comwrote:
The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Please let me know how to use isset() correctly.

Thanks ahead.

<?php
if(!isset($_POST['varA'])||!isset($_POST['varB'])){
?>
<form action="isset.php" method="POST">
<table><tr>
<td>Value 1:</td><td><input type="text" name="varA" value=""></td></
tr>
<tr><td>Value 2:</td><td><input type="text" name="varB" value=""></
td></tr>
<tr><td colspan="2"><input type="submit" value="submit"></td></tr>
</table>
</form>
<?php

}

else {
$x=$_POST['varA'];
$y=$_POST['varB'];

if($x>$y){
echo "Value 1:".$x." is larger";

}else

echo "Value 2:".$y." is larger";

}

?>
use empty() :

if ( ( isset($_POST['varA'] && !empty($_POST['varA']) ) || ...

ichevc
Feb 28 '08 #3

"major" <ex********@gmail.comwrote in message
news:b2**********************************@p43g2000 hsc.googlegroups.com...
The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Please let me know how to use isset() correctly.
This one, I actually can help with :)

This page will save you a lot of grief if you take a few minutes to read and
understand it:
http://us3.php.net/manual/en/types.comparisons.php

Feb 28 '08 #4
major wrote:
The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.
Yes, if the value of a field is "", then it is still set. You would want
to test for both:

if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...

Or, if you want to count " " as blank:
if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...

There may be a more efficient way to do this - anyone know of one?
Feb 29 '08 #5
Tony wrote:
major wrote:
>The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Yes, if the value of a field is "", then it is still set. You would want
to test for both:

if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...

Or, if you want to count " " as blank:
if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...

There may be a more efficient way to do this - anyone know of one?
Yes, validate it with a regex also:

$name = (isset($_POST['name']) &&
eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;

if (!$name)
{
....
}

would check to see if the post variable has been set and if it matches a
string of alpha a-z (upper & lower case), and is at least 2 characters
but not more than 25 characters in length. If not it's set to false and
you take appropriate action.

--
Norman
Registered Linux user #461062
Feb 29 '08 #6
Norman Peelman wrote:
Tony wrote:
>major wrote:
>>The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Yes, if the value of a field is "", then it is still set. You would
want to test for both:

if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...

Or, if you want to count " " as blank:
if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...

There may be a more efficient way to do this - anyone know of one?

Yes, validate it with a regex also:

$name = (isset($_POST['name']) &&
eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;

if (!$name)
{
....
}

would check to see if the post variable has been set and if it matches a
string of alpha a-z (upper & lower case), and is at least 2 characters
but not more than 25 characters in length. If not it's set to false and
you take appropriate action.
But what if I don't want to validate according to those rules?
Mar 1 '08 #7
Tony wrote:
Norman Peelman wrote:
>Tony wrote:
>>major wrote:
The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Yes, if the value of a field is "", then it is still set. You would
want to test for both:

if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...

Or, if you want to count " " as blank:
if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...

There may be a more efficient way to do this - anyone know of one?

Yes, validate it with a regex also:

$name = (isset($_POST['name']) &&
eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;

if (!$name)
{
....
}

would check to see if the post variable has been set and if it matches
a string of alpha a-z (upper & lower case), and is at least 2
characters but not more than 25 characters in length. If not it's set
to false and you take appropriate action.

But what if I don't want to validate according to those rules?
Then make up your own rules. And accept that if you don't do it right,
someone can delete your entire database - or at least an entire table.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Mar 1 '08 #8
Tony wrote:
Norman Peelman wrote:
>Tony wrote:
>>major wrote:
The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Yes, if the value of a field is "", then it is still set. You would
want to test for both:

if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...

Or, if you want to count " " as blank:
if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...

There may be a more efficient way to do this - anyone know of one?

Yes, validate it with a regex also:

$name = (isset($_POST['name']) &&
eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;

if (!$name)
{
....
}

would check to see if the post variable has been set and if it matches
a string of alpha a-z (upper & lower case), and is at least 2
characters but not more than 25 characters in length. If not it's set
to false and you take appropriate action.

But what if I don't want to validate according to those rules?
What rules do you want to validate by?

--
Norman
Registered Linux user #461062
Mar 1 '08 #9

"Tony" <no****@example.comwrote in message
news:13*************@corp.supernews.com...
major wrote:
>The following code processes a blank field as though it has some value
and proceeds as though it was set to some value.

Apparently isset() is not working, because it thinks that a blank text
field is set to something.

Yes, if the value of a field is "", then it is still set. You would want
to test for both:

if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...

Or, if you want to count " " as blank:
if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...

There may be a more efficient way to do this - anyone know of one?
if($_POST['name']) {

This will, however, evaluate 0 and "0" to false. It will also throw a
warning. !empty() is exactly the same thing, but does not throw a warning.

Mar 3 '08 #10
In article <13*************@corp.supernews.com>, no****@example.com
says...

Otherwise - just how "efficient" do you need a $_POST test to be?
Stop worrying - do it and move on. That what script languages like PHP
are for.

If regex is troubling you or you want to do more validation I'd take a
look at www.streamforensics.com to automate your forms validations.
Their system makes life a whole lot easier in so many ways.

You probably have more pressing problems than the one you are worrying
about?

Jeez, people - I was just asking. I don't know everything about PHP, and
was simply admitting there might be a better way than mine.

Is the moon full or something? People seem to be awfully touchy lately.
Tony - I wasn't having a go I was just trying to explain why I didn't
think it worth worrying about instead of just saying so.

Sorry if it read bad to you.

Mar 3 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
4
by: Brian Olivier | last post by:
Hello, What I try to do is create a string variabel with some intelligence. I want this variabel to read the information sent by another page and interpret the result. The idea is that is a...
25
by: wd | last post by:
I want my server to send a 404 header if a URL with a query string is requested. So if a browser or spider requests something like www. my_site .com?p=chair they would get a 404... But if they...
9
by: wouter | last post by:
hey hi..... I wanna make a switch wich does this: if pagid is set do A, if catid is set do B, if projectid is set do C, else do D. So i was thinking something like this:
3
by: oggy | last post by:
Hello I try to answer this question for the Zend Certification PHP 5 : "What is the difference between isset() and other is_*() functions (is_alpha(), is_number(), etc.)?" Do you have a...
2
by: sathyashrayan | last post by:
Dear group, My question may be novice. I have seen codes where the isset() is used to test weather a user's session ($_SESSION) is set before entering a page. A kind of direct access to a page is...
2
by: yawnmoth | last post by:
<?php $var = 'test'; echo isset($var) ? 'true' : 'false'; echo '<br>'; echo isset($var) ? 'true' : 'false'; ?> Why does that result in this output?:
8
by: Giovanni R. | last post by:
Take a look at this code (you can execute it): error_reporting(E_ALL); function byVal( $v) {} function byRef(&$v) {} print '<pre>'; byVal ($first); // gives a notice
9
by: arundelo | last post by:
Is there a way to tell whether accessing a variable will result in an "Undefined variable" E_NOTICE? isset() almost does this, but it also returns false if a variable is set to null: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.