• I created a page template named contactform.php and linked my contact page to this template.

    here the code:

    <?php
    
        //Some variables
        $mymail = "sample@domain.com";
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
    	$find = $_POST['find'];
        $eip = $_POST['eip'];
    
        //Function to check email address
        function checkemail($email) {
          if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$email))  {
            return true;
          }
          else {
            return false;
          }
        }
    
      //Mail Processing
      if ($_POST['esubmit']) {
        //Check for blank fields
        if ($name == "" || $email == "" || $phone == "") {
          echo "<p>You left a blank field.<br/>Please try again.</p>";}
        //Check to see if the email address is valid
        else if (checkemail($email) == false) {
           echo "<p>You entered an invalid email address.</p>";}
        //Send the email if there's no error
        else {
          $body = "Name: $name\r\nEmail: $email\r\nPhone: $phone\r\nHow They Found Us: $find\r\n\r\nIp: $eip";
          mail($mymail,$name,$body,"From: $email\r\n");
          echo "<p>Thank you, $name!<br />Your request has been sent.</p>";}
      }
    
    ?>
      <form class="form" name="contact" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
        Name:*<br/>
        <input type="text" class="text" name="name"><br/>
        Email:*<br/>
        <input type="text" class="text" name="email"><br/>
        Phone:*<br/>
        <input type="text" class="text" name="phone"><br/>
        <div style="text-align:center;">How did you hear about us?</div>
    	<select name="find" class="select">
    	<option value="Choose" selected>Please select...</option>
    	<option value="Magazine">Magazine</option>
        <option value="Radio">Radio</option>
        <option value="Newspaper">Newspaper</option>
    	<option value="Other">Other...</option>
    	</select>
        <input type="submit" name="esubmit" value="Send Mail" class="submit">
        <input type="hidden" name="eip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">
      </form>

    The issue: when i submit the form it takes me to a “not found” page and the form does not get sent via email. I have used this form in other websites (not wordpress, but html created) and it always worked fine. Is there something different within wordpress that i must do? Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" – Is this 100% correct?

    Also, are you using pretty permalinks? They sometimes cause issues with forms. If so, try switching them back to the defualt and see if the form works then.

    Thread Starter xmatter

    (@xmatter)

    the code is correct for after the form is submitted, a message pops up rather than redirecting to another page. I will try changing the permalinks, but that will only be temporary. I am currently using /%category%/%postname%/

    That will probably be your problem then – if you do print_r($_SERVER['php_self']); you get the standard link, not the permalink (I think), so you are in effect driecting your form to a page that WP thinks does not exist. You may need to get the page ID and do action="get_permalink($id)"?

    Not 100% on that, but worth a shot for 30 seconds of work I’d say.

    Thread Starter xmatter

    (@xmatter)

    thank you very much, i will give it shot sometime today and post the results.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘php contact form’ is closed to new replies.