Page 1 of 1
Is it isset or is it elseif or is it ME?
Posted: Fri Jan 01, 2010 2:07 pm
by BillAnton
When I try this code below and enter text in the first field it works as expected, but not in the other two fields. What am I missing?
Code: Select all
<?php
$OK = TRUE;
if (isset($_GET['email'])) {
$email = $_GET['email'];
sayHi($email, $OK);
}
elseif (isset($_GET['fname'])) {
$fname = $_GET['fname'];
echo $fname;
sayHi($fname, $OK);
echo($fname);
}
elseif(isset($_GET['author'])) {
$author = $_GET['author'];
// $OK = TRUE;
sayHi($author, $OK);
echo "$author";
}
else {
$zBlank = "nothing entered";
sayHi($zBlank, $OK);
}
?>
Regards,
Bill Antonacchio
Re: Is it isset or is it elseif or is it ME?
Posted: Fri Jan 01, 2010 3:17 pm
by flying_circus
Hi Bill, your code looks like it is doing what you told it to do, whether that is the desired result or not. If $_GET['email'] is set, then the if/then will always return true, regardless of the values of fname or author.
I presume you are trying to check if all three are set?
Try this:
Code: Select all
<?php
# Vars
$OK = TRUE;
$email = "";
$fname = "";
$author = "";
# Check $_GET Vars
if (isset($_GET['email'])) {
$email = $_GET['email'];
sayHi($email, $OK);
}
if (isset($_GET['fname'])) {
$fname = $_GET['fname'];
echo $fname;
sayHi($fname, $OK);
echo($fname);
}
if(isset($_GET['author'])) {
$author = $_GET['author'];
// $OK = TRUE;
sayHi($author, $OK);
echo "$author";
}
if(empty($email) && empty($fname) && empty($author)) {
$zBlank = "nothing entered";
sayHi($zBlank, $OK);
}
?>
Re: Is it isset or is it elseif or is it ME?
Posted: Fri Jan 01, 2010 5:59 pm
by daedalus__
Hi Bill,
why did you post this in general discussion?

