bassdog65 Posted May 8, 2009 Share Posted May 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/ Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829555 Share on other sites More sharing options...
bassdog65 Posted May 8, 2009 Author Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829556 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Can you post the entire code? Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829559 Share on other sites More sharing options...
bassdog65 Posted May 8, 2009 Author Share Posted May 8, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829560 Share on other sites More sharing options...
Axeia Posted May 8, 2009 Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829561 Share on other sites More sharing options...
bassdog65 Posted May 8, 2009 Author Share Posted May 8, 2009 what would a better substitute for stristr() be? Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829562 Share on other sites More sharing options...
Axeia Posted May 8, 2009 Share Posted May 8, 2009 Depends on what you want to do. What are you trying to do with that (stristr($_GET['lid'], '1') == TRUE) ? Post an example of what you think could be in $_GET['lid'] and what you're searching for in it. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829569 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 We're not telling you to use a substitute function. I'm just telling you that comparing it to true will always be false because stristr() will never return a true value. You can say if it's not false. Also, why are you putting \ in front of / in the URL? Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829570 Share on other sites More sharing options...
bassdog65 Posted May 8, 2009 Author Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829577 Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829580 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 premiso, that function doesn't return true. Either the matched string or false. From php.net Returns the matched substring. If needle is not found, returns FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829584 Share on other sites More sharing options...
bassdog65 Posted May 8, 2009 Author Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829586 Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 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 do not think so. stristr seemed to be right on key for what you were trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829594 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 If the URL is just contact_form.php?lid=2, then if ($_GET['lid'] == 2) would suffice. You don't need a function. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829598 Share on other sites More sharing options...
bassdog65 Posted May 8, 2009 Author Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829601 Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 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 ==. Quote Link to comment https://forums.phpfreaks.com/topic/157373-solved-if-statement-not-working/#findComment-829606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.