Jump to content


Photo

Problem on simple if statement


  • Please log in to reply
5 replies to this topic

#1 foolygoofy26

foolygoofy26
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 March 2006 - 11:55 PM

I have a form on page A that gets a code and send it to page B, if the key is correct it will print out the name of X file as a link, if not correct, it will print out Access Denied. For some reason I get an error on page B on the line 2 of this code. Could someone tell me what is wrong in this if statement.


<?php
if ($_POST["code"] == "list")
echo "<a href=Downloads/list.txt target=top>See List</a>";

else
echo "Access Denied";
?>

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 29 March 2006 - 12:20 AM

Try...

<?php 
if ($_POST["code"] == "list") {
  <? <a href="Downloads/list.txt" target="top">See List</a>
  <?PHP
;
}else{
echo "Access Denied";
}
?>
Note the use of braces

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 Cojawfee

Cojawfee
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 29 March 2006 - 01:52 AM

You have to use braces in PHP, they are not optional.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 02:57 AM

[!--quoteo(post=359510:date=Mar 28 2006, 08:52 PM:name=Cojawfee)--][div class=\'quotetop\']QUOTE(Cojawfee @ Mar 28 2006, 08:52 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You have to use braces in PHP, they are not optional.
[/quote]
No, braces are optional if there is only one statement in the statement block. Read the [a href=\"http://us3.php.net/manual/en/language.control-structures.php\" target=\"_blank\"]manual[/a] and be enlightened.

BTW, the first response given is all wrong. There are invalid PHP start tags within the PHP statements.

To the OP, what is the error you get & do you have any PHP code above these statements? Many times PHP will print an message indicating an error on a particular line, but the error is really on a line above the one indicated. This usually happens when you haven't closed a quoted string.

Please post about 5 to 10 lines of code that precede these.

Ken

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 March 2006 - 04:06 PM

rather than checking that $_POST['code'] value also check that is actually set. This is why you are getting an error on your page B. So chnage you code to the following:

<?php

//first check that $_POST['code'] is actually set and then chekc that its value is set to "list"
if (isset($_POST['code']) && $_POST['code'] == "list")
{
    echo "<a href=Downloads/list.txt target=top>See List</a>";
}
else
{
    echo "Access Denied";
}
?>
I would also recommend you to use indentaion with you code blocks this helps you to idently code bocks to code blocks for functions, if, if/ele/else if statements etc. ALso I would recommend to use braces too.

#6 foolygoofy26

foolygoofy26
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 March 2006 - 08:41 PM

Thanks everyone for the advices. litebearer your example works. the only problem I ran into was that on page A when you type list you would get access denied and not the printed link as expected. Still, thanks I got the point and the lesson. Next stop for me is finding out a way to solve that little issue





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users