wizzy886 Posted June 13, 2013 Share Posted June 13, 2013 profit_calc.php <?php $page_title = "Calculating Profit"; include ('includes/header.php'); $pnd = "£"; $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. Quote Link to comment https://forums.phpfreaks.com/topic/279128-php-validating-randomly-generated-number/ Share on other sites More sharing options...
requinix Posted June 13, 2013 Share Posted June 13, 2013 It looks like you're on the right track. Put the generated numbers into the form as hidden inputs. Then in code get the value from the form (if it was submitted) or generate new ones (if not). Quote Link to comment https://forums.phpfreaks.com/topic/279128-php-validating-randomly-generated-number/#findComment-1435831 Share on other sites More sharing options...
wizzy886 Posted June 13, 2013 Author Share Posted June 13, 2013 Now thats clever. I will have a shot at it. Thanks for quick reply. Quote Link to comment https://forums.phpfreaks.com/topic/279128-php-validating-randomly-generated-number/#findComment-1435832 Share on other sites More sharing options...
wizzy886 Posted June 13, 2013 Author Share Posted June 13, 2013 <?php $page_title = "Calculating Profit"; include ('includes/header.php'); $pnd = "£"; $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? Quote Link to comment https://forums.phpfreaks.com/topic/279128-php-validating-randomly-generated-number/#findComment-1435860 Share on other sites More sharing options...
Q695 Posted June 13, 2013 Share Posted June 13, 2013 Hidden fields are for those just being used to hide data from a user, but keep it on the form. try something else, like a text, instead of hidden. the value will show up, and you can see it at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/279128-php-validating-randomly-generated-number/#findComment-1435870 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.