Jump to content

Sticky forms with captcha


Enderpanda

Recommended Posts

Hi everyone,

 

I'm fairly new to working with forms, and having a bit of a difficult time getting this all tied together.  I'm trying to use a simple contact form with reCAPTCHA, using Spry (Javascript) to validate the text area (everything else is optional):

- If they enter something in the text area and solve the reCAPTCHA, it takes them to comment_sent.php

- If they enter something in the text area, it takes them to comment_error.php

- If they don't enter anything into the text area, it turns red and they can't sumbit

 

Therefore the only real validation that takes you to another page and would need to retain data would be the reCAPTCHA part... Simple enough right?  It all works pretty well...

 

Except for the damn sticky forms part... Not sure what I am doing wrong.  I've tried a few different things and at this point I'm just frustrated.  Feel like I'm close but missing something simple.

 

My contact form can be seen directly at http://enderpanda.com/contact.php (tho on my site I use it as a 540px-wide shadowbox popup).

 

Couple notes: 1) the action of the form points to contact_mailer.php, which is basically some reCAPTCHA code I borrowed and the default godaddy script (I know, I should get a new one.. it works for now and I want to understand what is going on with this first) 2) currently the way I have it "jumps a page" (goes to contact_error.php) but I'd like to rewrite it to just reload contact and apply variables... but I want to try and get it work this way first if possible 3) some examples I have seen use $_SERVER['PHP_SELF'] instead of pointing to processing file... is that part of the problem? 4) I admit I don't complete understand what is going on the default godaddy script... maybe that's why the $_POST stuff isn't coming thru? 5) I tried adding isset, such as the example here, but that didn't seem to work... the code creating the captfail variable in contact_error seems to be working fine.

 

Again, the problem I'm having right now is that the forms aren't sticky, the data is gone if you answer the captcha wrong and you have blank fields (which is very, very good way to make people never come back to your site again).

 

Sorry if this a completely noob question (I'm sure it is), THANK YOU very much for any help you can give, I really appreciate it.

 

 

contact.php (key omitted)

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contant Enderpanda</title>
<link href="config/main.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="contact_form_wrap">
<form action="contact_mailer.php" method="post"> 
<input type="hidden" name="subject" value="Comment From Enderpanda.com" /> 
<input type="hidden" name="redirect" value="contact_sent.php" />
<label for="name">Name (optional)<br /></label>
<input name="name" type="text" id="name" size="50" maxlength="200" /><br /><br />
<label for="email">Email (optional)<br /></label>
<input name="email" type="text" id="email" size="50" maxlength="200" /><br /><br />
<label for="message_sub">Subject (optional)<br /></label>
<input name="message_sub" type="text" id="message_sub" size="50" maxlength="200" /><br><br>
<label for="message">Message<br/></label>
<span id="sprytextarea1">
<textarea name="message" cols="50" rows="6" id="message"></textarea>
<span class="textareaRequiredMsg">You must include a message.</span></span><br /><br />
<? require_once('recaptchalib.php');
$publickey = "OMITTED"; 
echo recaptcha_get_html($publickey);
?><br>
<input type="submit" name="submit" value="Send Message" />
</form>
<p><a href="#" onclick="parent.Shadowbox.close();return false;"> Close Window </a></p>
</div>
<script type="text/javascript">
<!--
var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1");
//-->
</script>
</body>
</html>

 

contact_mailer.php (key omitted)

<?php

/* reCAPTCHA script from http://inko9nito.wordpress.com/2007/12/12/installing-recaptcha-with-php/ */

require_once('recaptchalib.php');
$privatekey = "OMITTED";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
header("location:contact_error.php?capresult=fail");
die();
}
else if ($resp->is_valid) {
header("location:contact_sent.php?capresult=pass");
}

/* default GoDaddy script (from gdform.php on my server) */

