联系表格未发送?

时间:2018-12-13 20:24:13

标签: html

所以我试图在此Codepen上使用“联系方式”

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

我的页面上有表单,不确定图像输入字段是否适合表单,但看起来似乎正确,因此在我填写表单并点击Submit之后,由于某种原因它将重新加载页面,并且我没有看到它实际发送到的任何选项。

该页面是托管的,当前没有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>

1 个答案:

答案 0 :(得分:3)

首先要获取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()函数仅将image tag包含在路径中,例如http://yoursite.com/imageUploads/nameOfTheImage.TYPE。路径应该保持不变,您可以从var $imgName中获取最后一部分,所以您可以这样做

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

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