【问题标题】:PHP Contact Form is not submitting to email or redirecting new URL [closed]PHP 联系表未提交到电子邮件或重定向新 URL [关闭]
【发布时间】:2014-03-26 00:16:28
【问题描述】:

我以前使用过联系表格,但这个表格没有提交到我的电子邮件中。我也在尝试将重定向 URL 实现到 /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>

帮帮我!我知道你们可能一直都有这些问题..

【问题讨论】:

  • name='Submit' 更改为 name='submit' 变量和表单元素区分大小写。实际上,这更像是一个答案而不是评论。另外,去掉&lt;script type="text/php"&gt;&lt;/script&gt;
  • 嗯嗯嗯,&lt;script type="text/php"&gt;?顺便说一下,你需要澄清你的问题,什么代码是什么文件的一部分?
  • 另外,你会有一个粗鲁的觉醒。让我们看看谁解决了这个问题; 我有
  • 更改了提交,但电子邮件本身仍未收到。我知道脚本类型很不稳定,但是之前它会使整个页面崩溃。它实际上在一个 .php 文件中。 index.php
  • See my answer, tested@Cee 完整的解释。

标签: php html forms twitter-bootstrap


【解决方案1】:

您的$_POST['submit'] 应该有一个大写“S”,因为您的提交按钮的名称是“Submit”。

另外,您不需要将 PHP 包装在 &lt;script&gt; 标签中 - 只需 &lt;?php ... ?&gt;

代码:

<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);
} 
?>

如果您有任何问题,请告诉我!

谢谢

【讨论】:

    【解决方案2】:

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

    另外,去掉 &lt;script type="text/php"&gt;&lt;/script&gt;

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

    已修复/更改并经过测试。

    旁注:如果您在一个文件中使用此代码,请使用 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 位于 HTML 之上并用作单个文件时,这才有效,如果您想使用此方法。

    <?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>
    

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2015-10-11
      相关资源
      最近更新 更多