PDA

View Full Version : If else with html



scrooby
07-14-2008, 08:49 PM
I am very new to php and I am trying to do a If Else statement, this is what I have so far:

<?php

if(isset($_SESSION['myusername']))

echo "Welcome ". $_SESSION['myusername'];

else

?>

<form action="checklogin.php" method="post" name="form1">
<input type="text" name="myusername" value="john" id="myusername"> <input type="password" name="mypassword" value="1234" id="mypassword"><input type="submit" value="Login" name="Submit">
</form>

<?php

;

?>


My problem is, is that I dont know where to put the form because i want it to say, if there is a session active, say hello session value, if not, display the login form. At the moment it displays the hello session value but also displays the form because its not in the php tags but i dont know where and how to put it ?

Scrooby

scrooby
07-14-2008, 09:05 PM
NVM, found the answer

http://codingforums.com/archive/index.php?t-36744.html

Scrooby

Nile
07-14-2008, 09:17 PM
If your new to PHP like you are. I'd suggest using the curly brackets.
I'll also give you a little trick with if() if you don't already know it.
The code below.


$test = "Dog";
if($test == "Dog"){
$testNext = "Cat";
} else {
$testNext = "Dog";
}

Is equivalent to(check below for explanation:


$test = "Dog";
$testNext = ($test=="Dog") ? "Cat" : "Dog";

Red : if()
Blue : $test == "Dog"
Navy : First bracket( ' { ' )
Gray : $textNext = "Cat"
Light Blue : Else bracket( ' } ' )
Yellow : $textNext = "Dog"

dicegame
07-14-2008, 11:13 PM
i recommend the shorter version for one line conditional statements, while the first one would be better for longer conditionals, seeing as sometimes you'd want to check if you have the correct amount of curly braces and where.