Jump to content

[SOLVED] Wrapping if statement around HTML/PHP


flashpipe

Recommended Posts

I'm new to php and I need to wrap an if statement around a group of php/html and don't know how to go about it...if anyone could help, that would be great...

 

I need to make the following chunk of code display based on the if statement below:

 

<form action="<?php echo($cartweaver->thisPageName . "?cartid=" . $_SESSION["CartId"]);?>" method="post" name="addToCart">
<?php cw3ProductOptions($productId, $taxRate);?>
    <input name="prodId" type="hidden" value="<?php //echo($productId);?>">
    <input name="submit" type="submit" class="formButton" value="Add to Cart">
</form> 

 

and this is the if statement I need to wrap around it...

 

if(isset($_SESSION["customerID"]) && $_SESSION["customerID"] != "0") {

} 

 

Thanks!!

Link to comment
Share on other sites

<?php
if(isset($_SESSION["customerID"]) && $_SESSION["customerID"] != "0") {
?>
<form action="<?php echo($cartweaver->thisPageName . "?cartid=" . $_SESSION["CartId"]);?>" method="post" name="addToCart">
<?php cw3ProductOptions($productId, $taxRate);?>
    <input name="prodId" type="hidden" value="<?php //echo($productId);?>">
    <input name="submit" type="submit" class="formButton" value="Add to Cart">
</form> 
<?php
}
?>

Link to comment
Share on other sites

That is a shady way to do it, the proper way is to have the data you want printed in a string and print it out at the end of the script to avoid any issues that might come from modifying headers etc. This is the way i would go about doing it:

 

<?php
$form=false;
if(isset($_SESSION["customerID"]) && $_SESSION["customerID"] != "0") {
    $form = '<form action="' . $cartweaver->thisPageName . '?cartid=' . $_SESSION["CartId"] . '" method="post" name="addToCart">
     '. cw3ProductOptions($productId, $taxRate) .'
    <input name="prodId" type="hidden" value="'.$productId.'">
    <input name="submit" type="submit" class="formButton" value="Add to Cart">
</form>';
}

if (!$form) { 
    echo $form;
}

 

This way you have full control and do not have to worry about conflicting items such as session_start or headers or even setcookie.

 

Just another option to do it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.