Jump to content

Archived

This topic is now archived and is closed to further replies.

simcoweb

form script doesn't show any errors but doesn't work either

Recommended Posts

I have this form script (below) that doesn't produce any errors but does not display any validation errors nor sends the data nor displays the confirmation message. Basically it displays the header/footer and the rest is blank. I don't see the point where it's breaking down.

[code]<?php

// do the security check
if (isset($_POST['submit'])) {
// clean and check form inputs including the secure image code
    $name = trim(strip_tags($_POST['name']));
    $location = trim(strip_tags($_POST['location']));
    $email = trim(strip_tags($_POST['email']));
    $phone = trim(strip_tags($_POST['phone']));
    $best_time = trim(strip_tags($_POST['best_time']));
    $details = trim(strip_tags($_POST['details']));
    $referred_by = trim(strip_tags($_POST['ref_by']));
    $secure = strtoupper(trim(strip_tags($_POST['secure'])));
    $match = $_SESSION['secure']; // the code on the image
}
$err = "";
include 'header.php';
// input error checking
    if ($name=="") {
        $err.= "Please provide your name. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }
    if ($location=="") {
  $err.= "Please provide your city of residence. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
}
if (!$email) {
        $err.= "Please provide your email address. <a href=\"javascript: history.go(-1)\">Back</a><br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err.= $email. " is not a valid email address. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
        }
    }
    if ($phone=="") {
        $err.= "Please provide a phone number. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }
    if ($best_time=="") {
  $err.= "Please provide the best time to contact you. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
}
    if (!$secure) {
        $err.= "No security code entered. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err.= "Security code mismatch. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }
    if ($err=="") {

// set mail variables
$to="admin@plateauprofessionals.com";
$subject="Plateau Professionals Inquiry";
$mailContent="--------CONTACT--------\n"
            ."Name: ".$name."\n"
            ."Location: ".$location."\n"
            ."E-mail: ".$email."\n\n--------PHONE--------\n"
            ."Phone: ".$phone."\n"
            ."Best time to call: ".$best_time."\n\n--------Details--------\n"
            ."Details: ".$details."\n"
."Referred by: ".$referred_by."\n";
// send the results
mail($to, $subject, $mailContent, "From:$email") or print "Could not send email";

echo "<h2>Message Sent</h2><br>\n";
echo "<font class='bodytext'><blockquote>Your message has been sent. A representative will respond as soon as possible. Thank you for contacting Plateau Professionals!</blockquote><br>\n";
}
include 'footer.php';
?>[/code]

The form is at [url=http://www.plateauprofessionals.com/gencontact.php]www.plateauprofessionals.com/gencontact.php[/url]

It's fairly basic with the exception of the CAPTCHA image and has action="sendform.php" which is the script code shown.

Share this post


Link to post
Share on other sites
Debug your $err variable. It will only perform the mailing part of your script if $err is empty. Perhaps it isn't?

[code]
<?php

$err = "";
include 'header.php';
// input error checking
    if ($name=="") {
        $err.= "Please provide your name. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }
    if ($location=="") {
  $err.= "Please provide your city of residence. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
}
if (!$email) {
        $err.= "Please provide your email address. <a href=\"javascript: history.go(-1)\">Back</a><br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err.= $email. " is not a valid email address. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
        }
    }
    if ($phone=="") {
        $err.= "Please provide a phone number. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }
    if ($best_time=="") {
  $err.= "Please provide the best time to contact you. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
}
    if (!$secure) {
        $err.= "No security code entered. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err.= "Security code mismatch. <a href=\"javascript: history.go(-1)\">Back</a><br/>";
    }

echo $err;
exit(); //this will exit the script allowing you to see the value of $err

?>
[/code]

Share this post


Link to post
Share on other sites
heh...silly me. I forgot to put an echo statement to show the various errors. Guess I thought they'd somehow magically appear.  ;D

Only challenge left is the security image. For some reason it's not matching it and continually says the security code entry doesn't match. I KNOW i'm typing them in correctly. See anything in that segment that stands out?

Share this post


Link to post
Share on other sites
first thing that stands out to me is this:

$match = $_SESSION['secure']; // the code on the image

i don't see a session_start(); at the beginning of your script, so $match is not being set to anything.

Share this post


Link to post
Share on other sites
Funny you should mention that as i'd just put that in there before receiving your post email. I've added it but it still provides a mismatch error.

My revised code looks like this now:

[code]<?php
session_start();
// do the security check
if (isset($_POST['submit'])) {
// clean and check form inputs including the secure image code
    $name = trim(strip_tags($_POST['name']));
    $location = trim(strip_tags($_POST['location']));
    $email = trim(strip_tags($_POST['email']));
    $phone = trim(strip_tags($_POST['phone']));
    $best_time = trim(strip_tags($_POST['best_time']));
    $details = trim(strip_tags($_POST['details']));
    $referred_by = trim(strip_tags($_POST['ref_by']));
    $secure = strtoupper(trim(strip_tags($_POST['secure'])));
    $match = $_SESSION['secure']; // the code on the image
}
$err = "";
include 'header.php';
// input error checking
    if ($name=="") {
        $err.= "Please provide your name. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br/>";
    }
    if ($location=="") {
  $err.= "Please provide your city of residence. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br/>";
}
if (!$email) {
        $err.= "Please provide your email address. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err.= $email. " is not a valid email address. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br/>";
        }
    }
    if ($phone=="") {
        $err.= "Please provide a phone number. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br/>";
    }
    if ($best_time=="") {
  $err.= "Please provide the best time to contact you. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br/>";
}
    if (!$secure) {
        $err.= "No security code entered. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err.= "Security code mismatch. <a class='body' href=\"javascript: history.go(-1)\">Back To Form</a><br/>";
    }
   
//echo $err;
//exit;
      if ($err!='') {
    echo "<center>";
    echo "<strong>Form Error(s)</strong><br/>";
    echo "<font class='bodytext'>The following fields were not completed correctly. Please return to the form and complete them accordingly.<br>\n";
    echo "<font color='#cc3300'>". nl2br($err). "</font><br/>\n";
    echo "</center>";
} else {

// set mail variables
$to="admin@plateauprofessionals.com";
$subject="Plateau Professionals Inquiry";
$mailContent="--------CONTACT--------\n"
            ."Name: ".$name."\n"
            ."Location: ".$location."\n"
            ."E-mail: ".$email."\n\n--------PHONE--------\n"
            ."Phone: ".$phone."\n"
            ."Best time to call: ".$best_time."\n\n--------Details--------\n"
            ."Details: ".$details."\n"
."Referred by: ".$referred_by."\n";
// send the results
mail($to, $subject, $mailContent, "From:$email") or print "Could not send email";

echo "<h2>Message Sent</h2><br>\n";
echo "<font class='bodytext'><blockquote>Your message has been sent. A representative will respond as soon as possible. Thank you for contacting Plateau Professionals!</blockquote><br>\n";
}
include 'footer.php';
?>[/code]

The form input for the security image is basic stuff:

[quote]<td><img src="captcha_image.php" alt="security image" border="0"/></td>
</tr>
            <tr>
            <td><p align="right"><font class="bodytext">Verification:</font><br><font size="1">Enter the code shown.</font> </p></td>
            <td><input type="text" name="secure" maxlength="8"></td>[/quote]

Thanks for your help!

Share this post


Link to post
Share on other sites
Ok, got it to work by adding this to the sessions:

[code]<?php
session_start();
session_register("secure");
?>[/code]

Which then matches the variable match for $_SESSION in the validation process. Form produced no errors and I received the email from the contact form.

Thanks again!

Share this post


Link to post
Share on other sites
It echoes nothing if I remove the session lines.

Actually i've removed the session_register lines and just having session_start(); on both the form page and the parsing script makes it work.

Share this post


Link to post
Share on other sites

×

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.