Re: Is it isset or is it elseif or is it ME?
Posted: Sat Jan 02, 2010 5:29 am
by BillAnton
In the interest of clarity and because I really need the help, here is the entire block of code!
The second and third fields do NOT respond when user enters something into either of these two field, only the first field will display from the sayHi function.
Code: Select all
<?php
function sayHi($name, $status){
if ($status) {
echo "Entered: $name!";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css" media="screen">
p {
background-color: SaddleBrown;
color: Yellow;
width: 12em;
padding: 1px 3px 1px 3px;
}
</style>
</head>
<body>
<p>Enter your name below:</p>
<form action="" method="get">
email: <input type="text" name="email" /><br />
Name: <input type="text" name="fname" /><br />
author: <input type="text" name="author" /><br />
<p> <input type="submit" value="GO" /> </p>
</form>
<?php
$OK = TRUE;
$email = "";
$fname = "";
$author = "";
if (isset($_GET['email'])) {
$email = $_GET['email'];
sayHi($email, $OK);
}
elseif (isset($_GET['fname'])) {
$fname = $_GET['fname'];
echo $fname;
sayHi($fname, $OK);
echo($fname);
}
elseif(isset($_GET['author'])) {
$author = $_GET['author'];
// $OK = TRUE;
sayHi($author, $OK);
echo "$author";
}
else {
$zBlank = "nothing entered";
sayHi($zBlank, $OK);
}
?>
</body>
</html>
Regards,
Bill Antonacchio
Re: Is it isset or is it elseif or is it ME?
Posted: Sat Jan 02, 2010 2:16 pm
by flying_circus
I see you're still trying the whole "elseif" thing.
Another tip, when you build a page, make sure you run it through a validator. Google for "W3C Validator".
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bill's Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
p {
background-color: SaddleBrown;
color: Yellow;
width: 12em;
padding: 1px 3px 1px 3px;
}
</style>
</head>
<body>
<p>Enter your name below:</p>
<form action="" method="get">
<fieldset>
email: <input type="text" name="email" /><br />
Name: <input type="text" name="fname" /><br />
author: <input type="text" name="author" /><br />
</fieldset>
<p><input type="submit" value="GO" /> </p>
</form>
<div>
<?php
# Vars
$OK = TRUE;
$email = "";
$fname = "";
$author = "";
# Methods
function sayHi($name, $status){
if ($status && !empty($name)) {
echo "Entered: $name!";
}
}
# Check $_GET Vars
if (isset($_GET['email'])) {
$email = $_GET['email'];
sayHi($email, $OK);
}
if (isset($_GET['fname'])) {
$fname = $_GET['fname'];
sayHi($fname, $OK);
}
if(isset($_GET['author'])) {
$author = $_GET['author'];
sayHi($author, $OK);
}
if(empty($email) && empty($fname) && empty($author)) {
$zBlank = "nothing entered";
sayHi($zBlank, $OK);
}
?>
</div>
</body>
</html>
Re: Is it isset or is it elseif or is it ME?
Posted: Sun Jan 03, 2010 5:25 am
by BillAnton
Hi,
Yes, I am using "elseif". Is there something wrong with it? If you try running the code you will see that it doesn't work, that is why I am asking for help to ascertain what is wrong. If you know something about
flying_circus wrote:trying the whole "elseif" thing.
, please tell me.
This is NOT usuable code for my site. it is only "proof of concept" example, hence
flying_circus wrote:Another tip, when you build a page, make sure you run it through a validator. Google for "W3C Validator".
is not an issue to my way of thinking.
I would appreciate anyone offering some constructive comments or am I missing the point of this forum?
Regards,
Bill Antonacchio
Re: Is it isset or is it elseif or is it ME?
Posted: Sun Jan 03, 2010 12:33 pm
by flying_circus
Let's start over...
What exactly is your code supposed to do? I don't think you've told us yet.
I fixed it the way I thought you wanted it, but maybe I dont understand what you are trying to accomplish. Have you tried my code? How does it opperate differently than you expect?
Re: Is it isset or is it elseif or is it ME?
Posted: Sun Jan 03, 2010 1:45 pm
by manohoo
Bill, it looks like you are experimenting with forms.
Notice that you can replace:
Code: Select all
if (isset($_GET['email'])) {
$email = $_GET['email'];
sayHi($email, $OK);
}
if (isset($_GET['fname'])) {
$fname = $_GET['fname'];
sayHi($fname, $OK);
}
if(isset($_GET['author'])) {
$author = $_GET['author'];
sayHi($author, $OK);
}
with this:
Code: Select all
foreach ($_GET as $key=>$value)
{
$$key = $value;
sayHi($$key, $OK);
}
Re: Is it isset or is it elseif or is it ME?
Posted: Sun Jan 03, 2010 4:18 pm
by flying_circus
manohoo wrote:
with this:
Code: Select all
foreach ($_GET as $key=>$value)
{
$$key = $value;
sayHi($$key, $OK);
}
I prefer checking each val explicitly. Not that it would matter much in this particular case, but with your method, what if I edit the querystring myself?
Code: Select all
http://www.mydomain.com/mypage.php?email=email&fname=fname&author=author&myVar=xss
Re: Is it isset or is it elseif or is it ME?
Posted: Mon Jan 04, 2010 5:29 am
by BillAnton
Thanks for the responses so far.
What I am trying to accomplish with this code block is to determine which of the three fields has been entered by the user. another part will be to prevent entry in more than one field. The resulting query can be based on only one of the three, this tells me which one.
If you try my code you will find that only the first field echo or result of passing through the function is shown. The second and third are NOT showing up. This is the problem I am trying to solve.Entry is one at a time with the user then pressing the submit button. If you try it only enter one of the three. Try the second field and you will see the problem.
Appreciate your help.
Regards,
Bill Antonacchio
Re: Is it isset or is it elseif or is it ME?
Posted: Mon Jan 04, 2010 11:26 pm
by flying_circus
Does it have to be done with 3 text fields? You can never garuntee that your user will enter data in 1 and only 1 field. Moreover, when you use the if/elseif statements, the way that you have, there will be a priority of fields. Hence, when you fill in email, it does not matter if fname or author are populated.
Why not do 1 text field and radio buttons?
Code: Select all
<input type="radio" name="select" value="email" /> Email
<input type="radio" name="select" value="fname" /> Fname
<input type="radio" name="select" value="author" /> Author
<br />
<input type="text" name="query" /><br />
If you are set on using 3 distinct text fields, then you'll have to check to make sure that 1 and only 1 text field is populated, but it gets messy.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bill's Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
p {
background-color: SaddleBrown;
color: Yellow;
width: 12em;
padding: 1px 3px 1px 3px;
}
</style>
</head>
<body>
<p>Enter your name below:</p>
<form action="" method="get">
<fieldset>
email: <input type="text" name="email" /><br />
Name: <input type="text" name="fname" /><br />
author: <input type="text" name="author" /><br />
</fieldset>
<p><input type="submit" value="GO" /> </p>
</form>
<div>
<?php
# Vars
$OK = TRUE;
$email = "";
$fname = "";
$author = "";
# Methods
function sayHi($name, $status){
if ($status && !empty($name)) {
echo "Entered: $name!";
}
}
# Check $_GET Vars
if(isset($_GET['email']) && isset($_GET['fname']) && isset($_GET['author'])) {
if (!empty($_GET['email']) && empty($_GET['fname']) && empty($_GET['author'])) {
$email = "Email: " . $_GET['email'];
sayHi($email, $OK);
} elseif (empty($_GET['email']) && !empty($_GET['fname']) && empty($_GET['author'])) {
$fname = "Fname: " . $_GET['fname'];
sayHi($fname, $OK);
} elseif (empty($_GET['email']) && empty($_GET['fname']) && !empty($_GET['author'])) {
$author = "Author: " . $_GET['author'];
sayHi($author, $OK);
} else {
if(!empty($_GET['email']) || !empty($_GET['fname']) || !empty($_GET['author'])) {
$zBlank = "more than 1 field entered";
sayHi($zBlank, $OK);
} else {
$zBlank = "nothing entered";
sayHi($zBlank, $OK);
}
}
}
?>
</div>
</body>
</html>
Re: Is it isset or is it elseif or is it ME?
Posted: Tue Jan 05, 2010 5:54 am
by BillAnton
Hello and thank you flying_circus for all your hard work and for staying with it for me.
Now I need to digest what you have done so that I can better understand it.
I am most interested in why if only one entry were to be made in either the second or third text box that it would not work, but I will try to see if that is answered in your code.
Yes, I understood that the user could (and probably would) enter in more than one box and will seriously consider your solution.
Thank you very much it is much appreciated!
Regards,
Bill Antonacchio
Re: Is it isset or is it elseif or is it ME?
Posted: Tue Jan 05, 2010 8:01 am
by jason
Bill, this has been posted already previous in the thread, but I think the issue is simple. isset() will always return true for any of the if's, and because the first If() returns true, the other two won't ever run as an elseif(). The reason is that the form is always sending over the three variables you are testing. So they are 'set', but they might also be empty. isset() checks to see if the variable exists; it doesn't check if the variable is empty. So, you'd probably want:
Code: Select all
if ( isset($_GET['email']) && !empty($_GET['email']) {
...
Now, that's still not good enough, I'd imagine, for your purposes.
Better would be something like this:
Code: Select all
if ( isset($_GET['email']) || isset($_GET['fname']) || isset($_GET['author'])) {
$email = trim($_GET['email'];
$fname = trim($_GET['fname'];
$author = trim($_GET['author'];
if ( !empty($email) ) {
// Yay! Email is good! Run your funky code!
} elseif ( !empty($fname) ) {
// More code here...
} // And the other elseif here...
}
Of course, this doesn't do any actual verification on the code, and their are a host of design issues with this, but frankly, for your purposes, this probably solves the issue in the easiest way possible.
Re: Is it isset or is it elseif or is it ME?
Posted: Tue Jan 05, 2010 9:23 am
by BillAnton
Hi Jason,
Thank you for the code but more importantly for giving such a clear explanation. I just reread the PHP manual page for isset and see where I misinterpreted it's meaning.
I think I got it now! Obviously it was ME all along.
Thanks again for both.
Regards,
Bill Antonacchio