gpt4 book ai didi

php - PHP表格: validation, submission, displaying content

转载 作者:行者123 更新时间:2023-12-03 07:55:21 27 4
gpt4 key购买 nike

最近几天,我一直在尝试修改我在Internet上找到的PHP表单(并且我看到它很流行,因为我已经看到了很多有关该表单的帖子)。
提交表单后,会给我发送电子邮件-非常棒。但是还有其他事情我一直在尝试更改,根本找不到正确/可行的解决方案。

我想要实现的是:

1)提交表单后接收电子邮件(我认为此操作已完成)

2)验证

3)在同一页面上显示错误消息,例如

名称 *

(您的姓名为必填项)

[.......这是文本字段.......]

4)仅在纠正了所有错误或首次正确输入所有数据后才提交表格一次。

5)在验证数据时出现错误消息(之前)时,我的页脚未显示。这是什么原因呢?提交表单时,它显示良好。

这是我一直在尝试修改的代码(我复制了“原始”代码,因为我的代码可能太乱了,并且在我玩了之后也改变了)

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you@yourdomain.com";
$email_subject = "Your email subject line";


function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

下面是来自contact.php的代码的一部分(表格在其中)
<form name="contactform" method="post" action="submit_form.php">

<div class="left_form">
<label for="name" class="label">your name</label>

(this is the code that was suppose to display error message within the same page

<?php if($errors['name'] = "") echo $errors['name']; ?>)


<input type="text" class="inputtext" name="name" value="" />

上面的代码对于所有输入字段几乎相同(唯一不同的是name =“”)

如果有人能帮助我,我将不胜感激。

谢谢。 =)

最佳答案

在这样的函数中处理帖子,并生成一个单独的数组来保存错误的字段。如果您有错误,请输出错误,或者,如果字段验证为OK,则处理该帖子。永远不要相信用户输入。

if(isset($_POST['submit']))
{

if(!ctype_alpha($_POST['fieldname'])) //or other validation type you prefer
{
$errors[] ='<p>problem with fieldname</p>';
}

... repeat above for each field

if(count($errors))
{
//output error message using foreach from
}
else
{
//process the post
}
}

如果您对php不太熟悉,建议您进行大量阅读和教程学习,以便您了解所生成的内容以及将来如何改进它。

首先以纯文本形式编写您想作为伪代码形式发生的事情,然后一点一点地替换为真正的php代码,这样您就可以在每个阶段测试结果。

php official site foreach construct

关于php - PHP表格: validation, submission, displaying content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519201/

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