我有一个用HTML定位在页面上的表单,如果用户完成了表单,那么他们就会得到PHP消息的感谢。因为表单是用<form id="formIn"> CSS定位的,所以PHP现在处于错误的位置,有没有人知道如何定位这个位置,从而将PHP文本嵌套到表单中?我尝试将代码包含在PHP中,即
<div id=\"text\">但是没有joy。
到目前为止使用的代码是:
<?php
echo "* - All sections must be complete"."<br><br>";
$contact_name = $_POST['contact_name'];
$contact_name_slashes = htmlentities(addslashes($contact_name));
$contact_email = $_POST['contact_email'];
$contact_email_slashes = htmlentities(addslashes($contact_email));
$contact_text = $_POST['contact_text'];
$contact_text_slashes = htmlentities(addslashes($contact_text));
if (isset ($_POST['contact_name']) && isset ($_POST['contact_email']) && isset ($_POST['contact_text']))
{
if (!empty($contact_name) && !empty($contact_email) && !empty($contact_text)){
$to = '';
$subject = "Contact Form Submitted";
$body = $contact_name."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (@mail($to,$subject,$body,$headers))
{
echo "Thank you for contacting us.";
}else{
echo 'Error, please try again later';
}
echo "";
}else{
echo "All sections must be completed.";
}发布于 2013-11-12 17:27:41
这样做的一个好方法是创建一个div,用于在表单页面中显示消息,然后从php脚本返回到表单页面,包括form.html?error=email或form.html?success到url。然后,使用javascript,您可以识别何时发生这种情况,并显示消息。
如果您需要一些代码示例,请进行注释。
编辑:假设您的php脚本检测到电子邮件字段未被填充,因此它将返回到包含url中的变量的表单网页:
<?php
if(!isset($_POST["email"]){
header("location:yourformfile.html?error=email")
}
?>然后,在您的表单网页中,您必须添加一些javascript,以便在URL中捕获该变量并在页面中显示一条消息:
Javascript:
function GetVars(variable){
var query = window.location.search.substring(1); //Gets the part after '?'
data = query.split("="); //Separates the var from the data
if (typeof data[0] == 'undefined') {
return false; // It means there isn't variable in the url and no message has to be shown
}else{
if(data[0] != variable){
return false; // It means there isn't the var you are looking for.
}else{
return data[1]; //The function returns the data and we can display a message
}
}
}在这个方法中,data[0]是var名称,data[1]是var数据。您应该实现如下方法:
if(GetVars("error") == "email"){
//display message 'email error'
}else if(GetVars("error") == "phone"){
//dislpay message 'phone error'
}else{
// Display message 'Success!' or nothing
}最后,为了显示消息,我建议使用HTML和CSS创建一个div,并将其动画化,使其与jQuery一起出现并消失。或者仅仅显示一个警报alert("Email Error");
发布于 2013-11-12 17:11:32
只需在表单之前或之后创建一个<div>,并在其上放置如果提交条件。喜欢,
<?php
if(isset($_POST['submit']))
{ ?>
<div>
<!-- Your HTML message -->
</div>
<?php } ?>
<form>
</form>https://stackoverflow.com/questions/19935355
复制相似问题