gpt4 book ai didi

javascript - PHP联系表单修改

转载 作者:行者123 更新时间:2023-11-28 20:04:32 25 4
gpt4 key购买 nike

我有一个基本的 php 脚本,用于简单的联系表单,其中包含姓名、电子邮件和消息输入。
我希望在其中包含更多选项,但不知道该怎么做。我进行了搜索,但找不到所有的解决方案。我愿意:

1.将副本发送到发件人电子邮件
我想包含一些输入,以便发件人在检查表单中的输入时可以选择接收他提交到电子邮件的副本。

2.上传文件
另外,如果可能的话,在同一个 php 脚本中,我希望发送者可以在提交表单时附加文件(最好仅包含 img 扩展名)。

3.感谢留言
对此不确定,但现在提交表单时,我在 echo 中收到一条简单的感谢消息。如果可能的话,我希望此消息保持可见 5 秒,然后重定向到 index.html。

这是表单的 php:

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$formcontent="Name: $name \nEmail: $email \nMessage: $message";
$recipient = "test123@...";

$subject = "Contact";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

echo
"<div style='display: block; text-align: center;'>" .
"<span style='font-size: 14px;'>You message has been sent!</span>" .
"<a href='index.html'>Go back</a>" .
"</div>";
?>

和演示 jsfiddle表单设置。

感谢您的帮助。

最佳答案

这是一个全局设置,只是为了让您知道我将如何执行此操作(如果我想在一页上执行此操作,但最好制作函数等)

编辑:另请注意,我不知道这是否有效。也许有错误,但我这样做只是为了让您开始。

    <?php 
//Check if form submitted
if (isset($_POST)) {

//this all will run when form is submitted


//First sanitize you data thats been posted
$name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
$email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
$message = htmlentities($_POST['message'], ENT_QUOTES, 'UTF-8');

//make a error array to hold errors
$error = array();
//check if fields are not empty you can also do other checks
if (!empty($name) || !empty($email) || !empty($message))

//here you could do extra checks.. like check if emai is really a email...
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
//email invalid
array_push($error, 'email not valid');
}
//for image you could also do a if...
if(isset($_FILES)) {
$uploads_dir = 'YOUR DIR'
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$temp = $_FILES['image']['tmp_name'];
$error = $_FILES['image']['error'];

if ($error === 4) {
//No file was selected
return false;
}
else
{
//do your stuff with the image here...
move_uploaded_file($temp, "$uploads_dir/$temp");
}

///you could do more ifs.. but if all is good then do the mail

$subject = 'new contact form message';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($email, $subject, $message, $headers);
$success = "here the success message";
} else {
//some fields are empty
array_push($error, 'some fields are empty');
}
?>
<!-- THE ENCTYPE IS NEEDED FOR IMAGES -->


<form action="submit.php" name="contact-form" id="contact-form" method="post" enctype="multipart/form-data">
<input name="name" placeholder="Name" type="text" id="name" required />
<input name="email" placeholder="Email" type="email" id="email" required />
<textarea name="message" placeholder="Message" id="message" required></textarea>
<input type="file" id="upload-file" accept="image/*" />
<div class="clear"></div>
<input type="checkbox" id="copy" name="copy" />
<label for="copy">Send a copy to my email</label>
<div class="clear"></div>
<input type="submit" value="Submit" form="contact-form" name="submit-form" />
</form>

<?php
if (isset($success) && !empty($success)) {
//echo the success
echo $success
}
if (isset($error) && !empty($error)) {
//loop trough error
foreach ($error as $e) {
echo $e;
}
}
?>

关于javascript - PHP联系表单修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com