Jump to content

random number match


jarvis

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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-

 

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!?!?!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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