首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >联系人表单未发送?

联系人表单未发送?
EN

Stack Overflow用户
提问于 2018-12-14 04:24:13
回答 1查看 77关注 0票数 1

所以我试着使用这个Codepen的联系人表单

https://codepen.io/ssbalakumar/pen/uzDIA

我的页面上有表单,不确定图像输入字段对于表单是否正确,但似乎显示正确,所以在我填写表单并单击提交后,由于某种原因,它会重新加载页面,并且我看不到任何实际发送到何处的选项。

该页面是托管的,目前没有php,我不知道如何标记,但我假设它是发送操作所需的?

那么有两个问题,1图像输入是否适用于此设置?2,如何设置将接收已提交邮件的电子邮件

HTML

  <div id="contactDiv">  
  <form id="contact" action="" method="post">
  <h3>Quick Contact</h3>
  <h4>Contact us today, and get reply with in 24 hours!</h4>
  <fieldset>
  <input placeholder="Your name" type="text" tabindex="1" required 
  autofocus>
  </fieldset>
  <fieldset>
  <input placeholder="Your Email Address"  type="email" tabindex="2" 
  required>
  </fieldset>
  <fieldset>
  <input placeholder="Your Phone Number" type="tel" tabindex="3" required>
  </fieldset>
  <input type="file" tabindex="4" required>
  </fieldset>
  <fieldset>
  <textarea placeholder="Type your Message Here...." tabindex="5" required> 
  </textarea>
  </fieldset>
  <fieldset>
  <button name="submit" type="submit" id="contact-submit" data- 
  submit="...Sending">Submit</button>
  </fieldset>
  </form>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-14 04:53:03

首先,你需要一个.php文件来获取PHP。您可以为您的表单创建一个名为sendEmail.php的表单。然后,要从表单调用.php文件,请将操作参数更改为action="sendEmail.php"。要实际获得sendEmail.php的值,请使用POST方法。以下是代码应该显示的内容的简化版本:

index.html

<form id="contact" action="sendEmail.php" method="post">
    <input name="yourName" placeholder="Your name" type="text" tabindex="1" required autofocus />
</form>

sendEmail.php

<?php
    $yourName = $_POST["yourName"];

    echo $yourName; //Outputs the inputted value for Your Name
?>

以下是一些注意事项:

要发送电子邮件,请使用前面提到的here中的PHP mail()函数。

要非常小心MYSQL Injections。虽然在MYSQL数据库上使用,但它们仍然可以破坏这样的脚本。

编辑

对于图像,我建议将图像上传到您的服务器,然后在电子邮件中使用<img>标签来发送图像。

这是我从here得到的代码,并做了一些修改:

<?php
$target_dir = "imageUploads/";
$target_file = $target_dir . basename($_FILES["imgUpload"]["name"]);
$uploadOk = 1;
$imgName = basename($_FILES["imgUpload"]["name"]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["imgUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["imgUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["imgUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["imgUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

然后,对于PHP mail()函数,只需包含路径为http://yoursite.com/imageUploads/nameOfTheImage.TYPEimage tag即可。路径应该保持不变,您可以从var $imgName中获取最后一部分,因此您可以这样做

$imageUrl = 'http://yoursite.com/imageUploads/' . $imgName;

您需要为要上载的图像创建一个文件夹。在上面的代码中,我使用了imageUploads

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53769546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档