Captcha equation isn't displayed

I’ve downloaded a Contact Form and am trying to get it to work.
I took this code:

<?php include_once('ajaxContact/config.php'); ?>

<link href="css/contact.css" rel="stylesheet" type="text/css" /> <!-- AJAX Contact Form Stylesheet -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="js/jquery.jigowatt.js"></script><!-- AJAX Form Submit -->

<body>
    <div id="contact">
        <h1><?php echo TITLE; ?></h1>
        <p><?php echo DESCRIPTION; ?></p>
        <!--<form method="post" action="" name="contactform" id="contactform"> -->
        <fieldset  name="contactform" id="contactform">
            <legend>Please fill in the following form</legend>
            <div id="result"></div> <br>
            <label for="name" accesskey="U"><span class="required">*</span> Your Name</label>
            <input name="name" type="text" id="name" size="30" value="" />
            <br />
            <label for="email" accesskey="E"><span class="required">*</span> Email</label>
            <input name="email" type="text" id="email" size="30" value="" />
            <br />
            <label for="phone" accesskey="P"> Phone</label>
            <input name="phone" type="text" id="phone" size="30" value="" />
            <br />
            <label for="subject" accesskey="S"> Subject </label>
            <input name="subject" type="text" id="subject" size="30" value="" />

            <br />
            <label for="comments" accesskey="C"><span class="required">*</span> Your comments</label>
            <textarea name="comments" cols="40" rows="3" id="comments" style="width: 350px;"></textarea>
            <p><span class="required">*</span> Are you human?</p>
            <?php
            $a = rand(0, 9);
            $b = rand(0, 9);
            $c = $a + $b;
            ?>
            <label for="verify" accesskey="V">&nbsp;&nbsp;&nbsp; <?php echo $a ?> + <?php echo $b; ?> =</label>
            <input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
            <input type="hidden" name="cap" value="<?php echo $c; ?>" />
            <button class="submit_btn" id="submit_btn">Submit</button>
        </fieldset>
        <!-- </form> -->
    </div>
</body>

I put this in the index.php file:

`<?php include_once('ajaxContact/config.php'); ?>

I put this in the < head > section:

<link href="css/contact.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="js/jquery.jigowatt.js"></script>

I put this in the html page:

    <h1><?php echo TITLE; ?></h1>
    <p><?php echo DESCRIPTION; ?></p>
    <!--<form method="post" action="" name="contactform" id="contactform"> -->
    <fieldset  name="contactform" id="contactform">
        <legend>Please fill in the following form</legend>

        <div id="result"></div> <br>

        <label for="name" accesskey="U"><span class="required">*</span> Your Name</label>
        <input name="name" type="text" id="name" size="30" value="" />
        <br />
        <label for="email" accesskey="E"><span class="required">*</span> Email</label>
        <input name="email" type="text" id="email" size="30" value="" />
        <br />
        <label for="phone" accesskey="P"> Phone</label>
        <input name="phone" type="text" id="phone" size="30" value="" />
        <br />
        <label for="subject" accesskey="S"> Subject </label>
        <input name="subject" type="text" id="subject" size="30" value="" />
        <br />
        <label for="comments" accesskey="C"><span class="required">*</span> Your comments</label>
        <textarea name="comments" cols="40" rows="3" id="comments" style="width: 350px;"></textarea>
        <p><span class="required">*</span> Are you human?</p>
        <?php
        $a = rand(0, 9);
        $b = rand(0, 9);
        $c = $a + $b;
        ?>

        <label for="verify" accesskey="V">&nbsp;&nbsp;&nbsp; <?php echo $a ?> + <?php echo $b; ?> =</label>
        <input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
        <input type="hidden" name="cap" value="<?php echo $c; ?>" />
        <button class="submit_btn" id="submit_btn">Submit</button>
    </fieldset>
    <!-- </form> -->
</div>

But the captcha math equation doesn’t appear (see attached image).
Any suggestions on how to get that to appear, or what I may be missing, will be appreciated

.

Well, there’s a missing ; after where you try to echo $a, but I can’t remember whether that matters if you’re closing the php immediately afterwards. If you view the html source after the page is drawn, is the value of $c correct in the hidden field?

Also, if you put that code in the html page, is your server actually processing the PHP? At one point (and it may still be the case), for speed reasons, servers would only scan pages that the delivery for any kind of script language if they were configured to do so, and would make the decision based on the page name - so stuff called .php is sent via the php processor, stuff called .asp via the ASP processor, and so on. So if this is in a file called something.html, it might just ignore any php stuff.

Doing captcha with JavaScript is never going to work if a user has JavaScript disabled.

1 Like

A post was split to a new topic: Captcha not displaying

Thanks for your replies.
Regarding “Doing captcha with JavaScript is never going to work if a user has JavaScript disabled”, so what is the solution? Can you recommend a Contact Form where the capthcha is a math equation, and after the Form is submitted, the ‘success’ mesage appears next to the Form, and the ‘Submitter’ isn’t sent to another page?

The simplest way is to validate the captcha code when you validate all the other inputs - assuming you are validating the inputs. This can all be done in PHP.

Thanks for your reply
Can you recommend a Contact Form that exists already that accomplishes these things?

When I run your code the numbers do display.

The errors I get are for “undefined constants” from these lines:-

        <h1><?php echo TITLE; ?></h1>
        <p><?php echo DESCRIPTION; ?></p>

I’m guessing they should be variables, and defined somewhere.
And of course there are validation errors, from the commented out form tags, lack of head tags, etc…

I tend to take one I’ve done before and change it as necessary. I have one with the type of captcha you’re using but cannot find it right now. I will try to hunt it down.

This is the code I use as the basis for my contact forms. I haven’t been able to find the captcha part so I will have to add that later when I get time. I don’t know if this is of any help. I realise it may raise more questions than it answers, and others might find holes in my code and want to make changes to it, too.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Contact us</title>
<meta name="description" content="Contact us">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="form.css" rel="stylesheet">
</head>
<body>
<div>
  <h1>Contact us</h1>
  <?php
  $missing = false;
  if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    // form has been submitted
    $emailAddr = isset($_POST['emailAddr']) ? filter_var($_POST['emailAddr'], FILTER_VALIDATE_EMAIL) : '';
    $yourName  = isset($_POST['yourName'])  ? preg_replace('/[^A-za-z0-9 \'-]+/', '', $_POST['yourName']) : '';
    $message   = isset($_POST['message'])   ? $_POST['message'] : '';
    $botcatch  = isset($_POST['botcatch'])  ? $_POST['botcatch'] : '';
    $captcha   = isset($_POST['captcha'])   ? $_POST['captcha'] : '';
    $captSum   = isset($_POST['captSum'])   ? $_POST['captSum'] : '';

    if ( $botcatch !== '' ) die();     // not a very grown-up way to handle it, but it does the job
    $noName    = ($yourName  == '') ? true : false;
    $noEmail   = ($emailAddr == '') ? true : false;
    $noMessage = ($message   == '') ? true : false;
    $noCaptcha = ($captcha  !== $captSum) ? true : false;
    $missing   = $noName || $noEmail || $noMessage || $noCaptcha;
  }

  if ( ($_SERVER['REQUEST_METHOD'] == 'POST') && !$missing ) :
    // form has been submitted and validates
    $to   = 'myemail@mydomain.com';
    $body = 'Message from: ' . $yourName . "\n"
      . 'Email: ' . $emailAddr . "\n"
      . $message . "\n\n"
      . 'IP address: ' . $_SERVER['REMOTE_ADDR'] . "\n"
      . 'Browser: ' . $_SERVER['HTTP_USER_AGENT'] . "\n";

    // send email
    require 'PHPMailerAutoload.php';
    $mail = new PHPMailer;

    #$mail->SMTPDebug = 3; // Enable verbose debug output
    // will need to change some of the following settings
    $mail->isSMTP();
    $mail->Host       = 'mail.domain.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'ciao@domain.eu';
    $mail->Password   = 'password';
    #$mail->SMTPSecure = 'tls';
    $mail->Port       = 2525;

    $mail->setFrom('no-reply@domain.eu', 'Web enquiry');
    $mail->addAddress($to);
    $mail->addReplyTo($emailAddr, $yourName);

    $mail->isHTML(false);

    $mail->Subject = 'Website enquiry';
    $mail->Body    = $body;

    if ( !$mail->send() ) {
      echo '<p class="error">Sorry, your message could not be sent. Mailer Error: ', $mail->ErrorInfo, '</p>', "\n";
    } else {
      echo '<p>Your message has been sent and will be attended to as soon as possible.</p>', "\n";
      echo '<p>&nbsp;</p>', "\n";
      echo '<p>&nbsp;</p>', "\n";
    }
  else :
    if ( $missing ) {
      // missing or erroneous entries
      echo '<p class="error">You have not completed the form correctly. Please fill in all fields and press Send when complete.</p>', "\n";
    } else {
      // show form
      echo '<p>If you would like to get in touch, just complete the form below.</p>', "\n";
    }
    $noName    = isset($noName)    ? $noName    : false;
    $noEmail   = isset($noEmail)   ? $noEmail   : false;
    $noMessage = isset($noMessage) ? $noMessage : false;
    $noCaptcha = isset($noCaptcha) ? $noCaptcha : false;
  ?>
  <form id="form" class="form" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
    <div class="field<?php if ( $noName ) echo ' error'; ?>">
      <label for="yourName">Your name *</label>
      <input type="text" value="<?php if (isset($yourName)) echo $yourName; ?>" name="yourName" placeholder="Your name" required>
    </div>
    <div class="field<?php if ( $noEmail ) echo ' error'; ?>">
      <label for="emailAddr">Email address *</label>
      <input type="email" value="<?php if (isset($emailAddr)) echo $emailAddr; ?>" name="emailAddr" placeholder="Your email address" required>
    </div>
    <div class="field<?php if ( $noMessage ) echo ' error'; ?>">
      <label for="message">Message *</label>
      <textarea name="message" placeholder="Your message (up to 500 characters)" required maxlength="500"><?php if (isset($message)) echo $message; ?></textarea>
    </div>
    <?php
    $n1 = rand(1, 8);
    $n2 = rand(1, 8);
    $captSum = $n1 + $n2;
    ?>
    <div class="field<?php if ( $noCaptcha ) echo ' error'; ?>">
      <label for="emailAddr"><?php echo 'What is ',$n1,' + ',$n2; ?> *</label>
      <input type="text" value="<?php if (isset($captcha)) echo $captcha; ?>" name="captcha" placeholder="Please show you're human" required>
    </div>
    <?php
    // Simple way to fool bots #botcatch needs to be display: none;
    ?>
    <div class="field botcatch">
      <label for="botcatch">Leave empty</label>
      <input type="text" name="botcatch">
    </div>
    <div class="field">
      <input name="send" type="submit" value="Send">
    </div>
    <input type="hidden" name="captSum" value="<?php echo $captSum; ?>">
  </form>
  <?php
  endif;
  ?>
</div>
</body>
</html>

EDIT: Rather than post another lot of code I have edited the code above to include a simple captcha.

Thanks

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.