views:

120

answers:

3

I am trying to echo the action for my form if a post equals 'paypal'

This is what I have:

<?php if $_POST['method'] == 'paypal' echo 'action="paypal/process.php"' else echo 'action="moneybookers/process.php" '?> 

Do i need to print the variable before I do this? what am I doing wrong?

I get this error:

Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /var/www/account/credits/credit_amount.php on line 27
+3  A: 

You are missing parentheses around your if conditional statement:

<?php if( $_POST['method'] == 'paypal' ) 
           echo 'action="paypal/process.php"';
      else 
           echo 'action="moneybookers/process.php"';
?>
Jacob Relkin
Thanks I am new to php. I was also missing the semi-colons ; after the echo
+2  A: 

You should try to format your code properly (ex. parentheses in if statement):

<?php
if ($_POST['method'] == 'paypal') {
    echo 'action="paypal/process.php"';
} else {
    echo 'action="moneybookers/process.php"';
}
?> 
Ondrej Slinták
A: 

It looks as though you formatted it that way because you are displaying the results of that code in a template. You could cut down on the amount of code you need by using a ternary operator:

action="<?php echo ($_POST['method'] == 'paypal' ? 'paypal' : 'moneybookers'); ?>/process.php"

It's essentially the same as saying if condition is true then return A otherwise return B

Matthew Purdon