Javascript PHP表单相同页面提交消息
我是PHP新手,使用以下代码(正在运行),但是页面会被更改为另一个页面,网站有不同的表单,我希望成功消息出现在表单或任何不影响现有页面的警报框格式上。考虑到我的初级水平,我需要简单步骤的帮助 我的HTML代码:Javascript PHP表单相同页面提交消息,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我是PHP新手,使用以下代码(正在运行),但是页面会被更改为另一个页面,网站有不同的表单,我希望成功消息出现在表单或任何不影响现有页面的警报框格式上。考虑到我的初级水平,我需要简单步骤的帮助 我的HTML代码: <form action="order-mail.php" method="post" class=""> <div class="row"> <div class="col-md-12
<form action="order-mail.php" method="post" class="">
<div class="row">
<div class="col-md-12">
<ul>
<li>
<i class="fa fa-user" aria-hidden="true"></i>
<input placeholder="Full Name *" type="text" name="first_name" maxlength="50" size="30" required>
</li>
</ul>
</div>
<div class="col-md-6">
<ul>
<li>
<i class="fa fa-envelope" aria-hidden="true"></i>
<input placeholder="Email Address *" type="text" name="email" maxlength="80" size="30">
</li>
</ul>
</div>
<div class="col-md-6">
<ul>
<li>
<i class="fa fa-phone" aria-hidden="true"></i>
<input placeholder="Phone No. *" type="text" name="telephone" maxlength="30" size="30">
</li>
</ul>
</div>
<div class="col-md-12">
<ul>
<li>
<i class="fa fa-paper-plane-o" aria-hidden="true"></i>
<textarea name="comments" placeholder="To help us understand better, enter a brief description about your project."></textarea>
</li>
</ul>
</div>
<div class="col-md-12">
<ul>
<li>
<input type="submit" value="Submit">
</li>
</ul>
</div>
</div>
</form>
-
-
-
-
-
我的PHP代码:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email@mydomain.com";
$email_subject = "xyz Form Submissions";
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['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
$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(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 .= "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 the Submission
<?php
}
?>
谢谢你的意见
您的问题可以通过标准逻辑流程解决:
<?php
// deal with user input, if it exists
if( $_POST['email']) {
$error_message = '';
// validate input, build $error_message if errors
// if no error message, send email
if( !$error_message ) {
// send email...
// redirect to next page
header('Location: /path/to/next/page');
die;
}
}
// done with php logic; show view
?>
<html>
<head>
... snip ...
</head>
<body>
... snip ...
<?php if( isset( $error_message )): ?>
<div><?= $error_message ?></div>
<?php endif; ?>
... the rest of your page ...
注意:看到视图(html)中引用的POST变量应该被认为是可以做得更好的标志
这将获取错误消息,并预先填充表单 一,。创建一个变量来保存错误。2.将PHP代码放在HTML之上,直到$\u POST中有内容后才会处理它。3.回显错误变量(首先应该是空的。4.在PHP中,验证每个字段,如果有错误变量,则将消息添加到错误变量。5.如果错误变量为空,则发送电子邮件,回显成功消息,并包含一个元标记以重定向到另一个页面。有关从ajax获得响应的信息,请参阅以下问题:如果要使用ajax,请在同一页面中回显错误消息e或查看此教程,并检查它们如何在表单中显示错误:完全可以获得此信息..因为我已经提到我太新了,所以做这些都是为了学习透视图..如果可能,请帮助我将其发布,并比较我的代码。您有几个选项:1)使用ajax提交表单。这样,php脚本会以json消息响应,该消息包含错误消息或表示成功(页面上的javascript会重定向到下一页)。2)如果设置了
$error\u message
,php脚本(应始终在输出html之前)会忽略发送电子邮件的尝试,(不要让脚本死掉;只需使用html)并在html中显示错误消息(如果已设置)。选项1更专业,对用户更好,但选项2对初学者更容易。
<div class="col-md-12">
<ul>
<li>
<i class="fa fa-user" aria-hidden="true"></i>
<input placeholder="Full Name *"
type="text"
name="first_name"
value="<?= $_POST['first_name'] ?? '' ?>"
maxlength="50"
size="30"
required
>
</li>
</ul>
</div>