PHP的形式决定为形式,而不是指定的页面

时间:2018-09-20 10:22:50

标签: php html forms

我遇到的问题与我的PHP表单有关。

我希望html链接(在php显示错误之后)链接回到“联系我们”页面。目前,它链接到表单页面。我在PHP中使用HTML,以显示链接。原因是用户可以返回到“联系我们”页面,而不必使用浏览器工具。

PHP对我来说似乎很好。

这是-

第14行是我认为的问题。

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "test@gmail.com";
    $email_subject = "Contact Form Details";
    $link_address = 'http://cleaner-driveways.co.uk/dev/';

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        echo "<a href='$link_address'>Please Click here to retry the form</a>";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }                       



    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $message = $_POST['message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }


    if(strlen($message) < 2) {
        $error_message .= 'The Message you entered does not appear to be valid.<br />';
    }

    if(strlen($error_message) > 0) {
        died($error_message);
    }

    $email_message = "Form details below.\n\n";



    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";

    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
     'X-Mailer: PHP/' . phpversion();
      @mail($email_to, $email_subject, $email_message, $headers);  
      ?>

    <!-- include your own success html here -->

    <p>Thank you for contacting us. We will be in touch with you very soon.</p>
    <p>Please <a href="http://cleaner-driveways.co.uk/dev/">Click Here</a> to 
    return back to the Website.</p>
<?php
}
?>

1 个答案:

答案 0 :(得分:2)

$link_address未在函数中定义。

尝试更改为

define('link_address', 'http://cleaner-driveways.co.uk/dev/');

在您的函数中,将其引用为

echo "<a href='" . link_address . "'>Please Click here to retry the form</a>";