473,569 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($_POS T['varA'])||!isset($_POS T['varB'])){
?>
<form action="isset.p hp" 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"><in put 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 2359
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...@gma il.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($_POS T['varA'])||!isset($_POS T['varB'])){
?>
<form action="isset.p hp" 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"><in put 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********@gma il.comwrote in message
news:b2******** *************** ***********@p43 g2000hsc.google groups.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*******@attgl obal.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.supe rnews.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

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

Similar topics

7
11866
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 the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
4
1786
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 variabel is set, a line break is printed. First question is if this is possible? Second if so, what should be the
25
2480
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 request www. my_site .com/chair.htm everything would be normal.
9
9692
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
4352
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 solution ? Thanks
2
37140
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 not possible. But I tested with the "input type="button" and the clicking of the event is not happened. Can any one tell me why. The code: <html>...
2
2571
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
2446
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
4483
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: $SetNonNull = 0; $SetNull = null; var_dump(isset($SetNonNull)); // bool(true) $Junk = $SetNonNull; // No error, as predicted by isset()....
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.