jarvis Posted March 15, 2010 Share Posted March 15, 2010 Hi, I a daft question. I use <?php $number = rand(1,10); ?> to generate a random number between 1 & 10 I then show that using <?php echo $number; ?> Why is it that if this is a simple form, when you submit it this doesn't work. It always returns no rather than match, whether you enter the same numer in the form or not! ARGHHHHHH! :'( if (($_REQUEST['number']) == $number) { echo 'match'; }else{ echo 'no'; } Guessing it's easy & im being daft! Thanks Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2010 Share Posted March 15, 2010 You would need to store the number in a session variable. Web servers are stateless. They don't know or care what happened on any page request before or after the current page request. All resources used on any page, such as a variable, are destroyed when the server finishes with that page request. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted March 15, 2010 Share Posted March 15, 2010 You would need to store the number in a session variable. Web servers are stateless. They don't know or care what happened on any page request before or after the current page request. All resources used on any page, such as a variable, are destroyed when the server finishes with that page request. In other words, every time the script is ran, the $number variable becomes a new random number. It isn't saved or stored in any way. So with each page request it is likely (but not absolute) to be different. Quote Link to comment Share on other sites More sharing options...
jarvis Posted March 15, 2010 Author Share Posted March 15, 2010 Obvious really! Feeling rather foolish!!! Is someone able to help me out or point me in the right direction? many thanks Quote Link to comment Share on other sites More sharing options...
Wolphie Posted March 15, 2010 Share Posted March 15, 2010 As PFMaBiSmAd said, store the number in a session variable.. <?php $_SESSION['number'] = rand(1, 10); // This will make the number absolute until changed by you if ($_REQUEST['number'] == $_SESSION['number']) { // correct } else { // false } ?> Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted March 15, 2010 Share Posted March 15, 2010 I use this on my forms. You need to echo the rand number in a text field in order for it to be passed along to the form processing script. Simply echo the rand number in the INITIAL VALUE of the text field. Now it will be passed along to the processing script like any other input from the form and you can do with it as you please. I suspect that the reason why it is not working for you is one of two reasons; 1- you simply have it as an echo statement and not in a text field. 2- if you are echoing it in the INITIAL VALUE of the text field on the form- then i suspect thtat the name of the text field does not match the $_POST variable that you have in the processing script. Cheers- Quote Link to comment Share on other sites More sharing options...
jarvis Posted March 15, 2010 Author Share Posted March 15, 2010 Hi all, thanks for the replies. Ok, in rough format (until i get it working). This is my code: <?php session_start(); ?> <?php // process form if (@$_REQUEST['submitForm']) { // error checking $errorsAndAlerts = ""; if (!@$_REQUEST['quick_enquiry_name']) { $errorsAndAlerts = "No name\n"; } if (!@$_REQUEST['number']) { $errorsAndAlerts = "Add a number\n"; } $_SESSION['number'] = rand(1, 10); if ($_REQUEST['number'] == $_SESSION['number']) { echo 'yay'; }else { echo 'no'; } } ?> <?php $number = rand(1,10); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Quick Enquiry Confirmation</title> </head> <body> <form method="post"> <?php if (@$errorsAndAlerts): ?> <div style="color: red; font-weight: bold; font-size: 12px; font-family: arial;"> <?php echo $errorsAndAlerts; ?> </div> <?php endif ?> <?php echo $number; ?> <table width="213" border="0" cellpadding="2" cellspacing="2"> <tr> <td colspan="2"> <input name="quick_enquiry_name" type="text" id="quick_enquiry_name" style="width:180px;" value="Name..." onclick="clickclear(this, 'Name...')" onblur="clickrecall(this,'Name...')" /> <input type="text" id="number" name="number" style="width:180px;" /> </td> </tr> <tr> <td><div align="right"> <input type="image" src="images/blue_submit_button.jpg" value="Submit" alt="Submit" /> </div></td> </tr> </table> <input type="hidden" name="submitForm" value="1" /> </form> However, it works sometimes and not others! Grrr.... Something so simple can take sooooo much time! Quote Link to comment Share on other sites More sharing options...
jarvis Posted March 15, 2010 Author Share Posted March 15, 2010 I'd like to try this without sessions but think I've not grasped meltingpoint's method <?php // process form if (@$_REQUEST['submitForm']) { // error checking $errorsAndAlerts = ""; if (!@$_REQUEST['quick_enquiry_name']) { $errorsAndAlerts = "No name\n"; } if (!@$_REQUEST['number']) { $errorsAndAlerts = "Add a number\n"; } $test = $_REQUEST['test']; echo $test; if (($_REQUEST['number']) == $test) { echo 'match'; }else{ echo 'no'; } } ?> <?php $number = rand(1,10); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Quick Enquiry Confirmation</title> </head> <body> <form method="post"> <?php if (@$errorsAndAlerts): ?> <div style="color: red; font-weight: bold; font-size: 12px; font-family: arial;"> <?php echo $errorsAndAlerts; ?> </div> <?php endif ?> <?php echo $number; ?> <table width="213" border="0" cellpadding="2" cellspacing="2"> <tr> <td colspan="2"> <input name="quick_enquiry_name" type="text" id="quick_enquiry_name" style="width:180px;" value="Name..." onclick="clickclear(this, 'Name...')" onblur="clickrecall(this,'Name...')" /> <input type="text" id="number" name="number" style="width:180px;" /> <input type="hidden" id="test" value="<?php echo $number;?>" style="width:180px;" /> </td> </tr> <tr> <td><div align="right"> <input type="image" src="images/blue_submit_button.jpg" value="Submit" alt="Submit" /> </div></td> </tr> </table> <input type="hidden" name="submitForm" value="1" /> </form> Thanks Quote Link to comment Share on other sites More sharing options...
Wolphie Posted March 15, 2010 Share Posted March 15, 2010 Here's a very simple way of doing it without using sessions <?php $rand = rand(1, 10); if (isset($_POST['submit'])) { if (preg_match('/[0-9]/', $_POST['user_input']) && preg_match('/[0-9]/', $_POST['user_input'])) { if ($_POST['user_input'] == $_POST['number']) { echo '<p>Correct!</p>'; } else { echo '<p>In-correct!</p>'; } } else { echo '<p>Please enter a valid number.</p>'; } } ?> <html> <body> <?php echo '<p>'. $rand .'</p>'; ?> <form name="form_sent" method="POST"> <input type="text" name="user_input" /><br /> <input type="hidden" name="number" value="<?php echo $rand; ?>" /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
jarvis Posted March 15, 2010 Author Share Posted March 15, 2010 Thanks all, I think I've now got it working. Albeit an undefined warning message. Here's my final code: if (@$_REQUEST['submitForm']) { // error checking $errorsAndAlerts = ""; $check = $_REQUEST['check']; $randomNumber = $_REQUEST['randomNumber']; if (!@$_REQUEST['name']) { $errorsAndAlerts = "Please add your name\n"; } if (!@$_REQUEST['email']) { $errorsAndAlerts = "Please add your email\n"; } else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts = "That email address is not valid\n"; } if (!@$_REQUEST['enquiry']) { $errorsAndAlerts = "Please add your enquiry\n"; } if (!@$_REQUEST['randomNumber']) { $errorsAndAlerts = "Please verify the number\n"; } $textToConvert = $_REQUEST['enquiry']; $convertedText = iconv("UTF-8", "ISO-8859-1", $textToConvert); // send email user if ((!$errorsAndAlerts) && ($randomNumber == $check)) { $to = "info@domain.co.uk"; $subject = "Quick Contact"; $email = $_REQUEST['email']; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; #$headers .= "Content-Type: text/html; charset=utf8\r\n"; $headers .= "From: $email \r\n"; $body = "<strong>Full name</strong>: ". $_REQUEST['name'] ."<br />"; $body .= "<strong>Email</strong>: ".$_REQUEST['email'] ."<br />"; $body .= "<strong>Enquiry</strong>: $convertedText<br /><br />"; $body .= "The user who sent this message had the IP address <strong>".$_SERVER['REMOTE_ADDR']."</strong><br />"; $message = $body; // send message $mailResult = @mail($to, $subject, $message, $headers); if (!$mailResult) { die("Mail Error: $php_errormsg"); } // $errorsAndAlerts = "Thanks! We've sent your email."; $_REQUEST = array(); // clear form values } } ?> <?php $randomNumber = rand(1,10); ?> <!-- right quick_contact form --> <!-- quick_contact_title --> <div class="rh_quick_contact_title"> <img src="images/rh_quick_enquiry_title.jpg" width="232" height="31" /> </div> <!-- /quick_contact_title --> <!-- quick_contact_form --> <div class="rh_quick_contact_content"> <form method="post"> <input type="hidden" name="submitForm" value="1" /> <?php if (@$errorsAndAlerts): ?> <div style="color: red; font-weight: bold; font-size: 12px; font-family: arial;"> <?php echo $errorsAndAlerts; ?> </div> <?php endif ?> <table width="213" border="0" cellpadding="2" cellspacing="2"> <tr> <td align="center"> <input name="name" type="text" id="name" style="width:180px;" value="Name..." onclick="clickclear(this, 'Name...')" onblur="clickrecall(this,'Name...')" /> </td> </tr> <tr> <td align="center"> <input name="email" type="text" id="email" style="width:180px;" value="Email Address..." onclick="clickclear(this, 'Email Address...')" onblur="clickrecall(this,'Email Address...')"/> </td> </tr> <tr> <td align="center"> <textarea name="enquiry" id="enquiry" value="Enquiry..." onclick="clickclear(this, 'Enquiry...')" onblur="clickrecall(this,'Enquiry...')" style="width:180px;" rows="3"></textarea> </td> </tr> <tr> <td><?php echo $randomNumber; ?> <input type="text" id="randomNumber" name="randomNumber" style="width:20px;" /> Please enter the number </td> </tr> <tr> <td><div align="right"> <input type="image" src="images/blue_submit_button.jpg" value="Submit" alt="Submit"> </div></td> </tr> </table> <input type="hidden" id="check" name="check" value="<?php echo $randomNumber;?>" /> </form> Thank you all for your help. I think that's everything I need!?!?! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted March 15, 2010 Share Posted March 15, 2010 Why do you have the @ character in your if-statements? That operator is used when you want to suppress errors from calling a function, which you are not doing where you are using it. Also, stop using $_REQUEST. Use $_POST or $_GET depending on where your input is coming from. Quote Link to comment Share on other sites More sharing options...
jarvis Posted March 15, 2010 Author Share Posted March 15, 2010 Hi roopurt18 I'll alter those now. It's code i've inherited, I'm just adding to it. is there anyway I can redirect the user after the form is complete with correct info? Can only think of header() but the above is an include on a page so gives the headers sent error Thanks Quote Link to comment 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.