Jump to content

mkultron

Members
  • Posts

    16
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

mkultron's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi guys, I'm having problems integrating Recaptcha into an existing form. I want it all to process on the same page. I took a look on Google and found a pre-existing script which does what I want but I'm still having trouble integrating it with what I already have, form fields and the recaptcha validate seperately. Here's my script so far: before the <html> tag: <?php session_start(); ?> <?php // DEMO to use reCAPTCHA library on a form // // courtesy Spectrum Nashville // http://www.spectrum-nashville.com // // provide the Public Key and Private Key for your account here: define( API_PUBLIC_KEY, '6LeNt84SAAAAAAH0Et-eJpNeuYO-kRXgrpcXML36' ); define( API_PRIVATE_KEY, '6LeNt84SAAAAAMlGWZUEHqFHncRyvaYbI5YdE8BY' ); // // once the keys have been provided above, this demo should work without any further changes to the code below // // include the recaptcha library file here // require_once('recaptchalib.php'); // // the $validated variable will switch to true in the code below if a valid CAPTCHA is submitted (see code below). // do not change this. // $validated = false; ?> <?php //If the form is submitted if(isset($_POST['submit'])) { //Check to make sure that the name field is not empty if(trim($_POST['name']) == '') { $hasError = true; } else { $name = trim($_POST['name']); } //Check to make sure sure that a valid email address is submitted if(trim($_POST['email']) == '') { $hasError = true; } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { $hasError = true; } else { $email = trim($_POST['email']); } //Check to make sure comments were entered if(trim($_POST['comment']) == '') { $hasError = true; } else { if(function_exists('stripslashes')) { $comment = stripslashes(trim($_POST['comment'])); } else { $comment = trim($_POST['comment']); } } //If there is no error, send the email if(!isset($hasError)) { $emailTo = 'eevansrange@gmail.com'; //Put your own email address here $subject = "Feedback from TEST FORM"; $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comment"; // $body = "Enquiry type: $enquiry \n\nName: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments"; $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; mail($emailTo, $subject, $body, $headers); $emailSent = true; } } ?> Within body tag: <!-- RECAPTCHA START --> <script> // example of using JavaScript variable to provide "theme" control // of the reCAPTCHA element. This entire <script> block is optional. var RecaptchaOptions = { theme : 'blackglass', tabindex : 2 }; </script> <!-- RECAPTCHA END --> <section id="form"> <h2 class="blue">CONTACT ME</h2> <div id="form-holder"> <p class="form-instructions">Need some work doing? Have a comment? Post them below and I'll get back to you asap!</p> <?php if(isset($hasError)) { //If errors are found ?> <p class="errors">Please check you've filled out the fields correctly.</p> <?php } ?> <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?> <p class="success">Thank you <strong><?php echo $name;?></strong>, your email has been sent.</p> <?php } ?> <?php // see the form below and look for the hidden input named 'validate' to see how this works. This is a nice flag to // indicate to us that the validation should be attempted. The first time a user browses to this page, there will be // no 'validate' variable in the $_POST[] array, so no validation is attempted and no error message will be generated. // // do not change any of this code. // if( $_POST['validate'] === 'yes' ) { $response = recaptcha_check_answer( API_PRIVATE_KEY, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field'] ); if( ! $response->is_valid ) { // // captcha failed -- display the error message // // change this to whatever message you want to display. you could even perform other form validations // and display messages about other required fields, etc., here if you want. // use the CSS in the <head> section above to determine how your error message "box" will appear. // echo '<div id="recaptcha_error_box">The reCAPTCHA failed with this message: '.$response->error.'<br />Please try again.</div>'; // by default now, let the flow-of-control fall back into the <form> below so the user can try again } else { // set $validated to true so we know later in our document not to show the form again // don't change this. $validated = true; // // YOUR CODE HERE.... // // at this point, th form was submitted with valid reCAPTCHA, so go do whatever you wanted to do. // for the demo, we'll just echo back the values from the form. // // you could also send an email message, add or update database records, etc. // ?> <?php } /* end if( ! is_valid ) */ } /* end if($_POST['validate']==='yes') */ ?> <?php if( ! $validated ) { ?> <form class="cmxform" id="commentForm" method="post" border="0" action="<?php echo $_SERVER['PHP_SELF']; ?>#form"><!-- added #form so on submit the page anchors to the form div rather than taking you back to the top of the page --> <?php require_once('recaptchalib.php'); $publickey = "6LeNt84SAAAAAAH0Et-eJpNeuYO-kRXgrpcXML36"; // you got this from the signup page echo recaptcha_get_html($publickey); ?> <!--<input type="submit" />--> <br /> <label class="required" for="name" title="Enter your name"><span>Name</span><input type="text" id="name" name="name" size="50" class="required" value="<?php echo stripslashes(htmlentities($_POST['your_name'])); ?>" /></label> <label class="required" for="email" title="Enter your email"><span>Email</span><input type="text" id="email" name="email" size="50" class="required" value="<?php echo stripslashes(htmlentities($_POST['your_email'])); ?>" /></label> <label class="required" for="comment" title="Enter your comments" size="50" value="<?php echo stripslashes(htmlentities($_POST['comment'])); ?>"> <span>Comments</span><textarea name="comment" rows="5" cols="50" class="required"></textarea> </label> <!-- CAPTCHA START --> <?php echo recaptcha_get_html(API_PUBLIC_KEY); ?> <!-- CAPTCHA END --> <input type="hidden" name="validate" value="yes" /> <!--<input type="submit" value="Try It" />--> <label for="submit" class="nocontent"><input class="submit" type="submit" name="submit" value="Try It" title="Send form" />Items marked <img src="images/required-2.png" width="12" height="12" alt="required field" /> are required fields</label> <!-- need to include the name tag =submit above for the form to process and send the email --> <!-- class="nocontent" - commmented out of above label --> </form> <?php } /* end if( ! $validated ) */ ?> </div> </section> <!-- client-side validation --> <script type="text/javascript"> jQuery(function(){ // Grab each form element jQuery("label[title]").each(function(){ jQuery(this).append("<div class=\"infopop\">"); titletext = jQuery(this).attr("title"); jQuery(this).removeAttr("title"); jQuery(".infopop",this).css({opacity:0}).html(titletext); jQuery("input",this).focus(function(){ // Mouseover doFocus(this); }).blur(function(){ // MouseOut doBlur(this); }); /* ADDED TO show errors for textarea */ jQuery("textarea",this).focus(function(){ // Mouseover doFocus(this); }).blur(function(){ // MouseOut doBlur(this); }); }); }); function doFocus(obj) { jQuery(obj).addClass("active").parents("label").addClass("active").find(".infopop").animate({opacity:1,left:492},500); } function doBlur(obj) { if (validate(obj)) { isGood(obj); } } function reportErr(obj, message) { jQuery(obj).addClass("error").parents("label").removeClass("isgood").addClass("required").addClass("error").find(".infopop").html(message).addClass("errorpop").animate({opacity:1,left:492},500); } function isGood(obj) { jQuery(obj).removeClass("error").removeClass("active").parents("label").addClass("isgood").removeClass("error").removeClass("active").find(".infopop").removeClass("errorpop").animate({opacity:0,left:513},500); } function validate(obj) { mask = jQuery.extend({textfieldmask: /^[a-z\.\s-]{5,}$/i,emailmask: /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i,commentsboxmask: /^[a-z\.\s-]{5,}$/i}); errmsg = jQuery.extend({textfielderr:"5 or more letters",emailerr:"Invalid address",matcherr: "Must match",commenterr: "5 or more letters"}); var masktouse = null; var mustmatch = null; switch(obj.name) { case "name": masktouse="textfieldmask"; errtouse="textfielderr"; break; case "email": masktouse="emailmask"; errtouse="emailerr"; break; case "comment": masktouse="commentsboxmask"; errtouse="commenterr"; break; } // Check that the element is a required field before validating against it. if(jQuery(obj).parents("label").hasClass("required") && masktouse) { // Set up a quick way of accessing the object we're validating pointer = jQuery(obj); // Test the value of the field against the Regular Expression if (mask[masktouse].test(pointer.val())) { // The field validated successfully! // Check to see if the field needs to match another field in the form if (mustmatch) { // It does need to match, so grab the object it needs to match matchobj = jQuery("#"+mustmatch); if (matchobj.val()!='' && matchobj.val()!=pointer.val()) { // The fields don't match, so report an error on both of them reportErr(obj,errmsg["matcherr"]); reportErr(matchobj,errmsg["matcherr"]); } else { // Either the fields match, or the other field hasn't been completed yet // If the other field has been completed, call the isGood function to clear any error message showing if (matchobj.val()!='') { isGood(matchobj);} return true; } } else { // No match is required, so return true - validation passed! return true; } } else { // The field failed to validate against the Regular Expression reportErr(obj,errmsg[errtouse]); return false; } } else { // This isn't a required field, so we won't validate it against anything return true; } } </script> Any help would be much appreciated MK
  2. OK, thanks. I tried this and it posts the first result once and the second one over and over (btw I'm pulling more values out of the database using * but I've left them out for the purpose of this post). $result = mysql_query("SELECT *, DATE_FORMAT(mydate, '%a %b %e %Y %H:%i') AS mydate FROM stuff.eventstest ORDER BY mydate ASC") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row['mydate']; }
  3. Oh OK, could you tell me how I'd go about formatting the SELECT statement? $result = mysql_query("SELECT * FROM stuff.test ORDER BY mydate ASC") I take it I can put a "WHERE" following ASC and then format "jS F Y, g:i a". Many thanks MK
  4. Sorry I actually need it set out like this (with the line you replaced) as it's the only way I seem to be able to format the date the way I want it without it setting everythig to today's date, however when I use the line you took out it produces an infinite loop. Any idea how I can avoid this while still retaining the correct formatted date? while($row = mysql_fetch_array( $result )) { $mydate = mysql_result($result, $i, 'mydate'); $dt = new DateTime($mydate); echo '<td>' . $dt->format("jS F Y, g:i a") . '</td>'; } Thanks MK
  5. Hi guys, I'm having a bit of bother running this while loop, it's infinite. Could you give me some tips on getting it fixed please? while($row = mysql_fetch_array( $result )) { $stuffandthings = mysql_result($result, $i, 'stuffandthings'); echo '<td>' . $row['stuffandthings'] . '</td>'; } Thanks MK
  6. Cool, I've edited as suggested: <?php // connect to the database include('database-connect.php'); $result = mysql_query("SELECT * FROM stuff.eventstest ORDER BY eventdate ASC"); $eventdate = mysql_result($result, $i, '$eventdate'); echo mysql_result($result, $i, 'eventvenue'); echo mysql_result($result, $i, 'eventdate'); ?> And the value I return is "2012-02-08 10:57:52" (what I wanted, but obviously unformatted). Thanks MK
  7. Thanks kickstart, I now have the following set up, but still with only the venue name and today's date: include('database-connect.php'); $result = mysql_query("SELECT * FROM stuff.eventstest ORDER BY eventdate ASC"); $eventdate = mysql_result($result, $i, '$eventdate'); echo "<br />"; echo mysql_result($result, $i, 'eventvenue'); echo ", "; $dt = new DateTime($eventdate); echo $dt->format("jS F Y"); echo "<br />";
  8. $eventdate should be the eventdate field in from my database table, I've posted all the code I have right now above, do I need to do something like: $eventdate = $row["eventdate"]; Thanks in advance MK
  9. It's not anymore, it's just stored as a datetime. This should be such a simple thing but I can't seem to find a solution anywhere.
  10. I changed it to a datetime and it's just giving today's date.
  11. Hi guys, I'm putting together a small event system where I want the user to add his own date and time into a textfield (I'll probably make this a series of drop-downs/a date picker later). This is then stored as a timestamp - "0000-00-00 00:00:00" which displays fine until I try to echo it out as a UK date in this format - jS F Y, which just gives today's date but not the inputted date. Here's the code I have right now: $result = mysql_query("SELECT * FROM stuff.events ORDER BY eventdate ASC"); echo "<br />"; echo mysql_result($result, $i, 'eventvenue'); echo ", "; $dt = new DateTime($eventdate); echo $dt->format("jS F Y"); In my mysql table eventdate is set up as follows: field - eventdate type - timestamp length/values - blank default - current_timestamp collation - blank attributes - on update CURRENT_TIMESTAMP null - blank auto_increment - blank Any help as to why this could be happening would be much appreciated, thanks.
  12. Hi guys, I'm getting a bit tongue-tied trying to implement some next and previous link logic for my pagination (see lines under /* next and prev links */ comment). I've tried several alternatives but I can't seem to simply go forwards by one page through the pagination or back one. If you could give me some pointers I'd be really grateful. Thanks. MK $eventdate = mktime(0,0,0,date("m"),date("d")+1,date("Y")); // to get total pages / last page $total_results = mysql_num_rows($result); $total_pages = ceil($total_results / $per_page); // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1) if (isset($_GET['page']) && is_numeric($_GET['page'])) { $show_page = $_GET['page']; // make sure the $show_page value is valid if ($show_page > 0 && $show_page <= $total_pages) { $start = ($show_page -1) * $per_page; $end = $start + $per_page; } else { // error - show first set of results $start = 0; $end = $per_page; } } else { // if page isn't set, show first set of results $start = 0; $end = $per_page; } $mysqldate = date( 'd-m-Y H:i:s', $eventdate ); $eventdate = strtotime( $mysqldate ); // loop through results of database query, displaying them in the table for ($i = $start; $i < $end; $i++) { // make sure that PHP doesn't try to show results that don't exist if ($i == $total_results) { break; } // echo out the contents of each row into a table echo "<br />"; /*echo mysql_result($result, $i, 'eventid');*/ echo mysql_result($result, $i, 'eventvenue'); /*echo " "; echo mysql_result($result, $i, 'eventdate');*/ echo ", "; echo mysql_result($result, $i, 'eventdate'); //echo date("Y/m/d"); echo "<br />"; } echo "</div><p class='pagination'>"; echo "<a href='events3.php?page=$i=1'>« first</a> "; for ($i = 1; $i <= $total_pages; $i++) { echo "<a href='events3.php?page=$i'>$i</a> "; } echo "<a href='events3.php?page=$total_pages'>last »</a></p>"; /* next and prev links */ $k = $page-1; if ($i > 1) { echo "<a href='{$_SERVER['PHP_SELF']}?page=$k'>Previous</a>"; } if ($i < $total_pages) { echo "NEXT"; }
  13. Ah my bad, it is actually stored just as a "date".
  14. Thanks kickstart - Basically what I'm doing is adding the data via three differnet drop downs, one for date, day and year, joining these inputs together and inserting into the database as a timestamp. I'm guessing this is probably not the best way to do things. The timestamp is returning OK when I echo my select statement but it's obviously in the US format. Ideally I'd do something like "12th Dec 2011" though. Am I best to store the input as a VARCHAR or something to achieve this? I've tried to do as you suggested to draw my values out using the date_format like so: $result = mysql_query("SELECT eventvenue, DATE_FORMAT(eventdate, '%D %M %Y') FROM mydatabase.events"); but I get an error like this "Warning: mysql_result() [function.mysql-result]: eventdate not found in MySQL result index 3..." Any ideas? Thanks MK
×
×
  • 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.