If.....Then

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

If.....Then

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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 :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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>"; 

}

?>
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

nice eyes, thanks for catching that
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make sure to verify the value coming in is an acceptable one.. ;)
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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?
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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";
}
?>
Post Reply