Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have some problem making my contact form work. The contact form should only be in one simple site. So i created two templates. The first template only has the form. The other one have the php code to send a mail. But i cant make the form contact the other template

Here is the first template

<?php
/*
Template Name: Contact 1
*/
?>

<?php get_header(); ?>

<div id="content-container">
    <div id="content" role="main">


<form method="post" action="http://www.sbk-sm.se/lydnad-agility.2012/index.php/kontakt-2">
    Namn:
    <br />
    <input type="text" name="name" />
    <br />
    Email:<br />
    <input type="email" name="email" />
    <br /> <br />

    Till:
    <select name="mailTo">
        <option value="adibbinhaider@gmail.com">Ex2 Mail</option>
        <option value="">Ex2 Mail</option>
    </select>
    <br /><br />
    Meddelende:<br/>
    <textarea name="msg"></textarea>
    <input type="submit" value="Skicka" name="submit">
</form>
    </div><!-- #content -->
</div><!-- #content-container -->


<?php get_sidebar(); ?>
<?php get_footer(); ?>

Here is the second template

<?php
/*
Template Name: Contact 2
*/
?>

<?php
    if($_POST["name"] != "")    
    {
        echo "Medelende har skickats ";
        $name = $_POST["name"];
        $emailFrom = $_POST["email"];
        $mailTo = $_POST["mailTo"];

        $msg = "Från: \n";
        $msg .= "Namn: " .$name;
        $msg .= "\nE-Mail: " .$emailFrom;
        $msg .= "\n\n";     
        $msg .= "Till: \n";
        $msg .= $mailTo;
        $msg .= "\n\n";
        $msg .= "Meddelnde: \n";
        $msg .= $_POST["msg"];
        $msg .= "\n\n";

        $subject = utf8_decode("SM Lydnad & Agility 2012 Kontaktformulär");
        $headers = utf8_decode("From: " .$emailFrom."\r\n");

        mail($mailTo, $subject, utf8_decode($msg), $headers);

    }
?>

So I cant get to the second template, WP says the site doesnt exists

share|improve this question
    
Is sbk-sm.se/lydnad-agility.2012/index.php/kontakt-2 the permalink to your second page? Also, this form can be very easily and very badly abused by spammers to send any message they want to any recipient they want. You may want to consider encoding the email recipient as, say, "person1" and then your php knowing that person1 is adibbinhaider@gmail.com –  Matthew Boynes Jan 29 '12 at 16:32
add comment

2 Answers 2

up vote 1 down vote accepted

One way to accomplish this is to setup two pages, with two page templates, one with your form code and the other with your mail code. The form action would point to the permalink of the page whose template hosts the mail code.

That said, I'd actually recommend using a plugin like Contact Form 7 instead. It's highly modular and customizable and is a major time saver.

share|improve this answer
    
but i need a drop down menu with diffrente values, and the mail will be sent depending on who you want to mail –  Mrfgtngt Jan 29 '12 at 16:30
    
You can do that with a plugin, too. I highly encourage you to check out CF7, it is most definitely the path of least resistance. –  Matthew Boynes Jan 29 '12 at 16:34
    
hmm... just installed it but i coulnd find it :S –  Mrfgtngt Jan 29 '12 at 16:36
    
What couldn't you find? –  Matthew Boynes Jan 29 '12 at 16:44
add comment

How about using a plugin like contact form 7 or gravity forms instead? Otherwise, just have the page with the form do the form processing and submit the form to the page it's currently on. You'll need to register the templates correctly and what not, but that's as simple as putting /* Template Name: Your Template Name */ right after the opening PHP tag in your template and putting your template in your theme's root directory. You can also register templates using the {page}_template filter, which is useful if you're writing a plugin. See this tutorial for more on that.

share|improve this answer
    
jinx buy me a coke –  Matthew Boynes Jan 29 '12 at 16:25
    
On it! I like your answer so much I think I'll upvote :D –  m0r7if3r Jan 29 '12 at 16:26
    
Haha, well it's such a good ansewr I can only do the same to yours! –  Matthew Boynes Jan 29 '12 at 16:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.