Not sure how to do this

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
smordue
Forum Newbie
Posts: 7
Joined: Mon Dec 07, 2009 1:07 pm

Not sure how to do this

Post by smordue »

Basically I have these two divs, one is the default but if the person enters the correct discount it should be hidden and the other one displayed, if they enter the wrong code it should leave the default and display "wrong code" I know this is not secure, but it is not important for this. Obviously, I am not a coder :D .

Code: Select all

<?php
	$discount = "rain15";
	if ($_POST['txtdiscount'] != $discount) {
	?>
	<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	    <p><label for="txtdiscount" style="color:red">Discount Code:</label>
	    <input type="discount" title="Enter Discount Code" name="txtdiscount" />
	    </p>
	</form>
	<?php
	}
		$('#div1').css({'display' : 'block'});
		$('#div2').css({'display' : 'none'});
	else {
echo "wrong code";
	?>
	<?php
	}
?>
<div id="div1" style="display:none">Content to show is discount is correct</div>
<div id="div2" style="display:block">Content that is hidden if discount is correct</div>
eivind
Forum Commoner
Posts: 28
Joined: Tue Apr 19, 2011 7:57 am

Re: Not sure how to do this

Post by eivind »

This works great for me:

Code: Select all

<?php
// Set discount to true or false
if($_POST['txtdiscount'] == 'rain15'){
	$discount = true;
}else{
	$discount = false;
}

// show form if not submitted
if($_SERVER['REQUEST_METHOD'] != 'POST'){ ?>
        <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <p><label for="txtdiscount" style="color:red">Discount Code:</label>
            <input type="discount" title="Enter Discount Code" name="txtdiscount" />
            </p>
        </form>
<div id="div1">Content to show before submit</div>
<?php }else{ // if submitted, show result ?>
<?php if($discount){ // if discount, show correct text ?>
<div id="div2">Content to show if discount is correct</div>
<?php }else{ // if not discount, shot incorrect text ?>
<div id="div3">Content to show if discount is incorrect</div>
<?php } // end discount if ?>
<?php } // end post if ?>
Post Reply