$request_method = $_SERVER["REQUEST_METHOD"];
    if($request_method == "GET"){
      $query_vars = $_GET;
    } elseif ($request_method == "POST"){
      $query_vars = $_POST;
    }
    reset($query_vars);
    $t = date("U");

    $file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t;
    $fp = fopen($file,"w");
    while (list ($key, $val) = each ($query_vars)) {
     fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\n");
     fputs($fp,"$val\n");
     fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\n");
     if ($key == "redirect") { $landing_page = $val;}
    }
    fclose($fp);
    if ($landing_page != ""){
header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
    } else {
header("Location: http://".$_SERVER["HTTP_HOST"]."/");
    }
?>

 

contact_sent.php

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="config/main.css" rel="stylesheet" type="text/css" />
<title>Message Sent</title>
</head>

<body>
<div id="contact_form_wrap">
<br />
<h1>Thank you, your message has been sent.</h1><br /><br />
<h2><a href="#" onclick="parent.Shadowbox.close();return false;"> Close Window </a></h2>
</div>
</body>
</html>

 

contact_error.php (key omitted)

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Message Error</title>
<link href="config/main.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="contact_form_wrap">
<form action="contact_mailer.php" method="post">
<input type="hidden" name="subject" value="Comment From Enderpanda.com" /> 
<input type="hidden" name="redirect" value="contact_sent.php" />
<label for="name">Name (optional)<br /></label>
<input name="name" type="text" id="name" size="50" maxlength="200" value="<?=$_POST['name']; ?>" /><br /><br />
<label for="email">Email (optional)<br /></label>
<input name="email" type="text" id="email" size="50" maxlength="200" value="<?=$_POST['email'] ?>" /><br /><br />
<label for="message_sub">Subject (optional)<br /></label>
<input name="message_sub" type="text" id="message_sub" size="50" maxlength="200" value="<?=$_POST['message_sub'] ?>" /><br><br>
<label for="message">Message<br/></label>
<span id="sprytextarea1">
<textarea name="message" cols="50" rows="6" id="message"><?=$_POST['message'] ?></textarea>
<span class="textareaRequiredMsg">You must include a message.</span></span><br /><br />
<?
if (isset($_GET['capresult']))
{
$capresult = $_GET['capresult'];
}
if ($capresult == "fail" )
{
$captfail = "reCAPTCHA code incorrect, please try again.<br><br>";
}
?>
<span class="red"><? echo $captfail; ?></span>
<? require_once('recaptchalib.php');
$publickey = "OMITTED"; 
echo recaptcha_get_html($publickey);
?><br>
<input type="submit" name="submit" value="Send Message"/>
</form>
<p><a href="#" onclick="parent.Shadowbox.close();return false;"> Close Window </a></p>
</div>
</body>
</html>

 

Link to comment
Share on other sites

Okay I rewrote the contact page into one form file and one processing file, hopefully this will be a lot simpler (please disregard the earlier code I posted)...  It actually works without the captcha, but with it the information still isn't being retained (after getting the captcha wrong).  I added a back button in the error message, which sometimes works (info still there) and sometimes not (info gone - seems to depend on the browser), as a temporary band-aid.

 

Anyway, without the captcha it seems to keep the data no problem (even though, because of the spry field, you would never really get an "error" since only the message field is required and you can't hit submit without that).  I think the reason that the information isn't being retained is because of this (from contact_mailer.php):

if (!$resp->is_valid) {
header("location:/../contact.php?captresult=fail");
die();
}

I don't think the information in the fields is being retained through that window switch (though it is successfully GET'ing the "captresult" variable) .

 

At least, that's what I think is going wrong...  Anyone have any idea?  Seems like I'm soo close hehe.

 

Thanks for any help you can give me!

 

contact.php

 

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Enderpanda</title>
<link href="config/main.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css" />
</head>
<? 
if (isset($_GET['captresult']))
{
$result = $_GET['captresult'];
}

if ($result == "fail" )
{
$fail_message = 'reCAPTCHA code incorrect, please try again.';
}

if ($result == "pass" )
{
$sucess_message = "Message sent.";
}

?>
<body>
<div id="contact_form_wrap">
<? if ($result == "pass" ) {
echo '<h1><span class="green">'.$sucess_message.'</span></h1><br />';
}

