By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

ISSET() question

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.