Hi there,

I need a bit of help with redisplaying a form.

Basically, currently a user will fill out my contact form, the form and it's contents are passed to my verification page, and if the recaptcha was entered correctly it goes to a Thank You page.

When the recaptcha is entered INCORRECTLY, I want to redisplay the contact form with the fields already filled out. How do I do this?

Here is my verification code. Any help would be great:

<?php require('sbsquared.class.php'); ?>
<?php
  require_once('recaptchalib.php');
  $privatekey = "myprivatekey";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
	header("Location: http://www.google.com");  <--- this is the bit that I want to redisplay the form with fields already filled out.
  } else {
    
	$sb = New SBSquared;

	$name = $_POST['FullName'];
	$post_keys = array_keys($_POST);
	$my_db_string = "<table>";
	$ip_address = $_SERVER['REMOTE_ADDR'];
	foreach($post_keys as $field)
	{
		if($_POST[$field] != "" && $field != "submit_y" && $field != "submit_x" && $field != "submit_x")
		{
		
			$my_db_string .= "<tr><td><b>".$field.":</b></td><td>";
			if($field == "Email")
			{
				$my_db_string .= '<a href = "mailto:'.$_POST['Email'].'">'.$_POST['Email'].'</a>';
			}
			else
			{
				$my_db_string .= $_POST[$field];
			}
			
			$my_db_string .= "</td></tr>";
			
		}
	}
	
	$my_db_string .= "<tr><td><b>IP ADDRESS LOGGED: </b></td><td>".$ip_address."</td></tr>";
	
	$my_db_string .= "</table>";
	
	if(get_magic_quotes_gpc() != 1)
	{
		$my_db_string = addslashes($my_db_string);
		$name = addslashes($name);
	}

	$conn = $sb->openConnection();
	$dts = time();
	$sql = "INSERT INTO `contact_queries` VALUES ('', '$name', '$my_db_string', 'n/a', 0, $dts)";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	
$content = '<div id="main_middle">';

$content .= '<span class="title">'.$sb->dt('Contact').'</span>
<p>'.$sb->dt('Thank you for your enquiry. We will contact you shortly.').'</p>


</div>';

// admin auto email.
		$dts = date("d.m.y h:ia", time());
		$admin_content = "New contact query at $dts";
		$admin_content .= "\n\n--\n\n \r\n\r\n";
		mail("email address", 'NOTIFICATION: new query', $admin_content, 'From: email address');
		$FILE=fopen("./log/auto-contact.txt","a");
		fwrite($FILE, $admin_content);
		fclose($FILE);

echo pageHeader($sb);
echo pageContent($sb, $content);
echo pageFooter($sb);
  }
?>

Recommended Answers

All 40 Replies

See the example given in this thread.

I've gone through my form and added value="<?php echo "".$_SESSION.""; ?>" (and variations of) to all of the fields.

I'm a bit confused with what else I need to do though, because currently, it is just replying this: www.electrix.co.uk/contact2.php

I don't really understand what I have to do with my verify page?

Copy the values of POST to SESSION. See the example in the other thread. The echo example in the thread is probably different from your code, that's why you get the echo in the value.

Right, nearly got it! I've got it so that the Name, Address etc fields repopulate with the already entered text - see www.electrix.co.uk/contact2.php

However, the same technique doesn't seem to work with drop down boxes (for country / Enquiry Type) or the text area for the Enquiry itself.

Is there a way to repopulate these aswell?

Currently I'm using these in my contact2.php code:

<td align = "right"> <select name="country" style="font-size:90%;width:202px;border:1px solid #808080" value="'.$_SESSION["country"].'"">
'.genCountryList().'
</select></td>
<td align = "right">
<select name="EnquiryType" style="font-size:100%;width:202px;border:1px solid #808080" value="'.$_SESSION["EnquiryType"].'" />
<option value="Sales">'.$sb->dt('Sales').'</option>
<option value="Technical">'.$sb->dt('Technical').'</option>
<option value="Quotations">'.$sb->dt('Quotations').'</option>
<option value="General Enquiry">'.$sb->dt('General Enquiry').'</option>
<option value="Literature Request">'.$sb->dt('Literature Request').'</option>
</select>
</td>
<td align = "right"><textarea name="Enquiry" rows="8" style="font-size:90%;width:200px;border:1px solid #808080; font: 9pt arial" value="'.$_SESSION["Enquiry"].'" ></textarea></td>

