【问题标题】:php throwing "undefined index" or not sending email in email form [duplicate]php抛出“未定义的索引”或不以电子邮件形式发送电子邮件[重复]
【发布时间】:2016-05-19 06:11:53
【问题描述】:

我正在尝试在网站上放置一个基本的电子邮件表单,但遇到了“未识别索引”的问题。我四处阅读,发现“isset()”已经解决了这个问题。我发现了

$... = isset($_POST['...']);

确实消除了错误消息,但我的代码什么也没做。该页面甚至不会刷新或引发另一个错误。这是我的html:

    <form method="post" action="index.php">
        <h1>Send Us an Email</h1>
        <header class="emailbody">
           <label>Name</label>
           <input name="name" placeholder="Type Here">
        </header>
        <section class="emailbody">
            <label>Email</label>
            <input name="email" type="email" placeholder="Type Here">
        </section>
        <footer class="emailbody">
            <label>Message</label>
            <textarea name="message" placeholder="Type Here"></textarea><br>
        </footer>
        <div class="submitbutton">
            <input id="submit" type="submit" value="Send">
        </div>
    </form>

这是我的 php:

<?php

    $name = isset($_POST['name']);
    $email = isset($_POST['email']);
    $message = isset($_POST['message']);
    $from = 'From: SiteDemo';
    $to = 'exemail@gmail.com';
    $subject = 'Hello';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

?>

我已经尝试过在不同索引周围使用和不使用“isset()”。没有,它会抛出一个错误,并且,代码不会做任何事情。

【问题讨论】:

  • 你要发送空邮件吗???如果没有设置值??

标签: php html forms email indexing


【解决方案1】:

有两点需要纠正:

1) 你还没有写mail()函数语句。

2) isset() 仅返回 TRUEFALSE 取决于是否设置了变量。如果设置了变量,它不会返回变量。

更正为:

$name = isset($_POST['name']) ? $_POST['name'] : '';

其他变量也一样。

【讨论】:

    【解决方案2】:

    是的,您可以在此处使用isset() 来检查值集与否:

    if(isset($_POST['submit']))
    {   
       // if you dont want to send empty email.
       if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) 
       {
          $name = trim($_POST['name']);
          $email = trim($_POST['email']);
          $message = trim($_POST['message']);
          $header = "From: SiteDemo"; 
          $to = 'exemail@gmail.com';
          $subject = 'Hello';
    
          $body = "From: $name\n E-Mail: $email\n Message:\n $message";
          mail($to,$subject,$body,$header);
       }
    }
    

    并在提交按钮中添加名称属性为:

    <input id="submit" type="submit" value="Send" name="submit">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      相关资源
      最近更新 更多