• Hello everyone,

    I am trying to incorporate a HTML contact form into a site, in place of simple email links.

    Building the form is fine; the problem I am having is with calling the PHP function so that I get an email each time the form is completed, and the user gets a ‘Thanks, your message has been sent!’ or similar.

    I have added the HTML to the page I want it on, and uploaded the corresponding PHP, but each time you click ‘submit’, I get a 404. I suspect it is because it can’t find the designated PHP file – I am unsure if i am uploading this in the right place! I have tried a number of different places on my FTP – can someone please advise on where this should go?

    On clicking submit, the browser tries to redirect to the .php file, as an extension of the web page. ie: website/page/mailform.php

    Any help greatly appreciated!

    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi rjpinney, the 404 file not found error message you’re receiving means that you are submitting the form to a file that does not exist (your mailform.php) script. Most likely the file exists, but you are not putting in the URL correctly.

    It may be easier to troubleshoot if we can actually see the error in action. Do you mind posting a link to the url of the page with the issue so we can test further?

    Thread Starter rjpinney

    (@rjpinney)

    Hi Brad,

    Thanks for the reply.

    I’ve taken the script down so can’t point to a live example im afraid. I think the issue is with where the browser is looking for the PHP script, as you said – do you know exactly where I should upload this file? (ie: under the particular theme on FTP etc?)

    I justed wanted to mention that there are plugins that make it very easy to setup a contact form, exactly like what you’re doing right now. I’m assuming that you prefer to write code yourself and that’s why you’re not using a plugin. If by chance you wanted to explore the option of using a plugin, you may want to review:

    Contact Form 7

    do you know exactly where I should upload this file?

    Honestly, you can put the file anywhere you want, as long as you are calling it correctly. You may just want to put it in the root of your WordPress site thought. For example, if you uploaded it to your root, it would be in the same folder as these files:

    - wp-admin
    - wp-content
    - wp-includes
    - wp-activate.php
    - wp-app.php
    - wp-config.php

    You could then call it via blogurl.com/mailform.php

    If you were to put it in a template, it would be:
    blogurl.com/wp-content/themes/YOUR-THEME/mailform.php

    Hi rjpinney, have you been able to make any progress on your custom contact form?

    Thread Starter rjpinney

    (@rjpinney)

    Hi Brad,

    Yes – I’m trying to limit the number of active plugins on the site, and find that coding things (usually by trial and error) is a good way of picking it up.

    I’ll have another go at the form, but no luck as yet.

    Thanks for your help!

    If you are still having trouble finding the url you should post the form to, respond back with the url to your blog and the path to your file. The path, for example, would look something like this:
    /home/user/public_html/mailform.php

    Thread Starter rjpinney

    (@rjpinney)

    Just managed to get it work, finally! I’m not actually sure what changed, but it suddenly decided to work with /mailform.php

    Thanks again

    Thread Starter rjpinney

    (@rjpinney)

    Now that the script is running correctly, I have run into another problem – Instead of the script (copied below) showing up a plain text page with either the appropriate error message or the success message, I want it to redirect to a page I have set up on my blog. (EG: For invalid details, redirect to a page that says there was a problem and for success, to a thanks page).

    This is some stock code from a tutorial, but is working – do you know what the command would be to direct to a URL?

    Thanks!

    <?php
    if(isset($_POST['email'])) {
    
        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to = "editor@hurstblog.co.uk";
        $email_subject = "HurstBlog Contact Message";
    
        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 />";
            die();
        }
    
        // validation expected data exists
        if(!isset($_POST['first_name']) ||
            !isset($_POST['last_name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['telephone']) ||
            !isset($_POST['comments'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }
    
        $first_name = $_POST['first_name']; // required
        $last_name = $_POST['last_name']; // required
        $email_from = $_POST['email']; // required
        $telephone = $_POST['telephone']; // not required
        $comments = $_POST['comments']; // 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(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(strlen($comments) < 2) {
        $error_message .= 'The Comments you entered do 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 .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Telephone: ".clean_string($telephone)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\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 -->
    
    Thank you for contacting us. We will be in touch with you very soon.
    
    <?php
    }
    ?>

    You can use javascript, and tell it to redirect:

    http://www.tizag.com/javascriptT/javascriptredirect.php

    If you read that tutorial carefully, it also has the option to redirect after a set amount of time, such as 5 seconds.

    Thread Starter rjpinney

    (@rjpinney)

    Thanks for the link, unfortunately I think it may have been slightly above my head.

    I have managed to get it to direct to a URL when the form has been successfully completed, which is half the battle. What I now want it to do is direct to a different URL when the form validation is declined.

    From what I can figure out, the middle section of the code pasted below is the validation of what the user has put in to the HTML form on the site. When that comes back negative, it gives a plain text ‘We are sorry etc’ message, as coded at the top part of the form. What I want to have is for it to go to hurstblog.co.uk/contact-error instead.

    If you have any pointers on this it would be much appreciated, I can’t seem to find the answer in any tutorials and im very new to PHP!

    <?php
    if(isset($_POST['email'])) {
    
        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to = "editor@hurstblog.co.uk";
        $email_subject = "HurstBlog Contact Message";
    
       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 />";
            die();
        }
    
        // validation expected data exists
        if(!isset($_POST['first_name']) ||
            !isset($_POST['last_name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['comments'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }
    
        $first_name = $_POST['first_name']; // required
        $last_name = $_POST['last_name']; // required
        $email_from = $_POST['email']; // required
        $comments = $_POST['comments']; // 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(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(strlen($comments) < 2) {
        $error_message .= 'The Comments you entered do 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 .= "Last Name: ".clean_string($last_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Comments: ".clean_string($comments)."\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 -->
    
    <?php
    {
    header("Location: http://hurstblog.co.uk/contact-thanks");
    }
    ?>
    
    <?php
    }
    ?>

    How about this?

    // validation expected data exists
        if(!isset($_POST['first_name']) ||
            !isset($_POST['last_name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['telephone']) ||
            !isset($_POST['comments'])) {
    
            // comment out the below line so it doesn't run
            // died('We are sorry, but there appears to be a problem with the form you submitted.');
    
            // and instead redirect the user to your error page
            header("Location: http://hurstblog.co.uk/contact-error");
        }

    Basically instead of “dying” and showing an error page, direct the user to your custom error page.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Issues with HTML/PHP contact form’ is closed to new replies.