if ($result != "pass" ) {
?>
<form action="config/contact_mailer.php" method="post">
<input type="hidden" name="redirect" value="contact.php?captresult=pass" />
Name (optional)<br /><input name="name" type="text" size="50" maxlength="75" value="<? if (isset($_POST['name'])) echo $_POST['name']; ?>" /><br /><br />
Email (optional)<br /><input name="email" type="text" size="50" maxlength="200" value="<? if (isset($_POST['email'])) echo $_POST['email']; ?>" /><br /><br />
Subject (optional)<br /><input name="subject" type="text" size="50" maxlength="200" value="<? if (isset($_POST['subject'])) echo $_POST['subject']; ?>" /><br /><br />
Message <br />
<span id="sprytextarea1">
<label>
  <textarea name="message" id="message" cols="45" rows="5"><? if (isset($_POST['message'])) echo $_POST['message']; ?></textarea>
</label>
<span class="textareaRequiredMsg">Message is required.</span></span><br /><br />
<? if ($result == "fail" ) {
echo '<span class="red">'.$fail_message.'</span><br /><br />If your message got erased, try <a href = "javascript:history.back()">clicking here</a> to go back (also reload the reCAPTCHA).<br /><br />';
}

require_once('config/recaptchalib.php');
$publickey = "OMITTED"; 
echo recaptcha_get_html($publickey);
?><br />
<input name="submit" type="submit" value="Submit" />
<input name="submitted" type="hidden" value="TRUE" />
</form>
<? } ?>
<p><a href="#" onclick="parent.Shadowbox.close();return false;"> Close Window </a></p>
</div>
<script type="text/javascript">
<!--
var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1");
//-->
</script>
</body>
</html>

 

contact_mailer.php

 

<?php

/* reCAPTCHA script from http://inko9nito.wordpress.com/2007/12/12/installing-recaptcha-with-php/ */

require_once('recaptchalib.php');
$privatekey = "OMITTED";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
header("location:/../contact.php?captresult=fail");
die();
}

/* Removed for now, successful result handled by the redirect input in contact form

else if ($resp->is_valid) {
header("location:contact_sent.php");
}

*/

/* Default GoDaddy form script (from gdform.php on my server) */

$request_method = $_SERVER["REQUEST_METHOD"];
if($request_method == "GET"){
$query_vars = $_GET;
} elseif ($request_method == "POST"){
$query_vars = $_POST;
}
reset($query_vars);
$t = date("U");

$file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t;
$fp = fopen($file,"w");
while (list ($key, $val) = each ($query_vars)) {
fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\n");
fputs($fp,"$val\n");
fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\n");
if ($key == "redirect") { $landing_page = $val;}
}
fclose($fp);
if ($landing_page != ""){
header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
} else {
header("Location: http://".$_SERVER["HTTP_HOST"]."/");
}
?>

Link to comment
Share on other sites

As far as I can see, you are trying to gather POST values that don't exist anymore... after the header() redirect, I wouldn't expect them to still exist at all.  You could double check by doing a print_r($_POST) on the page you're redirecting to.

 

In the case where I want sticky forms, I usually use sessions to store the data.

Link to comment
Share on other sites

Ah okay, I thought the problem was something like that (since it worked without that part).  I just tried doing a print_r($_POST) and sure enough, nothing comes up but "Array()", whether the captcha is passed or failed... that info is definitely going somewhere though hehe, as I am getting an email with all the entered info, if the captcha is passed.

 

I'm pretty new to forms and using submitted data, I'll have to look into using sessions (been meaning to learn more about them anyway... mmm cookies).

 

Is there an easy way to fix this in the meantime, by chance?  Possibly changing the action taken with a non-valid response?  Passing and failing the captcha both result in being redirected back to the same page with a variable, but they're handled in slightly different ways... A fail is done by the window location change [linked above] (borrowed from a user script, source is in the comments) while a pass is an input variable that, from what I can tell, gets passed to the server cgi script by the code in contact_mailer (the default godaddy form script).

<input type="hidden" name="redirect" value="contact.php?captresult=pass" />

 

Thanks for your help, much appreciated!  :D

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.