Click to See Complete Forum and Search --> : if else


kahlmek
09-09-2004, 06:03 AM
one more time with if else...this code does not appear to be working:


$domain = $_SESSION['domain'];


if($domain == "Yes") {
header("Location: ../dom_reg.htm");
exit();
} else if($domain == "No") {
header("Location: ../index.html");
exit();
}


it doesn't give me an error, it just sits on the page...no redirect. the domain value is pulled from a form. Any ideas?

solavar
09-09-2004, 06:26 AM
What value is contained in $_SESSION['domain'] ?

When you say...


echo $_SESSION['domain'];



Does it say "Yes", or does it say "No" ?

If it says neither, then your code can not work. For it to say either "Yes" or "No", you must have made one of these two assignments earlier:



// You might have said...
$_SESSION['domain'] = "Yes";

// ...or maybe...
$_SESSION['domain'] = "No";




*** EDIT ***

You say the value is collected from the form. So how come you are trying to extract it from $_SESSION.

If it was POSTed, try

$domain = $_POST['domain'];

kahlmek
09-09-2004, 06:41 AM
It doesn't display the echo so I have to assign a value earlier?

if {$_POST['domain'] == "Yes"} {
$_SESSION['domain'] = "Yes";
} else if($_POST['domain'] == "No"} {
$_SESSION['domain'] = "No";
}


like that?

kahlmek
09-09-2004, 06:51 AM
ok, i changed it from the $_SESSION to $_POST and it now works. Thanks for you help

solavar
09-09-2004, 07:08 AM
Great to hear you've got it working.

Just bear in mind that $_POST will carry the form variables (IF method is "post") only as far as the file that has been defined in the action attribute.

For example...


<form name="form1" method="post" action="process.php">
<input type="text" name="domain" value="mydomain.com" />
<input type="submit" name="submit" value "Send" />
</form>

/*
On process.php $_POST['domain'] will contain "mydomain.com" as its value. After that it will be lost.

So if you want this value to be stored for later use on otherpage.php, after process.php,

then you need to store it in a session variable on process.php.

First of all, the very first line should have session_start(), then you can make this assignment...
*/

$_SESSION['domain'] = $domain = $_POST['domain'];

/* Here $domain and $_SESSION['domain'] have the same value. I prefer to use local variables, eg $domain, to minimise on typing errors.
*/

kahlmek
09-09-2004, 07:15 AM
that's what i thought, I've added

<?php
session_start();
$_SESSION['PLAN'] = $_POST['PLAN'];
$_SESSION['domain'] = $_POST['domain'];
?>


at the start of the pages, and the value has made it from page 1 to 4. I'm not sure if it's right, but it works :P

solavar
09-09-2004, 10:12 AM
That's the idea.

The only thing I can say is that you should try to adopt a standard...

$_SESSION['PLAN'] is different from
$_SESSION['plan']

(I have just been forced to go through lines and lines of code to clean up my scripts because I used different cases in my $_SESSION).

So don't get caught out like I did.

Otherwise, keep up the good work.

:D

Hellspire
09-09-2004, 12:56 PM
Regarding the header() part of your code. I was unaware that you could use relative urls.... I was under the impression that header("Location: ") required a http:// for address. *shrugs*

kahlmek
09-09-2004, 02:37 PM
yeah, i kick myself for not keeping consistencey...as far as the relative url, that's what i've always used...anyone else have an opinion?

NogDog
09-09-2004, 03:04 PM
From http://us2.php.net/manual/en/function.header.php :
Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. [my italics]

kahlmek
09-09-2004, 03:16 PM
thanks for adding that. for those concerned, the final product is up :D - http://emeraldoak.com thanks to everyone who helped!