• Resolved Giu Tae Kim

    (@giu-tae-kim)


    Hi people…

    I create a custom page-template for my contact page, with the following code:

    <?php
    
    // Template Name: Contact Page
    
    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    
    $to = '[email protected]';
    $subject = $_POST['subject'];
    $headers = 'Reply-to: ' . $_POST['name'] . ' <' . $_POST['email'] . '>' . ':';
    $message = $_POST['message'];
    
    wp_mail($to,$subject,$message,$headers);
    
    }
    
    ?>
    
    <?php get_header();?>
    
    <div id="content" style="padding:0">
    
    <form id="contact-form" method="post" action="<?php the_permalink();?>">
    
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" value="" placeholder="Tu nombre" autofocus="autofocus" required="required" />
    
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" value="" placeholder="[email protected]" required="required" />
    
    <label for="subject">Subject:</label>
    <select id="subject" name="subject" required="required">
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    </select>
    
    <label for="message">Message:</label>
    <textarea id="message" name="message" placeholder="Write your message here, please." required="required" maxlength="99"></textarea>  
    
    <input type="submit" value="SEND!" id="submit" class="btn" /> 
    
    </form>
    
    </div>
    
    <?php get_footer();?>

    I try and try, but in all my tests the submit button send me to an error page, with a title “Page not found”.

    I rly want to use pure wp functions without the need of plugins, to have the maximum control over my site. And also prefer the code readable and clean.

    I spent a whole week looking for docs and tuts but the use of wp_mail seems not too clear to me…

    Please can someone tell me what is wrong with my code above?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Your form action:

    action="<?php the_permalink();?>">

    Are you sure you are sending the form to the correct url to be processed?

    Perhaps try changing the “type” of your submit button from ‘submit’ to ‘button’?

    Thread Starter Giu Tae Kim

    (@giu-tae-kim)

    Hi Josh… thnks for the attention

    But if I change the type to button, the form are not going to send anything to anywhere…

    On the other hand, as you say maybe the problem is “the_permalink” code… I supose that as long as the php wp_mail code is in the same file with the template, I put the action address the same as the current page.

    I have to put a different location to proccess the request? or the wp_mail in a different file?

    Thnx again…

    Thread Starter Giu Tae Kim

    (@giu-tae-kim)

    OK…. think I find the solution.

    If someone is interested, when I submit a form I have to use non-default names for input fields (like “name”, “subject”, “email”, and others). Supose that is causing conflicts with wp.

    Better use custom names… like:

    <label for="name">Name:</label>
    <input type="text" id="name" name="customname" value="" placeholder="Your name" autofocus="autofocus" required="required" />

    I make this changes and the function work like a charm. ??

    Someone have good suggestions to customize more the wp_mail function… maybe add a mail template?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Failing to submit a very simple form with wp_mail’ is closed to new replies.