And this code in my contact-complete2.php (or verify page):

if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
	$_SESSION['EnquiryType'] = $_POST['EnquiryType'];
	$_SESSION['FullName'] = $_POST['FullName'];
	$_SESSION['Company'] = $_POST['Company'];
	$_SESSION['JobTitle'] = $_POST['JobTitle'];
	$_SESSION['address_1'] = $_POST['address_1'];
	$_SESSION['address_2'] = $_POST['address_2'];
	$_SESSION['address_3'] = $_POST['address_3'];
	$_SESSION['address_4'] = $_POST['address_4'];
	$_SESSION['Postcode'] = $_POST['Postcode'];
	$_SESSION['country'] = $_POST['country'];
	$_SESSION['Telephone'] = $_POST['Telephone'];
	$_SESSION['Email'] = $_POST['Email'];
	$_SESSION['Fax'] = $_POST['Fax'];
	$_SESSION['Website'] = $_POST['Website'];
	$_SESSION['Enquiry'] = $_POST['Enquiry'];
	header("Location: contact2.php?error=recaptcha");

Thanks. That'll work for the drop down selections, but what about the text area (the Enquiry box)?

Put the value between the tags.

Hi pritaeas.

Thanks for your help so far. I've got it working with the names, addresses and enquiry box now, but I can't get those drop down boxes to work.

I think it's because my html is all in php strings. How would I write:

<option value='1' <?php if($_POST['order_of_products_by_values1']=='1') echo 'selected="selected"';?> >Pret crescator</option>

... without <?php? I've tried a few variations but nothing seems to work (keep getting syntax errors).

Also, this method, once fixed, will work for my Enquiry Type box (sales, quotations etc), but it won't work for my Country drop down list as I am pulling in the list of countries like this:

<td align = "right"> <select name="country" style="font-size:90%;width:202px;border:1px solid #808080" value="'.$_SESSION["country"].'"">
'.genCountryList().'
</select></td>

Is there a workaround for that?

If you get syntax errors, it is most likely that your quotes are off.

For your last case, I'd suggest changing your genCountryList function, so it accepts a selected country parameter, which you can use to build the dropdown.

I've copied this exactly:

<?php if($_POST['order_of_products_by_values1']=='1') echo 'selected="selected"';?>

into my options, but it gives errors due to my HTML all being in PHP strings. The "<?" won't work like this. Any chance you could just show me once how to write it with this in mind?


ps. I really appreciate your help. Truth is, I didn't create this website initially and I'm not really qualified to be editing it so much so the simplest fixes the better.

Just this single line looks fine. Perhaps it is surrounding code that triggers your error. Really can't say anything sensible without more seeing code. What error are you getting anyway ?

These are the lines that I'm attempting to add that code into:

<td align = "right">
<select name="EnquiryType" style="font-size:100%;width:202px;border:1px solid #808080" value="'.$_SESSION["EnquiryType"].'" />
<option value="Sales">'.$sb->dt('Sales').'</option>
<option value="Technical">'.$sb->dt('Technical').'</option>
<option value="Quotations">'.$sb->dt('Quotations').'</option>
<option value="General Enquiry">'.$sb->dt('General Enquiry').'</option>
<option value="Literature Request">'.$sb->dt('Literature Request').'</option>
</select>
</td>

