Page 1 of 1

If.....Then

Posted: Sat Jul 17, 2004 12:18 am
by SidewinderX
Yeah its me again with another problem, im programing and I come to a bump in the road although I think it requires an 'if' statement

Code: Select all

<?php
//Function determines weather or not the server is to be public or private
print "Choose weather your server is public or private, private servers require passwords to join:";
print "<br><select name="pubOrPiv">
<option>Public Server
</option>
<option>Private Server
</option>
</select>";

if ($pubOrPiv == Public Server) {
	$value = $pubPrice;
} else if ($pubOrPiv == Private Server) {
	$value = $prvPrice;
	} //end if
print "<br><br>";
//END
?>
Right there is a drop down menu (with an attempted if statement) where you can chose weather the server is to be public or private, I have this script (with some other information) outputting onto another page called "getPrice.php". But when I output the information I dont what to know weather the server is public or private I want to know how much it is going to cost the actuall numerical value, at the top of the script I have 2 variables one $pubPrice and the other $prvPrice (which stand for the public price and private price obviousally) and are set to 3 and 2 dollars. Basically this is where my thoughts come in.

I think I need an 'if' statement saying that if the option of the drop down menu equals "Public Server" then send and output the value "$pubPrice" and if the option equals "Private Server" then send and output the value "$prvPrice".

I think my thinking might be on the right track, but I have the slightest clue on the syntax as you see from my attempt

Posted: Sat Jul 17, 2004 12:26 am
by feyd
Public Server and Private Server requires quoting.. $pubOrPiv may not exist (unless you have register_globals on [not good]), so you'll need $_GET['pubOrPiv'] or $_POST['pubOrPiv'] depending if your form is a "get" or "post" respectively...

Posted: Sat Jul 17, 2004 12:43 am
by SidewinderX
So this should work?

Code: Select all

<?php
//Function determines weather or not the server is to be public or private
print "Choose weather your server is public or private, private servers require passwords to join:";
print "<br>
<select name="pubOrPiv">
<option>Public Server
</option>
<option>Private Server
</option>
</select>
";
if ($_POST['pubOrPiv'] == "Public Server") {
	$value = $pubPrice;
} else if ($_POST['pubOrPiv'] == "Private Server") {
	$value = $pubPrice;
	} //end if
print "<br><br>";
//END
?>
it dont :(

Posted: Sat Jul 17, 2004 12:46 am
by John Cartwright
try

Code: Select all

<?php

if (empty($_POST["submit"]))
{

echo "Choose weather your server is public or private, private servers require passwords to join: <br>"; 

echo "
<form method="POST">
<select name="pubOrPiv"> 
<option>Public Server 
</option> 
<option>Private Server 
</option> 
</select>
<input type="button" name="submit" value="submit">
</form>
"; 

}
else
{

if ($_POST['pubOrPiv'] == "Public Server") { 
   $value = $pubPrice; 
} else if ($_POST['pubOrPiv'] == "Private Server") { 
   $value = $prvPrice; 
   } //end if 
echo"<br><br>"; 

}

?>

Posted: Sat Jul 17, 2004 1:02 am
by SidewinderX
nice eyes, thanks for catching that

Posted: Sat Jul 17, 2004 1:12 am
by SidewinderX
thanks for the help guys, im off to bed, tomorrow ill try to absorb understand your guys corrections, you havent heard the last of me :? I cant believe it took me such a long time to find a place that when I post something it has 5 replys in 5 minuts, great job, and thanks a lot.

Posted: Sat Jul 17, 2004 3:32 pm
by SidewinderX
haha, can you say "make more work then you have to?"

lol, I just realized all you have to do is set the option value to the variable I want :idea:

Code: Select all

<?php
print "Choose weather your server is public or private, private servers require passwords to join:";
print "<br>
<select name="pubOrPrv">
<option value="$pubPrice">Public Server
</option>
<option value="$prvPrice">Private Server
</option>
</select>";
?>
So now instead of outputting "Public Server" it out puts the value of "Public Server" Damn wasnt that simple

As they say "You have to crawl before you walk" Prfect example of why you need to master HTML before you move on to PHP, hehe

Posted: Sat Jul 17, 2004 8:21 pm
by feyd
make sure to verify the value coming in is an acceptable one.. ;)

Posted: Sat Jul 17, 2004 9:17 pm
by SidewinderX
yeah, moving on now to making the script more complicated. Right now the script is outputting this info onto another page

Code: Select all

<form method="post" action="getprice.php">
Now, How do I make it output the information onto another part of this script instead of going to another page?

I think,
:idea: Is it the "switch" function?

Posted: Sat Jul 17, 2004 9:40 pm
by SidewinderX
never mind i got that simple if statement

Code: Select all

<?php
if (empty($variables)) {
echo "
All your code";
}
else {
echo "
Your outputted code";
}
?>