将HTML表单数据发送到发件人的电子邮件

3
我已经在html中创建了一个表单,并使用php脚本尝试将其发送到电子邮件,但它不起作用。您能否检查我错在哪里?
每当我点击发送按钮时,它开始显示php代码而不是将电子邮件发送到指定的电子邮件地址。

HTML
----


<!DOCTYPE html>
<html>
<head>
<title>Form</title> <!-- Include CSS file here -->
<link href="css/form.css" rel="stylesheet">
</head>
<body onload="myFunction()">

<h1> Form Request </h1>

<form name="contactform"  action="email.php"  method="post">

  Name:  <input type="text" name="full_name"><br \><br \>
  Id:   <input type="text" id="id"><br \><br \>
  Email id: <input type="email" email="email"><br \><br \>
  <a href="Form_IP.pdf" download>Download Blank Form </a> <br \><br \><br \>
  Upload Filled Form<br \><br \>
  
  <input type="file" id="myFile" multiple size="50" onchange="myFunction()">

<p id="resellerfile"></p>
    
<script>
function myFunction(){
    var x = document.getElementById("myFile");
    var txt = "";
    if ('files' in x) {
        if (x.files.length == 0) {
            txt = "Select one or more files.";
        } else {
            for (var i = 0; i < x.files.length; i++) {
                txt += "<br><strong>" + (i+1) + ". file</strong><br>";
                var file = x.files[i];
                if ('name' in file) {
                    txt += "name: " + file.name + "<br>";
                }
                if ('size' in file) {
                    txt += "size: " + file.size + " bytes <br>";
                }
            }
        }
    } 
    else {
        if (x.value == "") {
            txt += "Select one or more files.";
        } else {
            txt += "The files property is not supported by your browser!";
            txt  += "<br>The path of the selected file: " + x.value; // If the browser does not support the files property, it will return the path of the selected file instead. 
        }
    }
    document.getElementById("resellerfile").innerHTML = txt;
}
</script>
<br \><br \>

 <input type="submit" value="Submit">



</form>
</body>
</html>

========================

    <?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {
  
  //Email information
  $admin_email = "xyx@submit.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $id = $_REQUEST['id'];
  
  //send email
  mail($admin_email, "$subject", $id, "From:" . $email);
  
  //Email response
  echo "Thank you for contacting us!";
  }
  
  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Name:  <input type="text" name="full_name"><br \><br \>
  Id:   <input type="text" id="id"><br \><br \>
  Email id: <input type="email" email="email"><br \><br \>
  <input type="submit" value="Submit" />
  </form>
  
<?php
  }
?>


这是因为您正在尝试在没有服务器的情况下运行PHP文件。 - Masivuye Cokile
2个回答

2
它展示给你原始的PHP代码,因为浏览器无法直接执行PHP脚本。PHP脚本会在服务器上被处理和执行,然后将HTML响应发送到浏览器。
因此,您需要在远程或本地服务器上托管PHP脚本。如果您想在系统上设置本地服务器,请尝试安装XAMP或WAMP。
请参考下面的链接。 http://www.homeandlearn.co.uk/php/php1p3.html

非常感谢!我会尝试的。 - Nickie

0
你尝试将它放在Xampp的htdocs文件夹中,看是否有响应了吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接