Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple if-else statement

Status
Not open for further replies.

carwee

Vendor
Jul 11, 2003
8
0
0
US
Can someone tell me what is wrog with this? PLEASE!!


<?php
$plan = $_GET['plan'];
if ( $plan == 'choirprem' ) {
?>


<form action=&quot; method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;cmd&quot; value=&quot;_xclick-subscriptions&quot;>
<input type=&quot;hidden&quot; name=&quot;business&quot; value=&quot;brookscooper3@hotmail.com&quot;>
<input type=&quot;hidden&quot; name=&quot;item_name&quot; value=&quot;Choir Premium Membership&quot;>
<input type=&quot;hidden&quot; name=&quot;item_number&quot; value=&quot;cm3&quot;>
<input type=&quot;hidden&quot; name=&quot;no_note&quot; value=&quot;1&quot;>
<input type=&quot;hidden&quot; name=&quot;currency_code&quot; value=&quot;USD&quot;>
<input type=&quot;image&quot; src=&quot; border=&quot;0&quot; name=&quot;submit&quot; alt=&quot;Make payments with PayPal - it's fast, free and secure!&quot;>
<input type=&quot;hidden&quot; name=&quot;a1&quot; value=&quot;60.00&quot;>
<input type=&quot;hidden&quot; name=&quot;p1&quot; value=&quot;1&quot;>
<input type=&quot;hidden&quot; name=&quot;t1&quot; value=&quot;M&quot;>
<input type=&quot;hidden&quot; name=&quot;a3&quot; value=&quot;50.00&quot;>
<input type=&quot;hidden&quot; name=&quot;p3&quot; value=&quot;1&quot;>
<input type=&quot;hidden&quot; name=&quot;t3&quot; value=&quot;M&quot;>
<input type=&quot;hidden&quot; name=&quot;src&quot; value=&quot;1&quot;>
<input type=&quot;hidden&quot; name=&quot;sra&quot; value=&quot;1&quot;>
</form>


<?php
$plan = $_GET['plan'];
if ( $plan == 'choirgold' ) {
?>


<form action=&quot; method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;cmd&quot; value=&quot;_xclick&quot;>
<input type=&quot;hidden&quot; name=&quot;business&quot; value=&quot;brookscooper3@hotmail.com&quot;>
<input type=&quot;hidden&quot; name=&quot;item_name&quot; value=&quot;Choir Gold Membership&quot;>
<input type=&quot;hidden&quot; name=&quot;item_number&quot; value=&quot;cm4&quot;>
<input type=&quot;hidden&quot; name=&quot;amount&quot; value=&quot;349.99&quot;>
<input type=&quot;hidden&quot; name=&quot;no_note&quot; value=&quot;1&quot;>
<input type=&quot;hidden&quot; name=&quot;currency_code&quot; value=&quot;USD&quot;>
<input type=&quot;image&quot; src=&quot; border=&quot;0&quot; name=&quot;submit&quot; alt=&quot;Make payments with PayPal - it's fast, free and secure!&quot;>
</form>


<?php endif; ?>


This is in the middle of an HTML page. Variables are coming from a form. PHP is having a problem with the &quot;endif&quot; statement. I dont need an &quot;else&quot; statement. How can I close this off?

Please help.

Thank you so much.
 
There is no endif in PHP.
Code:
if(){
   ... your code ...
}
That's it.
 
PHP doesn't normally use an endif.

The typical structure of a PHP if-clause is:

if (some_condition)
{
do some stuff
do some stuff
do some stuff
do some stuff
}
else
{
do some other stuff
do some other stuff
do some other stuff
do some other stuff
}


Braces delimit the actions to be taken.


PHP does support an alternate format for an if-statement which can take an endif. It's really not the standard way of doing it in the language, but it's documented here: . I strongly recommend against their use.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Could you have your form post to a php script which uses a switch/case doodad. Seems like it would be a lot less typing.

 
sleipnir214
Thanks, sleipnir214. I havent' come across the alternative structure format ever. Your recommendation to stay away from it is understandable.

carwee
So, after sleipnir214's post it's clear why it doesn't work. Either you use the braces without endif or the alternate structure with the colon. The braces are much more intutitve and many editors provide easy ways to check for correct opening/closing matches. The alternate format is probably not supported in any common editor.
 
As an addendum....

There are two programming styles in PHP. One is to switch PHP back and forth from program execution mode (inside <?php...?> tags) and HTML output mode (outside those tags):

<?php
do some PHP stuff....
?>
output some html
<?php
do some more php stuff....
?>

The code you've posted is of this form.

Another programming style is to stay in PHP execution mode and to use PHP print or echo statements to output HTML:

<?
do some PHP stuff;

print &quot;some HTML&quot;;

do some more PHP;

print &quot;some more HTML&quot;;

?>


I recommend the use of the &quot;stay in execution mode&quot; style (the second style), particularly as your code gets more complex. It's bad enough to have to keep track of the limits of looping constructs without also having to keep track of which mode you are in.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
As sleipnir state, it is better to have all coded in order to keep track of your page. When I make a php page, 1st step is to create the plain html, example:

Code:
<form action=&quot;[URL unfurl="true"]https://www.paypal.com/cgibin/webscr&quot;;[/URL] method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;cmd&quot; value=&quot;_xclick-subscriptions&quot;>
<input type=&quot;hidden&quot; name=&quot;business&quot; value=&quot;brookscooper3@hotmail.com&quot;>
<input type=&quot;hidden&quot; name=&quot;item_name&quot; value=&quot;Choir Premium Membership&quot;>
...
[CODE]

and then I put it pieces on vars, so:

[CODE]
$form1 = &quot;<form action=\&quot;[URL unfurl="true"]https://www.paypal.com/cgibin/webscr\&quot;;[/URL] method=\&quot;post\&quot;>\n&quot;;
$form1 .= &quot;<input type=\&quot;hidden\&quot; name=\&quot;cmd\&quot; value=\&quot;_xclick-subscriptions\&quot;>\n&quot;;
$form1 .= &quot;<input type=\&quot;hidden\&quot; name=\&quot;business\&quot; value=\&quot;brookscooper3@hotmail.com\&quot;>\n&quot;;
$form1 .= &quot;<input type=\&quot;hidden\&quot; name=\&quot;item_name\&quot; value=\&quot;Choir Premium Membership\&quot;>\n&quot;;
...

print $form1;
[CODE]

my humbly opinion... 
cheer.
 
Unless the HTML to be output is going to be reused somewhere else in my code (for example, identical HTML that will appear in the header and footer of the page), I don't bother with putting all the text in a variable then outputting the variable. I just print the HTML:

Code:
<?php
print '
<form action=&quot;[URL unfurl="true"]https://www.paypal.com/cgibin/webscr&quot;;[/URL] method=&quot;post&quot;>
	<input type=&quot;hidden&quot; name=&quot;cmd&quot; value=&quot;_xclick-subscriptions&quot;>
	<input type=&quot;hidden&quot; name=&quot;business&quot; value=&quot;brookscooper3@hotmail.com&quot;>
	<input type=&quot;hidden&quot; name=&quot;item_name&quot; value=&quot;Choir Premium Membership&quot;>
</form>';
?>

Note the use of single-quotes around the string to keep from having to escape all the internal double-quotes.

If I have a lot to print, I use PHP's HEREDOC style of printing:

Code:
<?php
print <<< EOHTML
<form action=&quot;[URL unfurl="true"]https://www.paypal.com/cgibin/webscr&quot;;[/URL] method=&quot;post&quot;>
	<input type=&quot;hidden&quot; name=&quot;cmd&quot; value=&quot;_xclick-subscriptions&quot;>
	<input type=&quot;hidden&quot; name=&quot;business&quot; value=&quot;brookscooper3@hotmail.com&quot;>
	<input type=&quot;hidden&quot; name=&quot;item_name&quot; value=&quot;Choir Premium Membership&quot;>
</form>
EOHTML;
?>

HEREDOC is useful for printing large chunks of output, but it's not as useful when you have to break a print statement to perform other PHP-code tasks.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
cant you just use a switch statement??

Code:
<?php
$plan = $_GET['plan'];

switch($plan){

case &quot;choirprem&quot; : ..code...; break;
case &quot;choirgold&quot; : ..code...; break;
}
?>

hope this helps


Martin

Computing help and info:

 
hmmm... good point.

I didn't know about HEREDOC... very good to know.


Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top