View Full Version : Newbie how to style php
quartz
01-05-2011, 04:54 PM
I have some php code that I would like to style and ideally stay on the same page and not go to another page. But I know nothing about php, it is for a captcha. I would like the code for a div and some way of naming the div so that I can style it, I want the div to remain on the same page when I tried to give the div an id, I developed an php error. Which frightened me, so I removed it.
if (!$resp->is_valid) {
// What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";
// What happens when CAPTCHA entered incorrectly ...
die ("<h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Successful captcha, so continue on with the email ...
}
Beverleyh
01-05-2011, 05:50 PM
You should be ok with;
if (!$resp->is_valid) {
// What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";
// What happens when CAPTCHA entered incorrectly ...
die ("<div id='message'><h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again.</div>" .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Successful captcha, so continue on with the email ...
}
Notice I used id='message' rather than id="message" - the quotes probably broke it.
You can also use id=\"message\" as long as you put a backslash before each quote to escape them :)
and the CSS;
#message {
background:yellow;
color:black;
padding:10px;
border:1px solid black;
}
bluewalrus
01-05-2011, 06:26 PM
He/she could also initiate the calling with single quotes rather than double quotes as the variable is being concocted anyway.
Could you be more clear about the "go to another page"? Is the address of the page changing or are you not getting all of the code you except to see? In what you've posted for code there is no reason for it to be loading another page so we'd need more code to troubleshoot that.
quartz
01-05-2011, 10:14 PM
Yes, the page is going to another url.
I thought that if I put it in an error div it would stay just below the original recaptcha. Thanks for the tip on single quotes, I will try that.
quartz
01-05-2011, 10:27 PM
I tried to do it again but alas, it threw up an error again.
The error is on the closing div line so it is in the wrong place.
if (!$resp->is_valid) {
// What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";
// What happens when CAPTCHA entered incorrectly ...
die ("<div id='captcha'><h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . </div>")");
} else {
// Successful captcha, so continue on with the email ...
}
bluewalrus
01-06-2011, 04:14 AM
Quotes in the wrong place, you can't have html in php. Please include your error message in future postings it will help identifying the problem.
if (!$resp->is_valid) {
// What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";
// What happens when CAPTCHA entered incorrectly ...
die ("<div id='captcha'><h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . "</div>)");
} else {
// Successful captcha, so continue on with the email ...
}
If you dont want it to redirect we will need to see the code, or you can try adding // in front of a line with header("location: $redirect"); that is a guess though, could break something...
quartz
01-06-2011, 01:50 PM
There is a lot of code to add, but I will do so here: This is the process.php which processes the form.
<?php
session_start();
require_once('recaptchalib.php');
$privatekey = "Removed";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
// Get variables from your form ...
$username = $_POST['name'];
$useremail = $_POST['email'];
// Who the form gets sent to ...
$recipient = "removed";
$subject = "Website Form";
// Where to go after the user submits the form ...
$redirect = "../thankyou.html";
if (!$resp->is_valid) {
// What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";
// What happens when CAPTCHA entered incorrectly ...
die ("<div id='captcha_error'>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")</div>");
} else {
// Successful captcha, so continue on with the email ...
}
$message = "
================================
Name: $username
Email: $useremail
-----------------------------------------
$fmessage
================================
";
$extraheaders = "From: $useremail\n\nReply-To: $useremail\n\n";
if (! mail($recipient, $subject, $message, $extraheaders))
echo "Mail did not send for some reason.";
header("Location: ".$redirect);
?>
This is the form iteself.
<form id="form" action="process.php" enctype="multipart/form-data" method="post" name="form_to_email">
<fieldset>
<legend>Enquiry Form</legend>
<p><label for="name"><span>Name</span></label><input id="name" name="name" size="35" type="text" /></p>
<p><label for="email"><span>Email</span></label><input id="email" name="email" size="35" type="text" /></p>
</fieldset><fieldset>
<p><label for="message"><span>Message</span></label><textarea id="message" cols="40" name="message" rows="8"></textarea></p>
</fieldset><fieldset>All fields Required </fieldset>
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'clean',
tabindex : 0
};
</script><noscript><p>Please enable javascript in your browser to post this form</p></noscript>
<?php
require_once('recaptchalib.php');
$publickey = "Removed";
echo recaptcha_get_html($publickey);
?>
<p><input id="submit" class="submit" name="submit" type="submit" value="Submit" /></p></form>
And this is the general recaptcha.lib
<?php
* Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
* AUTHORS:
* Mike Crawford
* Ben Maurer
/**
* The reCAPTCHA server URL's
*/
define("RECAPTCHA_API_SERVER", "http://www.google.com/recaptcha/api");
define("RECAPTCHA_API_SECURE_SERVER", "https://www.google.com/recaptcha/api");
define("RECAPTCHA_VERIFY_SERVER", "www.google.com");
/**
* Encodes the given data into a query string format
* @param $data - array of string elements to be encoded
* @return string - encoded request
*/
function _recaptcha_qsencode ($data) {
$req = "";
foreach ( $data as $key => $value )
$req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
// Cut the last '&'
$req=substr($req,0,strlen($req)-1);
return $req;
}
/**
* Submits an HTTP POST to a reCAPTCHA server
* @param string $host
* @param string $path
* @param array $data
* @param int port
* @return array response
*/
function _recaptcha_http_post($host, $path, $data, $port = 80) {
$req = _recaptcha_qsencode ($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
$response = '';
if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
die ('Could not open socket');
}
fwrite($fs, $http_request);
while ( !feof($fs) )
$response .= fgets($fs, 1160); // One TCP-IP packet
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response;
}
/**
* Gets the challenge HTML (javascript and non-javascript version).
* This is called from the browser, and the resulting reCAPTCHA HTML widget
* is embedded within the HTML form it was called from.
* @param string $pubkey A public key for reCAPTCHA
* @param string $error The error given by reCAPTCHA (optional, default is null)
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
* @return string - The HTML to be embedded in the user's form.
*/
function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
{
if ($pubkey == null || $pubkey == '') {
die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
}
if ($use_ssl) {
$server = RECAPTCHA_API_SECURE_SERVER;
} else {
$server = RECAPTCHA_API_SERVER;
}
$errorpart = "";
if ($error) {
$errorpart = "&error=" . $error;
}
return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
<noscript>
<iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>';
}
/**
* A ReCaptchaResponse is returned from recaptcha_check_answer()
*/
class ReCaptchaResponse {
var $is_valid;
var $error;
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
* @param string $privkey
* @param string $remoteip
* @param string $challenge
* @param string $response
* @param array $extra_params an array of extra variables to post to the server
* @return ReCaptchaResponse
*/
function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array())
{
if ($privkey == null || $privkey == '') {
die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
}
if ($remoteip == null || $remoteip == '') {
die ("For security reasons, you must pass the remote ip to reCAPTCHA");
}
//discard spam submissions
if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
$recaptcha_response = new ReCaptchaResponse();
$recaptcha_response->is_valid = false;
$recaptcha_response->error = 'incorrect-captcha-sol';
return $recaptcha_response;
}
$response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/verify",
array (
'privatekey' => $privkey,
'remoteip' => $remoteip,
'challenge' => $challenge,
'response' => $response
) + $extra_params
);
$answers = explode ("\n", $response [1]);
$recaptcha_response = new ReCaptchaResponse();
if (trim ($answers [0]) == 'true') {
$recaptcha_response->is_valid = true;
}
else {
$recaptcha_response->is_valid = false;
$recaptcha_response->error = $answers [1];
}
return $recaptcha_response;
}
/**
* gets a URL where the user can sign up for reCAPTCHA. If your application
* has a configuration page where you enter a key, you should provide a link
* using this function.
* @param string $domain The domain where the page is hosted
* @param string $appname The name of your application
*/
function recaptcha_get_signup_url ($domain = null, $appname = null) {
return "https://www.google.com/recaptcha/admin/create?" . _recaptcha_qsencode (array ('domains' => $domain, 'app' => $appname));
}
function _recaptcha_aes_pad($val) {
$block_size = 16;
$numpad = $block_size - (strlen ($val) % $block_size);
return str_pad($val, strlen ($val) + $numpad, chr($numpad));
}
//Mailhide and Credits Removed//
What I am trying to find out how to do, is that if there is an error with the recaptcha, I want the error displayed underneath the recaptcha, at the moment it goes to a new url. Been searching the web for days now, I cant write php code or pay anyone to write code for me. But I thought if I put the code in a div it should stay underneath the recaptcha.
bluewalrus
01-06-2011, 02:58 PM
Are you getting an error message still or just redirected?
The redirect is
header("Location: ".$redirect);
so make that
//header("Location: ".$redirect);
quartz
01-06-2011, 03:03 PM
Its OK I dont think you understand, sometimes I want the page to redirect to the thanks page. But if the code is wrong I just want the errror message to remain underneath the recaptcha box.
bluewalrus
01-06-2011, 03:40 PM
Oh, okay try this.
<?php
session_start();
require_once('recaptchalib.php');
$privatekey = "Removed";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
// Get variables from your form ...
$username = $_POST['name'];
$useremail = $_POST['email'];
// Who the form gets sent to ...
$recipient = "removed";
$subject = "Website Form";
// Where to go after the user submits the form ...
$redirect = "../thankyou.html";
if (!$resp->is_valid) {
// What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";
// What happens when CAPTCHA entered incorrectly ...
?>
<div id='captcha_error'>The reCAPTCHA wasn't entered correctly. Go back and try it again.<br />(reCAPTCHA said: <?php echo $resp->error ; ?>
<form id="form" action="process.php" enctype="multipart/form-data" method="post" name="form_to_email">
<fieldset>
<legend>Enquiry Form</legend>
<p><label for="name"><span>Name</span></label><input id="name" name="name" size="35" type="text" /></p>
<p><label for="email"><span>Email</span></label><input id="email" name="email" size="35" type="text" /></p>
</fieldset><fieldset>
<p><label for="message"><span>Message</span></label><textarea id="message" cols="40" name="message" rows="8"></textarea></p>
</fieldset><fieldset>All fields Required </fieldset>
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'clean',
tabindex : 0
};
</script><noscript><p>Please enable javascript in your browser to post this form</p></noscript>
<?php
require_once('recaptchalib.php');
$publickey = "Removed";
echo recaptcha_get_html($publickey);
?>
<p><input id="submit" class="submit" name="submit" type="submit" value="Submit" /></p></form>
<?php
exit();
} else {
// Successful captcha, so continue on with the email ...
}
$message = "
================================
Name: $username
Email: $useremail
-----------------------------------------
$fmessage
================================
";
$extraheaders = "From: $useremail\n\nReply-To: $useremail\n\n";
if (! mail($recipient, $subject, $message, $extraheaders))
echo "Mail did not send for some reason.";
header("Location: ".$redirect);
?>
quartz
01-07-2011, 01:19 AM
Thanks for your post, That produced the error but it did not show the recaptcha and the error should only show when there is an error in the recaptcha. Dont worry I will have to think of something else.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.