-1

The problem I am having is regarding my PHP form.

I want the the html link (after php displays the error) to link back to the contact us page. At the moment it links to the form page. I am using HTML in PHP, to display the link. The reason is so that the user can go back to the contact us page instead of using the browser tools.

The PHP seems perfectly fine to me.

Here it is -

Line 14 is what I think is the problem.

<?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
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Welcome to Stack Overflow. Check the Stack Overflow's [help on asking questions](http://stackoverflow.com/help/asking) first, please. Focus on [What topics can I ask about here](http://stackoverflow.com/help/on-topic), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask), [How to ask a good question](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). – David Ferenczy Rogožan Sep 20 '18 at 10:27
  • Anyway, I don't understand what you want to achieve and what's not working currently. – David Ferenczy Rogožan Sep 20 '18 at 10:28
  • 1
    You should have been getting an error in your PHP error log! Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Sep 20 '18 at 10:33

1 Answers1

2

$link_address is not defined in the function.

Try changing to

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

In your function, reference it as

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

Daniel Williams
  • 2,195
  • 7
  • 32
  • 53