Jump to content

PHP Validating Randomly Generated Number


wizzy886

Recommended Posts

profit_calc.php

<?php
	
	$page_title = "Calculating Profit";
	include ('includes/header.php');

	$pnd = "&pound";

	$answer = $_POST['answer'];

	/*if (!defined($income && $costs)) {
		
	}*/
	if (!isset($_POST['answer'])) {
		$income= number_format(rand(50, 30000),1);
		$costs= number_format(rand(100, 20000),1);
	} else {
		$income_new = $income;
		$costs_new = $costs;
	}
?>

<div id="spacer"></div>
	<div id="title" class="text1"><h1><?php echo $page_title ?></h1></div>
<div id="spacer"></div>
	<div id="question" class="text2">

		<p>If a company makes a total turnover of <?php echo $pnd.$income; ?> and all the costs come to <?php echo $pnd.$costs; ?>.</p> 

			<p>What is the total profit made by this company?</P>

		<?php

				$profits= number_format($income_new-$costs_new, 1);

					if (!empty($_POST['answer'])) {
						number_format($answer, 1);
							if ($answer == $profits) {
								echo '<div class="correct"></div>';
							} else {
								echo '<div class="incorrect"></div>';
							}
					} else {
						$answer = NULL;
						echo "Enter in your answer below.";
					}

		?>

	</div>
<div id="spacer"></div>
	<div id="submit_box">
		<form action="profit_calc.php" method="POST" name="form">
			<input type="text" size="12" name="answer">
			<div id="input_submit"><input type="submit" name="submit" value="SUBMIT"></div>
		</form>
	</div>

<script type="text/javascript" language="JavaScript">
document.forms['form'].elements['answer'].focus();
</script>

<?php include ('includes/footer.php'); ?>

So basically I want this script to randomly generate two numbers that are then taken away from each other to give you another value. This value will then be validated and display whether you got it right or not. The bit I am struggling with is to get the randomly generated number to follow through to the validation of the actual answer itself. How would I go about doing this. Thank you in advance.

<?php
	
	$page_title = "Calculating Profit";
	include ('includes/header.php');

	$pnd = "&pound";

	$answer = $_POST['answer'];


	if (!defined($income && $costs)) {
		$income= number_format(rand(50, 30000),1);
		$costs= number_format(rand(100, 20000),1);	
	} else {
		if (empty($_POST['submitted_income' && 'submitted_costs'])) {
			$_POST['submitted_income'] = $income;
			$_POST['submitted_costs'] = $costs;
		}
	}
?>

<div id="spacer"></div>
	<div id="title" class="text1"><h1><?php echo $page_title ?></h1></div>
<div id="spacer"></div>
	<div id="question" class="text2">

		<p>If a company makes a total turnover of <?php echo $pnd.$income; ?> and all the costs come to <?php echo $pnd.$costs; ?>.</p> 

			<p>What is the total profit made by this company?</P>

		<?php

				$profits= number_format($_POST['submitted_income'] - $_POST['submitted_costs'], 1);

					if (!empty($_POST['answer'])) {
						number_format($answer, 1);
							if ($answer == $profits) {
								echo '<div class="correct"></div>';
							} else {
								echo '<div class="incorrect"></div>';
							}
					} else {
						$answer = NULL;
						echo "Enter in your answer below.";
					}

		?>

	</div>
<div id="spacer"></div>
	<div id="submit_box">
		<form action="profit_calc.php" method="POST" name="form">
			<input type="text" size="12" name="answer">
			<div id="input_submit"><input type="submit" name="submit" value="SUBMIT"></div>
			<input type="hidden" name="submitted_income" value="<?php echo $_POST['submitted_income']; ?>">
			<input type="hidden" name="submitted_costs" value="<?php echo $_POST['submitted_costs']; ?>">
		</form>
	</div>

<script type="text/javascript" language="JavaScript">
document.forms['form'].elements['answer'].focus();
</script>

<?php include ('includes/footer.php'); ?>





So I have written this and still no success. I am new to using this hidden form part, and wondered what I am doing and why it is wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.