【问题标题】:How do I make this HTML form work?如何使这个 HTML 表单工作?
【发布时间】:2016-06-01 06:36:34
【问题描述】:

我正在处理一个网站模板,其中包含一个 HTML 格式的联系表单。我很难让它通过电子邮件发送表单内容。我尝试创建 PHP 脚本来这样做,但我没有任何运气。任何帮助将不胜感激!

这是表单的 HTML 代码:

<form action="" class="main_form error" novalidate target="_blank">
  <label class="form-group">
    <span class="fa fa-asterisk"></span>
    <input type="text" name="name" placeholder="Your name here" data-validation-required-message="Please, enter Your name" required />
    <span class="help-block text-danger"></span>
  </label>
  <label class="form-group">
    <span class="fa fa-asterisk"></span>
    <input type="email" name="email" placeholder="Your E-mail here" data-validation-required-message="Your E-mail isn't correct" required />
    <span class="help-block text-danger"></span>
  </label>
  <label class="form-group">
    <span class="fa fa-asterisk"></span>
    <textarea name="message" placeholder="Your message here" data-validation-required-message="Please, type Your message" required></textarea>
    <span class="help-block text-danger"></span>
  </label>
  <button>Send it to me</button>
</form>

【问题讨论】:

  • 添加你的 php 脚本。告诉我们你到目前为止做了什么。
  • 您可以阅读php中的表单发送here

标签: php html forms contact


【解决方案1】:

我们需要以下文件:

  1. form.html(带有表单的页面)
  2. form_processing.php(包含处理 HTML 表单的脚本的文件。)


您只需要公开您的数据。

HTML

<title>Some form</title>
</head>
<body>
<form action="form_processing.php" method="post">
<p>Your name:<br /><input type="text" name="your_name" /></p>
<p>E-mail:<br /><input type="text" name="email" /></p>
<p>Title:<br /><input type="text" name="title" /></p>
<p>Message: <br />
<textarea name="message" rows="5" cols="45"> </textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>

PHP

<?php
/* Carry out the checking of the input data and their protection from hostile scripts. */
$your_name = htmlspecialchars($_POST["your_name"]);
$email = htmlspecialchars($_POST["email"]);
$title = htmlspecialchars($_POST["title"]);
$message = htmlspecialchars($_POST["messages"]);

/* Set e-mail recipient */
$myemail = "my_email@mail.ru";

/* Check the full mandatory input fields */
$your_name = check_input($_POST["your_name"], "Enter your name!");
$title = check_input($_POST["title"], "Specify the subject");
$email = check_input($_POST["email"], "Enter your e-mail!");
$message = check_input($_POST["message"], "You forgot to write the message!");

/* Create a new variable by assigning a value to */
$message_to_myemail = "Hello! 
Your contact form sent message! 
Sender name: $your_name 
E-mail: $email 
Text: $message 
End";

/* Send the message using mail() function */
$from  = "From: $yourname <$email> \r\n Reply-To: $email \r\n"; 
mail($myemail, $title, $message_to_myemail, $from);
?>
<p>Your message has been successfully sent!</p>
<p>Go <a href="index.php">Home >>></a></p>
<?php

/* If when filling out the form mistakes were made the following code will work: */
function check_input($data, $problem = "") {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    if ($problem && strlen($data) == 0) {
    show_error($problem);
    }
    return $data;
    }
}

function show_error($myError) {
    // Some code
}

?>
<html>
    <body>
        <p>Please correct the following error:</p>
        <?php echo $myError; ?>
    </body>
</html>

【讨论】:

    【解决方案2】:

    我第一次尝试提供帮助,希望这会有所帮助。

    <!DOCTYPE HTML>
    <HEAD>
    <meta charset="utf-8">
    <TITLE>Contact</TITLE>
    <style>
    #form  {width : 500px;
            border-style : solid;
            border-width : 3px;
            border-color : blue;  }
    
    </style>
    <script type="text/javascript"> 
    
    function send_message (){  
    
       var onoma = document.getElementById("Name").value;
       var mail = document.getElementById("email").value;
       var Theme = document.getElementById("subject").value;
       var keim = document.getElementById("Texts").value;
    
       var mail_link = "mailto:your_email?subject=From Site         :"+Theme+"&body="+keim; 
       var win = window.open(mail_link, 'emailWindow');  
    }
    </script>
    
    </HEAD>
    <BODY>
    <center>
    <div id="form">
    <pre>
      Name : <input type="text" id="Name"><br>
    E-mail : <input type="email" id="email"><br>
    Subject: <input type="text" id="subject"><br>
             <textarea height="80" width="200" id="keimeno"></textarea>
    </pre>
    <button onclick="send_message()">Send it</button>
    </div>
    </pre>
    </FORM>
    </center>
    </BODY>
    </HTML>
    

    【讨论】:

      【解决方案3】:

      [由于您没有显示任何 PHP 代码,我可能会告诉您一些您已经知道的事情。]

      1。获取表单内容到服务器

      可以(通常)使用 GET 或 POST 请求提交表单。使用哪种方法由

      元素中的方法属性决定。例如
      。默认值为 GET,因此由于您尚未在表单代码中指定将要使用的方法。 GET 请求将数据附加到 URL 后的问号,例如 mysite.com/form.php?var1=val1&var2=val2&... 正如您可能想象的那样,如果数据有点不同于小。而且,还有长度限制。尽管还有其他事情需要考虑,但 GET 请求不适合“大”请求。

      输入 POST:这样 URL 将保持不变,数据将在“幕后”发送。(阅读 HTTP 协议了解详细信息)

      您需要在表单中更改的唯一内容是前面提到的方法属性,无论使用哪种方法,其他所有内容看起来都相同。 (在这种情况下,如果您要提交文件,事情就会变得更加复杂)

      2。使用 PHP 处理到达服务器的请求

      当 PHP 处理 GET 或 POST 请求时,数据被放置在分别称为 $_GET 和 $_POST 的保留数组中。索引是 HTML 代码中输入元素的名称,即“名称”、“电子邮件”和“消息”。所以要么 $_GET['name'] 要么 $_POST['name'] 来获取用户的名字。 您应该确保提交的数据是合法的。永远不要相信用户提交的数据!

      3。在 PHP 中使用某种方式发送您的邮件。

      这可以是一个内置函数:

      邮件(收件人、主题、消息)

      mail($recipient_email, 'my subject', $_GET['message']);
      

      ..在这种情况下。请注意,需要一些配置工作才能让 PHP 知道将邮件发送到哪里:)。

      ..或者像PHPMailer这样的包。

      $mail = new PHPMailer;
      
      $mail->setFrom($_GET['email'], $_GET['name']);
      $mail->addAddress('your email', 'Your name'); // Add a recipient
      
      $mail->isHTML(true); // Set email format to HTML
      
      $mail->Subject = 'Contact form subject';
      $mail->Body    = $_GET['message'];
      
      if(!$mail->send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } else {
          echo 'Message has been sent';
      }
      

      再次:

      • 您很可能应该对联系表单使用 POST 请求。
      • 信任用户数据。

      祝你好运:)。

      【讨论】:

      • 感谢大家的帮助!我能够使用你们提供的示例构建一个 php 脚本来执行表单。一切正常!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      相关资源
      最近更新 更多