Jump to content

[SOLVED] isset checking two vars


Winston_Smith

Recommended Posts

I seem to have a problem is isset, I've looked around a number of sites and from what I can tell it's correct.

<?php
//set Variables
$town = $_POST['town'];
$lname = $_POST['lname'];
$full = $_POST['full'];

//Check if box is checked, if true unset other variables
if ( $full == true ) {
	unset ($town);
	unset ($lname);
	} 
//Check if $town and $lname are set, if true set new variables and unset old
if (isset($town, $lname)) {
	$town2 = "$town";
	$lname2 = "$lname";
	unset ($town);
	unset ($lname);
	}
?>

If I comment out the if (isset($town, $lname)) {... section the rest of the code on the page works fine.

 

This is the section i'm setting the variables for.

<?php
elseif (isset($town2, $lname2)){
  print <<<_HTML_
<center>
<img src="ecfiber.jpg">

<table width="800" border="1">
  <tr>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">E-911 Address</th>
    <th scope="col">Town</th>
    <th scope="col">E-Mail</th>
  </tr>
_HTML_;

$result = mysql_query("SELECT * FROM `ecfiber` WHERE `town` ='".$town2."' AND `lname` ='".$lname2."'");
while($row = mysql_fetch_array($result)){
  print "<tr><td>";
  print $row['fname'];
  print "</td><td>";
  print $row['lname'];
  print "</td><td>";
  print $row['address'];
  print "</td><td>";
  print $row['town'];
  print "</td><td>";
  print $row['mail'];
  print "</td></tr>";
  }
  
  print "</table>";
  print "<a href = \"index.html\"> Return To Main Index</a><br>";
  print  "<a href = \"listdb.html\">View Database</a></center>";
  
  }
?>

 

Link to comment
Share on other sites

Ok, that works thank you. But It gives me another problem, it seems it always unsets the $town and $lname vars. I'm using a form using post to get my value's if nothing is entered into the fields they should be unset currect?

 

Basically as mgall stated.. if nothing is entered, that $_POST variable doesn't exist.

Hence, when you test if $_POST['whatever'] is set, it will fail (which is the whole point of testing using isset().

 

Also, looking at the first snippet of code, you can unset multiple variables in one fell swoop.

unset ($town);
unset ($lname);

 

could be written as:

unset ($town, $lname);

 

Cheers,

 

NRG

Link to comment
Share on other sites

The problem is that if I enter just the $town field in the form it prints nothing. But if a fill in both the $town and $lname fields it works great. From what I can tell the isset ($var && $var) should returns as false if I only fill in one field. Yet it seems that it is returning as true.

 

I see the new reply also, but if either one was false the code after the if shouldn't run correct? If one of the vars was not set it should skip that and insted continue till it hits the if statement for whichever variable was set. But it seems to set the $town2 and $lname2 vars and then it jumps up to the elseif for that and prints the table with no data.

 

Here's a little more of the code

<?php
//set Variables
$town = $_POST['town'];
$lname = $_POST['lname'];
$full = $_POST['full'];

//Check if box is checked, if true unset other variables
if ( $full == true ) {
	unset ($town);
	unset ($lname);
	} 
//Check if $town and $lname are set, if true set new variables and unset old
if (isset($town) && isset($lname)){
	$town2 = "$town";
	$lname2 = "$lname";
	unset ($town);
	unset ($lname);
	}
//
if ( $town == true ) {...
//
elseif( $lname == true ){...
//
elseif( $full == true ){...
//
elseif (isset($town2) && isset($lname2)){...
//
else {...
?>

That's the basic layout of the page.

 

This is my form page

<html>
<head>
<title>ECFiber Pre-Registration</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#CCCCCC">
<center><img src="ecfiber.jpg"></center>
<form action="listdb.php" method="post">

<table width="800" border="0" align="center">
<tr><th scope="col">Search Database by Town</th></tr>
  <tr>
    <td><center><select name="town" style="background-color:Yellow;width:160px;">
	<option value="">Select Town</option>

	<option value="Barnard">Barnard</option>
	<option value="Bethel">Bethel</option>
	<option value="Brookfield">Brookfield</option>
	<option value="Chelsea">Chelsea</option>
	<option value="Granville">Granville</option>
	<option value="Hancock">Hancock</option>

	<option value="Hartford">Hartford</option>
	<option value="Montpelier">Montpelier</option>
	<option value="Norwich">Norwich</option>
	<option value="Pomfret">Pomfret</option>
	<option value="Randolph">Randolph</option>
	<option value="Reading">Reading</option>

	<option value="Rochester">Rochester</option>
	<option value="Royalton">Royalton</option>
	<option value="Sharon">Sharon</option>
	<option value="Stockbridge">Stockbridge</option>
	<option value="Strafford">Strafford</option>
	<option value="Thetford">Thetford</option>

	<option value="Tunbridge">Tunbridge</option>
	<option value="Vershire">Vershire</option>
	<option value="West Windsor">West Windsor</option>
	<option value="Williamstown">Williamstown</option>
	<option value="Woodstock">Woodstock</option>

</select></center></td>
  </tr>
   <tr><td><center>Search Database by Last Name</center></td></tr>
  <tr>
    <td><center><input name="lname" type="text"></center></td>
  </tr>
  <tr><td><center>Check to display all names in Database</center></td></tr>
  <tr><td><center><input name="full" type="checkbox"></center></td></tr>
  <tr><td><center><input type="submit" value="Submit Data"></center></td></tr>
  </table>

</form>
<center><a href = "index.html"> Return To Main Index</a></center>

</body>
</html>

Link to comment
Share on other sites

The problem is that if I enter just the $town field in the form it prints nothing. But if a fill in both the $town and $lname fields it works great. From what I can tell the isset ($var && $var) should returns as false if I only fill in one field. Yet it seems that it is returning as true.

 

Well, isset ($var && $var) is an incorrect format.. it should be:

if(isset($var1) && isset($var2)){
   // do this, as both $var1 AND $var2 is set (in otherwords, they both have something entered into them)...
} else {
   // do that, as either $var1 or $var2 (or both) don't have anything entered...
}

 

I see the new reply also, but if either one was false the code after the if shouldn't run correct? If one of the vars was not set it should skip that and insted continue till it hits the if statement for whichever variable was set. But it seems to set the $town2 and $lname2 vars and then it jumps up to the elseif for that and prints the table with no

 

Well, again, if your isset isn't setup properly, then you'll have problems.

Link to comment
Share on other sites

You can pass isset as many parameters as you wish. It will return false if just one of those parameters is not set.

 

if (isset($town,$lname)){

 

That's the way I had it coded out before, I changed it over to what you see from some advice I got in another post.

 

I want it to do exactly what you are saying it does. If both variables are not valid to not unset the $town and $lname variables.

 

<?php
//set Variables
$town = $_POST['town'];
$lname = $_POST['lname'];
$full = $_POST['full'];

//Check if box is checked, if true unset other variables
if ( $full == true ) {
	unset ($town);
	unset ($lname);
	} 
//Check if $town and $lname are set, if true set new variables and unset old
if (isset($town,$lname)){
	$town2 = "$town";
	$lname2 = "$lname";
	unset ($town, $lname);
	}

 

That way I have the form setup is if you enter just a town it searches the database and displays all rows that have that town in them. If you enter just a last name it displays all rows with that last name. But if you enter both a town and a last name it displays only rows that have both. Also if you check the box it unsets both other vars if they are set and displays the whole database.

 

When I enter just a town I get a blank table that prints from my last elseif statement that is supposed to display the town and last name. If I put in a town and a last name it works correctly, the check box also works correctly.

 

The only thing I can think of is for some odd reason the code is running everytime. If I comment out

if (isset($town,$lname)){
	$town2 = "$town";
	$lname2 = "$lname";
	unset ($town, $lname);
	}

I can enter just a town and get all rows, and also all last names if I enter only a last name. So It must be that that code is running everytime, when it should be false when I don't enter both a town and last name.

Link to comment
Share on other sites

I think I have it figured out, now I need to figure out how to do this.

The php manual says that even if a var is empty such as $name = ""; it will still come back as true. So even though it's set to nothing it still is true.

 

So how would I get around this? Can I use empty? But you can only check one var with it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.