PHP联系表单未提交电子邮件或重定向新URL

时间:2014-03-25 01:16:35

标签: php html forms twitter-bootstrap

之前我曾使用过联系表单,但这个还没有提交到我的电子邮箱。我还尝试将重定向网址实施到/contact.php

代码的问题是:我的电子邮件没有收到它。我的问题是:什么是不允许联系表单通过电子邮件发送?

<form id='contactus' action='index.php' method='post' accept-charset='UTF-8'>
   <fieldset>
     <div class="first-row">
       <label for="name">Your Name</label>
       <input type='text' class="input-large" name='name' id='name' value=''  placeholder="Your Name"  />
       <label for="phone">Phone Number</label>
       <input type='text' class="input-large" name='phone' id='phone' value=''  placeholder="Phone Number"  />
     </div>
     <div class="second-row">
    <label for="email">Email Address</label>
    <input type='text' class="input-large" name='email' id='email' value=''  placeholder="Email Address" />
    </div>
     <div class="second-row">
       <label for="business">Business</label>
       <input type='text' class="input-large" name='business' id='business' value=''  placeholder="Business" />
     </div>
    <label style="margin-left: 20px;" for="message">Message</label>
    <textarea rows="10" class="input-xlarge" cols="50" name='message' placeholder="Message" id='message'></textarea>
    <div class="clearfix"></div> /* Sorry I'm using Bootstrap */
    <div><input type='submit' class="btn btn-primary" name='Submit' value='Submit' />
    </div>
  </fieldset>
</form>

<script type="text/php">                      
<?php
if(isset($_POST['submit'])) {
  $to = "hello@geniusghost.com";
  $subject = "Genius Ghost Contact Form";
  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $email = $_POST['email'];
  $business = $_POST['business'];
  $message = $_POST['message'];
  $body = "From: $name\n Phone Number: $phone\n E-Mail: $email\n Business: $business\n\n Message:\n $message";
  mail($to, $subject, $body);
} 
?>
</script>

救救我!我知道你们可能一直都会得到这些问题..

2 个答案:

答案 0 :(得分:1)

您的$_POST['submit']应该有一笔资金&#34; S &#34;提交按钮的名称为&#34; 提交&#34;。

此外,您不需要在<script>代码中包装PHP - 仅<?php ... ?>

<强>代码:

<form id='contactus' action='index.php' method='post' accept-charset='UTF-8'>
    <fieldset >
        <div class="first-row">
            <label for="name">Your Name</label>
            <input type='text' class="input-large" name='name' id='name' value=''  placeholder="Your Name"  />
            <label for="phone">Phone Number</label>
            <input type='text' class="input-large" name='phone' id='phone' value=''  placeholder="Phone Number"  />
        </div>
        <div class="second-row">
            <label for="email">Email Address</label>
            <input type='text' class="input-large" name='email' id='email' value=''  placeholder="Email Address" />
        </div>
        <div class="second-row">
            <label for="business">Business</label>
            <input type='text' class="input-large" name='business' id='business' value=''  placeholder="Business" />
        </div>

        <label style="margin-left: 20px;" for="message">Message</label>
        <textarea rows="10" class="input-xlarge" cols="50" name='message' placeholder="Message" id='message'></textarea>
        <div class="clearfix"></div> /* Sorry I'm using Bootstrap */
        <div>
            <input type='submit' class="btn btn-primary" name='Submit' value='Submit' />
        </div>
    </fieldset>
</form>

<?php
if(isset($_POST['submit'])) {
    $to = "hello@geniusghost.com";
    $subject = "Genius Ghost Contact Form";
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $business = $_POST['business'];
    $message = $_POST['message'];

    $body = "From: $name\n Phone Number: $phone\n E-Mail: $email\n Business: $business\n\n Message:\n $message";

    mail($to, $subject, $body);
} 
?>

如果您有任何问题,请与我们联系!

由于

答案 1 :(得分:1)

name='Submit'更改为name='submit'。变量和表单元素区分大小写。

另外,丢失了<script type="text/php"></script>

使用$body = "From: $name\n会导致From:部分action=""形成不正确,您将永远无法直接回复发送电子邮件的人你。

修正/更改并测试。

旁注:如果您在一个文件中使用此代码,请使用action='index.php'代替<form id='contactus' action='index.php' method='post' accept-charset='UTF-8'> <fieldset > <div class="first-row"> <label for="name">Your Name</label> <input type='text' class="input-large" name='name' id='name' value='' placeholder="Your Name" /> <label for="phone">Phone Number</label> <input type='text' class="input-large" name='phone' id='phone' value='' placeholder="Phone Number" /> </div> <div class="second-row"> <label for="email">Email Address</label> <input type='text' class="input-large" name='email' id='email' value='' placeholder="Email Address" /> </div> <div class="second-row"> <label for="business">Business</label> <input type='text' class="input-large" name='business' id='business' value='' placeholder="Business" /> </div> <label style="margin-left: 20px;" for="message">Message</label> <textarea rows="10" class="input-xlarge" cols="50" name='message' placeholder="Message" id='message'></textarea> <div class="clearfix"></div> /* Sorry I'm using Bootstrap */ <div><input type='submit' class="btn btn-primary" name='submit' value='Submit' /> </div> </fieldset> </form> <?php if(isset($_POST['submit'])) { $to = "hello@geniusghost.com"; $subject = "Genius Ghost Contact Form"; $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; $business = $_POST['business']; $message = $_POST['message']; $headers = "From: $name<$email>\r\n"; $body = "From: $name<$email>\r\n Phone Number: $phone\n E-Mail: $email\n Business: $business\n\n Message:\n $message"; mail($to, $subject, $body, $headers); } ?>

在我的脚注下还有更多内容

<?php
if(isset($_POST['submit'])) {

        $to = "hello@geniusghost.com";
        $subject = "Genius Ghost Contact Form";
        $name = $_POST['name'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $business = $_POST['business'];
        $message = $_POST['message'];
        $headers = "From: $name<$email>\r\n";

        $body = "From: $name<$email>\r\n Phone Number: $phone\n E-Mail: $email\n Business: $business\n\n Message:\n $message";

        mail($to, $subject, $body, $headers);
        echo "Thank you.";
        echo "<a href='http://www.example.com/home.php'>Click here</a> to return to the home page.";
        exit();
} 
?>

<form id='contactus' action='' method='post' accept-charset='UTF-8'>
   <fieldset >
   <div class="first-row">
    <label for="name">Your Name</label>
    <input type='text' class="input-large" name='name' id='name' value=''  placeholder="Your Name"  />

        <label for="phone">Phone Number</label>
    <input type='text' class="input-large" name='phone' id='phone' value=''  placeholder="Phone Number"  />
    </div>
    <div class="second-row">
    <label for="email">Email Address</label>
    <input type='text' class="input-large" name='email' id='email' value=''  placeholder="Email Address" />
    </div>
     <div class="second-row">
    <label for="business">Business</label>
    <input type='text' class="input-large" name='business' id='business' value=''  placeholder="Business" />
    </div>

        <label style="margin-left: 20px;" for="message">Message</label>
    <textarea rows="10" class="input-xlarge" cols="50" name='message' placeholder="Message" id='message'></textarea>
    <div class="clearfix"></div> /* Sorry I'm using Bootstrap */
    <div><input type='submit' class="btn btn-primary" name='submit' value='Submit' />
    </div>
     </fieldset>
</form>

<强>脚注:

如果您不希望在提交后出现该表单,则应该是这种情况,请使用以下内容:

旁注:这只有在PHP位于HTML之上且用作单个文件时才有效,如果您想使用此方法。

{{1}}