Bear in mind that my HTML is all in PHP strings (I'm not sure why they built it like this), so it doesn't like the "<?php" I don't think.

All the errors say are "there is a syntax error on line X. Code hinting may not work until you fix this error".

Awful, but it should work:

<option value="Sales"' . ((isset($_POST['order_of_products_by_values1']) and ($_POST['order_of_products_by_values1']=='1')) ? ' selected="selected"' : '') . '>' . $sb->dt('Sales') . '</option>

Thanks man. That gets rid of the errors but it doesn't make the drop down option stay selected when they return to the page?

I'm guessing I need something else in the verify.php but I'm not sure what?

You would have to store the current selection somewhere, $_SESSION perhaps. But I do not know enough of your process to tell you where to put what.

Well my verify page looks like this (the part of the code that is refilling the rest of the fields):

if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
	$_SESSION['EnquiryType'] = $_POST['EnquiryType'];
	$_SESSION['FullName'] = $_POST['FullName'];
	$_SESSION['Company'] = $_POST['Company'];
	$_SESSION['JobTitle'] = $_POST['JobTitle'];
	$_SESSION['address_1'] = $_POST['address_1'];
	$_SESSION['address_2'] = $_POST['address_2'];
	$_SESSION['address_3'] = $_POST['address_3'];
	$_SESSION['address_4'] = $_POST['address_4'];
	$_SESSION['Postcode'] = $_POST['Postcode'];
	$_SESSION['country'] = $_POST['country'];
	$_SESSION['Telephone'] = $_POST['Telephone'];
	$_SESSION['Email'] = $_POST['Email'];
	$_SESSION['Fax'] = $_POST['Fax'];
	$_SESSION['Website'] = $_POST['Website'];
	$_SESSION['Enquiry'] = $_POST['Enquiry'];
	header("Location: contact2.php?error=recaptcha");
  } else {
//What happens when the CAPTCHA is entered correctly.

And my drop down box now looks like this:

<select name="EnquiryType" style="font-size:100%;width:202px;border:1px solid #808080" value="'.$_SESSION["EnquiryType"].'" />
<option value="Sales"' . ((isset($_POST['EnquiryType']) and ($_POST['EnquiryType']=='Sales')) ? ' selected="selected"' : '') . '>' . $sb->dt('Sales') . '</option>
<option value="Technical" ' . ((isset($_POST['EnquiryType']) and ($_POST['EnquiryType']=='Technical')) ? ' selected="selected"' : '') . '>'.$sb->dt('Technical').'</option>
<option value="Quotations" ' . ((isset($_POST['EnquiryType']) and ($_POST['EnquiryType']=='Quotations')) ? ' selected="selected"' : '') . '>'.$sb->dt('Quotations').'</option>
<option value="General Enquiry" ' . ((isset($_POST['EnquiryType']) and ($_POST['EnquiryType']=='General Enquiry')) ? ' selected="selected"' : '') . '>'.$sb->dt('General Enquiry').'</option>
<option value="Literature Request" ' . ((isset($_POST['EnquiryType']) and ($_POST['EnquiryType']=='Literature Request')) ? ' selected="selected"' : '') . '>'.$sb->dt('Literature Request').'</option>
</select>

Looks like interchanging $_POST for $_SESSION may do the trick.

In the drop down or the verify?

Tried changing all the options to read SESSION, but no luck.

Can't say, should have worked in the dropdown. If you echo $_SESSION somewhere in the page, does it have a value ?

Yep. It shows up just saying Sales, or Quotations etc.

Strange, then it should work. Not sure where to look now.

Oh well. I'm completely stuck then.

Could you tell me very quickly how to display an error popup message saying "The captcha you entered was incorrect. Please try again."?

Sorry, not that familiar with captcha. I don't know what the normal flow is of that action.

Basically, someone enters the wrong recaptcha, it goes to my verify page which establishes whether it was entered correctly or not. If it was it sends you one place, if not, it sends you back to the form (with all the fields fill out - most of them anyway), but I want it to display something somewhere on the page which says, the Recaptcha was entered incorrectly etc.

The verify page also fires off correctly entered forms to our message receiver.

Here is my entire verify page if you can help:

<?php require('sbsquared.class.php'); ?>
<?php
  require_once('recaptchalib.php');
  $privatekey = "myprivatekey";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
	$_SESSION['EnquiryType'] = $_POST['EnquiryType'];
	$_SESSION['FullName'] = $_POST['FullName'];
	$_SESSION['Company'] = $_POST['Company'];
	$_SESSION['JobTitle'] = $_POST['JobTitle'];
	$_SESSION['address_1'] = $_POST['address_1'];
	$_SESSION['address_2'] = $_POST['address_2'];
	$_SESSION['address_3'] = $_POST['address_3'];
	$_SESSION['address_4'] = $_POST['address_4'];
	$_SESSION['Postcode'] = $_POST['Postcode'];
	$_SESSION['country'] = $_POST['country'];
	$_SESSION['Telephone'] = $_POST['Telephone'];
	$_SESSION['Email'] = $_POST['Email'];
	$_SESSION['Fax'] = $_POST['Fax'];
	$_SESSION['Website'] = $_POST['Website'];
	$_SESSION['Enquiry'] = $_POST['Enquiry'];
	header("Location: contact2.php?error=recaptcha");
  } else {
    
	$sb = New SBSquared;

	$name = $_POST['FullName'];
	$post_keys = array_keys($_POST);
	$my_db_string = "<table>";
	$ip_address = $_SERVER['REMOTE_ADDR'];
	foreach($post_keys as $field)
	{
		if($_POST[$field] != "" && $field != "submit_y" && $field != "submit_x" && $field != "submit_x")
		{
		
			$my_db_string .= "<tr><td><b>".$field.":</b></td><td>";
			if($field == "Email")
			{
				$my_db_string .= '<a href = "mailto:'.$_POST['Email'].'">'.$_POST['Email'].'</a>';
			}
			else
			{
				$my_db_string .= $_POST[$field];
			}
			
			$my_db_string .= "</td></tr>";
			
		}
	}
	
	$my_db_string .= "<tr><td><b>IP ADDRESS LOGGED: </b></td><td>".$ip_address."</td></tr>";
	
	$my_db_string .= "</table>";
	
	if(get_magic_quotes_gpc() != 1)
	{
		$my_db_string = addslashes($my_db_string);
		$name = addslashes($name);
	}

	$conn = $sb->openConnection();
	$dts = time();
	$sql = "INSERT INTO `contact_queries` VALUES ('', '$name', '$my_db_string', 'n/a', 0, $dts)";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	
$content = '<div id="main_middle">';

$content .= '<span class="title">'.$sb->dt('Contact').'</span>
<p>'.$sb->dt('Thank you for your enquiry. We will contact you shortly.').'</p>


</div>';

// admin auto email.
		$dts = date("d.m.y h:ia", time());
		$admin_content = "New contact query at $dts";
		$admin_content .= "\n\n--\n\n \r\n\r\n";
		mail("email address", 'NOTIFICATION: new query', $admin_content, 'From: email address');
		$FILE=fopen("./log/auto-contact.txt","a");
		fwrite($FILE, $admin_content);
		fclose($FILE);

echo pageHeader($sb);
echo pageContent($sb, $content);
echo pageFooter($sb);
  }
?>

$_GET will be set to 'recaptcha' (line 27), so you can check that and show a message.

use the $_POST array to display the entered form values. this method works best if the form or page submits to itself not to another page
for example

<form action="userlogin.php" method="post">
<p>Username:<input type="text" name="txtUsername" size="16" value="<?php if( isset( $_POST['txtUsername'] ) ) echo $_POST['txtUsername']; ?>" /></p>
<p>Password:<input type="password" name="txtPassword" />

$_GET will be set to 'recaptcha' (line 27), so you can check that and show a message.

I'm sorry for being incredibly dumb, but how exactly do I do this? I'm assuming it's something I need to put in my contact5.php but I genuinely don't know how to do it.

if (isset($_GET['error']) and ($_GET['error'] == 'recaptcha')) {
  echo 'There was an error in your recaptcha';
}

In the verify code of the contact form code?

Thank you by the way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.