Jump to content

Recommended Posts

I have an IF statement to assign a href to a variable, so i can call the variable later, and for some reason everytime i go to the page, it combines both options for the variable, like teh if statement ran both the if and the else.

 

example:

 

<?php
if (isset($_GET['lid'])) {

   if (stristr($_GET['lid'], '1') == TRUE) {
      $returnlink = '<a href=\"http:\\/www.farewaymovies.com/test\">FarewayMovies.com</a>';
      echo '<img src="images/FarewayMoviesLogo.png">';

   } else {
      $returnlink = '<a href=\"http:\/\/www.premiermoviegroup.com\">PremierMovieGroup.com</a>';
      echo '<img src="images/PMGlogo_med.png">';

   }

} else {

echo 'No location set.';
}
?>

 

Basically the end result is that when i call teh $returnlink variable later i get both links combined, instead of one or the other.

 

What am I missing?

Link to comment
https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/
Share on other sites

yes, later on I simply call this:

 

<p>Return to <?php echo $returnlink;?></p>

but instead of one href or the other, it returns:

 

http://www.premiermoviegroup.com////www.farewaymovies.com/test

 

where it should return one or the other based off of the location ID.

<?php

// load the variables form address bar
$subject = $_REQUEST["subject"];
$main = $_REQUEST["message"];
$from = $_REQUEST["from"];
$verif_box = $_REQUEST["verif_box"];
$name = $_REQUEST["name1"];
$number = $_REQUEST["number"];

// remove the backslashes that normally appears when entering " or '
$main = stripslashes($main); 
$subject = stripslashes($subject); 
$from = stripslashes($from); 
$name = stripslashes($name); 
$number = stripslashes($number); 

$main2 = 'Question/Comment: '.$main."\n\n";
$subject2 = 'Subject Line: '.$subject."\n\n";
$from2 = 'Email address entered: '.$from."\n";
$name2 = 'Contact Name: '.$name."\n";
$number2 = 'Phone Number: '.$number."\n";

$message = $subject2.$main2.$name2.$number2.$from2;

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("bassdog65@gmail.com", 'Message from contact form at FarewayMovies.com: ', $message);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else {
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Success!</title>
<style>
body {
    background: white url(images/bg5.jpg) repeat-x;
    font-family: "Trebuchet MS", Rockwell;
    font-size:14px;
}
#container {
margin: 0 auto;
background-color:#5e5e5e;
width:500px;
height:400px;
padding:30px;
border: 5px black double;
color:white;
z-index:auto;
}
#pagewrapper {
margin-top:78px;
}
#logochoice {
float: left; 
position:absolute;
top: 20px; 
left: 30px;
z-index:0;
}
/* Text Box with Gradient: */
.tb-gradient {
    border:0;
    background: transparent url(images/gradient.gif) no-repeat top left;
    height: 22px;
    width: 230px;
    padding-top:2px;
}
.tb-gradient2 {
    border:0;
    background: transparent url(images/gradient2.gif) no-repeat top left;
    height: 100px;
    width: 450px;
    padding-top:0px;
}
a:link {
    font-family: "Trebuchet MS", Rockwell;
    font-size: 14px;
    color: white; 
}
a:visited {
    font-family: "Trebuchet MS", Rockwell;
    font-size: 14px;
    color: white; 
}
a:hover {
    color: gray; 
}
</style>

</head>

<body>
<div id="logochoice">

	<?php
	if (isset($_GET['lid'])) {

		if (stristr($_GET['lid'], '1') == TRUE) {
		$returnlink = "<a href=\"http:\/\/www.farewaymovies.com/test\">FarewayMovies.com</a>";
		echo '<img src="images/FarewayMoviesLogo.png">';

		} else {
		$returnlink = "<a href=\"http:\/\/www.premiermoviegroup.com\">PremierMovieGroup.com</a>";
		echo '<img src="images/PMGlogo_med.png">';

		}

	} else {

	echo 'No location set.';
	}
	?>
</div>

<div id="pagewrapper">
<div id="container"> 

Email sent. Thank you. You will be contacted in 24 hours.<br />
<br />
<p>Return to <?php echo $returnlink;?></p>

</div>
</div>

</body>
</html>

 

Its a handle form for a contact us page, to put it in context for you.

Check out the link Ken2k7 posted and read what it says.

 

The returnvalue (the thing listed infront of the function) is of the type 'string', you're comparing it to the boolean true. So that if is NEVER EVER gonna do anything, it will always be the else.

Not sure as to why it would actually echo it it out like that, I'm guessing that's caused somewhere else in your code.

 

And don't forget basic HTML such as an alt tag for the image and setting the size if it isn't done trough a stylesheet. And making it self closing if it's served as xhtml.

 

i put the \ in front of the URL because i was trying to escape the second set of quotes, thinking that mgiht have been the problem.  it clearly was not, and im sure i can remove those by now.  Also the reason for putting \/\/ was because two forward slashes in PHP makes it a comment, so i didnt knwo how else to enter a href.

 

since this is a handle form for another page, i have a location id built into the page based off where the user was sent from.  we have one contact page beign shared between both of those domains.  if they are coming from farewaymovies.com they go to contact_form.php?lid=1 if they come from premiermoviegroup.com they go to contact_form.php?lid=2 and after they submit the form, i want the handle form to display the link of wherever they came from, so i am trying to pull their location id out of the page and display a link based on that.

I think your main issue is your slashes. Your IF statement is fine, and to ken's remark about stristr not returning correct values is false, however how your if statement is setup it can give a false positive.

 

      <?php
         $returnlink = ""; // initialize the variable to make sure nothing else is added.
      if (isset($_GET['lid'])) {

         if (stristr($_GET['lid'], '1') !== false) { // use the === operator to test a true/false condition
             $returnlink = "<a href=\"http://www.premiermoviegroup.com\">PremierMovieGroup.com</a>"; // the // are not escape characters no need to escape them
            echo '<img src="images/PMGlogo_med.png">';
         } else {
            $returnlink = "<a href=\"http://www.farewaymovies.com/test\">FarewayMovies.com</a>"; // the // are not escape characters no need to escape them
            echo '<img src="images/FarewayMoviesLogo.png">';         
         }
         
      } else {
                        
      echo 'No location set.';
      }
      ?>

 

Honestly Ken, if you would have taken the extra 2-3 minutes to read the post and the code he posted you might of been some help instead of confusing everyone on this topic. And actually posted something helpful instead of your one line answer of

 

man stristr. That never returns true so your second if statement is always going to be false. $returnlink should not be combined. Can you post more code or the part where $returnlink is not the expected value?

premiso!  that worked!  thanks! 

 

I was just confused about what i needed to escape and what i didn't need to.  I would be interested to learn if there is a better function i could call in the future for what I am trying to accomplish.  I take everything in that i learn on this site and absorb it.

Hmmmmm, Ken, good point.  I guess I am used to calling more than one thing at a time, so i have a stristr so that i can pull any value out of my string.  However in this case with one or the other, i see where you are going.  I learn so much on here.  Thanks to everyone.

If the URL is just contact_form.php?lid=2, then

if ($_GET['lid'] == 2)

would suffice. You don't need a function.

 

If this is the case, you are better off using that method above.

 

stristr is really for locating an item inside of a string. So let's say I wanted to see if the following contained "dog" in it:

 

$string = "The quick black dog ran over the neighbors yard.";
if (stristr($string, "dog") !== false) {
    echo "We have a dog!";
}

 

Just so you understand there is a a correct way to utilize stristr. I would say how you were using was the incorrect way and you should use the comparison operator ==.

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.