Page 1 of 1

Not sure how to do this

Posted: Tue May 03, 2011 7:08 pm
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>

Re: Not sure how to do this

Posted: Wed May 04, 2011 6:18 am
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 ?>