PHP联系表格代码不起作用 [英] PHP Contact Form Codes Doesn't Work

查看:94
本文介绍了PHP联系表格代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP和jQuery Validate.min.js创建PHP联系表单( http://jqueryvalidation.org/

我尝试提取jQuery和PHP都需要的所有代码,但由于某种原因,它的返回值位于顶部。



查看我的代码如下:

 <?php 

$ hasError = false;
$ sent = false;

if(issent($ _ POST ['submitform'])){
$ name = trim(htmlspecialchars($ _ POST ['name'],ENT_QUOTES));
$ email = trim($ _ POST ['email']);
$ message = trim(htmlspecialchars($ _ POST ['message'],ENT_QUOTES));

$ fieldsArray = array(
'name'=> $ name,
'email'=> $ email,$ b $'message'=> $消息
);

$ errorArray = array();

foreach($ fieldsArray as $ key => $ val){
switch($ key){
case'name':
case'message':
if(empty($ val)){
$ hasError = true;
$ errorArray [$ key] = ucfirst($ key)=字段为空。;
}

break;

case'email':
if(!filter_var($ email,FILTER_VALIDATE_EMAIL)){
$ hasError = true;
$ errorArray [$ key] =输入的电子邮件地址无效;
} else {
$ email = filter_var($ email,FILTER_SANITIZE_EMAIL);
}
break;
}

}

if($ hasError!== true){
$ to =samuelnico.norton@gmail.com;
$ subject =来自联系表单的消息;
$ msgcontents =名称:$ name< br />电子邮件:$ email< br />消息:$ message;
$ headers =MIME-Version:1.0 \r\\\
;
$ headers。=Content-type:text / html; charset = iso-8859-1 \r\\\
;
$ headers。=From:$ name< $ email> \r\\\
;

$ mailsent = mail($ to,$ subject,$ msgcontents,$ headers);

if($ mailsent){
$ sent = true;
unset($ name);
unset($ email);
unset($ message);
}
}
}

?>



<!doctype html>
< html>
< head>
< meta charset =UTF-8>
< meta name =viewportcontent =width = device-width,height = device-height,initial-scale = 1.0,user-scalable = no>

<! - - 页面标题 - >
< title> TEST FILE< / title>


<! - 样式 - >
< link rel =stylesheethref =css / slides.cssname =main-styles>

<! - 脚本 - >
< script src =http://code.jquery.com/jquery-1.11.3.min.jsname =jquery>< / script>
< script src =js / plugins.jstype =text / javascriptname =plugins>< / script>
< script src =js / slides.jstype =text / javascriptname =main-scripts>< / script>

<! - 为联络表格增加脚本 - >
< script src =https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.jstype =text / javascriptname =主脚本>< /脚本>

< script type =text / javascript>
jQuery(document).ready(function($){
$(#contactform)。validate {
rules:{
name:{
required: true,
minlength:2
},
email:{
required:true,
email:true
},
message {
必填:true,
minlength:50
}
},
消息:{
名称:{
required:请输入您的姓名,
minlength:您的名字看起来有点不对吗?
},
电子邮件:{
required:请输入您的电子邮件地址,
电子邮件:请输入av alid电子邮件地址

消息{
required:请输入您的消息,
minlength:您的消息似乎有点不对吗?请输入至少50个字符

}
});
});

< / script>



< / head>
< body class =幻灯片动画>



<! - 侧边栏 - >
< nav class =sidebar deepPurpledata-sidebar-id =1>
< div class =close>< svg>< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =#close>< / use>< / svg>< / div>
< div class =content>
< a href =#class =logo>< svg width =120height =50>< use xmlns:xlink = http://www.w3.org/1999/xlinkxlink:href =#logo>< / use>< / svg>< / a>
< ul class =mainMenu大写>
< li>< a href =#> Tour< / a>< / li>
< li>< a href =#>升级< / a>< / li>
< li>< a href =#>帮助< / a>< / li>
< a href =#>浏览< / a>< / li>
< / ul>

< ul class =social>
< li>< a href =#>< svg>< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =# Twitter的>< /使用>< / SVG>< / A>< /锂>
< li>< a href =#>< svg>< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =#者>< /使用>< / SVG>< / A>< /锂>
< li>< a href =#>< svg>< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =# GOOGLEPLUS>< /使用>< / SVG>< / A>< /锂>
< li>< a href =#>< svg>< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =# Pinterest的>< /使用>< / SVG>< / A>< /锂>
< / ul>
< / div>
< / nav>

<! - Slide#55 - >
< section class =slide fade kenBurns>
< div class =content>
< div class =container>
< div class =wrap>

< div class =fix-6-12 box-55>
< div class =pad shadow selected ae-3>

< form class =wide centerid =contactformmethod =postaction =<?php echo $ _SERVER ['PHP_SELF'];?> NOVALIDATE>

<?php

if($ sent === true){
echo< h2 class ='success'>谢谢,您的留言已成功发送< / h2>;
} elseif($ hasError === true){
echo'< ul clas =errorlist>';
foreach($ errorArray as $ key => $ val){
echo< li>。 ucfirst($键)。 field error - $ val< / li>;
}
echo'< / ul>';
}

?>


< label class =uppercase ae-4for =name37>您的名字< / label>


< label class =uppercase ae-6for =email37>电子邮件< / label>


< label class =uppercase ae-8for =message37> Message< / label>

< textarea class =stroke round left ae-9 sourceSansid =message37type =emailplaceholder =Messagerequired><?php echo(isset($ message) ?$ message:); ?>< / textarea的>

< input class =按钮宽粉红圆大写ae-10按钮-55type =submitvalue =发送消息name =submitform/>

< / form>


< / div>
< / div>

< / div>
< / div>
< / div>

< / section>



< / body>
< / html>

正如您所看到的,我将HTML代码放在了顶部。现在我想在每个文本框下面显示错误。

你可以看到它是如何发展的: http: //snowboardflowbindings.com/testing/testfile.php



致命错误:无法在/ home / olymnews / public_html的写入上下文中使用函数返回值/snowboardflowbindings.com/testing/testfile.php on line 25

解决方案

你有类似于 issent()在你的php中缺少() in你的jQuery ...尝试将代码分解成更易于管理的部分,并尝试保持尽可能多的逻辑,以便与视图分离:

 <?php 
function validate_input($ var = false,$ opt = false)
{
$ var = trim($ var,ENT_QUOTES);
回报($ opt!='email')? htmlspecialchars($ var):filter_var($ var,FILTER_VALIDATE_EMAIL);
}

函数error_codes($ code = false)
{
$ valid ['name'] =输入您的名字;
$ valid ['subject'] =写一个主题;
$ valid ['email'] =电子邮件无效;
$ valid ['message'] =写下您的评论;

return(isset($ valid [$ code]))? $ valid [$ code]:false;


函数send_email($ settings = false)
{
$ to =(!empty($ settings ['to']))? $设置[到]:假的;
$ from =(!empty($ settings ['from']))? 。 来源:[从] $设置PHP_EOL: no-replay@test.com;
$ subject =(!empty($ settings ['subject']))? $ settings ['subject']:这是一个很棒的主题;
$ message =(!empty($ settings ['message']))? $设置[消息]:假;
$ headers =(!empty($ settings ['headers']))? $设置[标题]:假的;

if(in_array(false,$ settings)=== true)
return false;

return(mail($ to,$ subject,$ message,$ headers));


// ISSET not ISSENT
if(isset($ _ POST ['submitform'])){
$ payload ['name'] = validate_input( $ _POST [ '名称']);
$ payload ['email'] = validate_input($ _ POST ['email'],'email');
$ payload ['message'] = validate_input($ _ POST ['message']);


print_r($ payload);
print_r($ _ POST);

if(in_array(false,$ payload)=== true){
foreach($ payload as $ key => $ err)
$ errors [$ key] = error_codes($ key);
}

if(empty($ errors)){
$ to =samuelnico.norton@gmail.com;
$ subject =来自联系表单的消息;
$ msgcontents =Name:。$ payload ['name']。< br />>电子邮件:。$ payload ['email']。< br /> Message:。 $有效载荷[ '消息'];
$ headers =MIME-Version:1.0 \r\\\
;
$ headers。=Content-type:text / html; charset = iso-8859-1 \r\\\
;
$ headers。=From:。$ payload ['name']。<。$ payload ['email']。> \r\\\
;

$ success =(send_email(array(to=> $ to,subject=> $ subject,message=> $ msgcontents,headers=> $头)));
}
}
print_r($ errors);
?><!doctype html>
< html>
< head>
< meta charset =UTF-8>
< meta name =viewportcontent =width = device-width,height = device-height,initial-scale = 1.0,user-scalable = no>

<! - - 页面标题 - >
< title> TEST FILE< / title>

<! - 样式 - >
< link rel =stylesheethref =css / slides.cssname =main-styles>

<! - 脚本 - >
< script src =http://code.jquery.com/jquery-1.11.3.min.jsname =jquery>< / script>
< script src =js / plugins.jstype =text / javascriptname =plugins>< / script>
< script src =js / slides.jstype =text / javascriptname =main-scripts>< / script>

<! - 为联络表格增加脚本 - >
< script src =https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.jstype =text / javascriptname =主脚本>< /脚本>
< script type =text / javascript>
jQuery(document).ready(function($){
$(#contactform)。validate({
rules:{
name:{
required :true,
minlength:2
},
email:{
required:true,
email:true
},
message: {
required:true,
minlength:50
}
},
消息:{
name:{
required:Please enter你的名字,
minlength:你的名字似乎有点不对吗?
},
电子邮件:{
required:请输入你的电子邮件地址,
电子邮件:请输入一个有效的电子邮件地址
},
消息:{
required:请输入您的消息,
minlength:您的消息似乎有点短不是吗?请输入至少50个字符

}
});
});

< / script>
< / head>
< body class =slides animated>

<! - Sidebar - >
< nav class =sidebar deepPurple
< use xmlns:xlink =http://www.w3 .org / 1999 / xlinkxlink:href =#close>< / use>
< / svg>
< / div>
< div class =内容>
< a href =#class =logo>
< svg width =120height =50>
< use xmlns :xlink =http://www.w3.org/1999/xlinkxlink:href =#logo>< / use>
< / svg>
< / a> ;
< ul class =mainMenu uppercase>
< li>< a href =#> Tour< / a>< / li>
< li>< a href =#>升级< / a>< / li>
< li>< a href =#>帮助< / a>< / li>
< li>< a href =#>浏览< / a>< / li>
< / ul>
< ul class =social>
< li>< a href =#>
< svg>
< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =#twitter>< / use>
< / svg>
< / a>
< / li>
< li>< a href =#>
< svg>
< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =#facebook>< / use>
< / svg>
< / a>
< / li>
< li>< a href =#>
< svg>
< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =#googlePlus>< / use>
< / svg>
< / a>
< / li>
< li>< a href =#>
< svg>
< use xmlns:xlink =http://www.w3.org/1999/xlinkxlink:href =#pinterest>< / use>
< / svg>
< / a>
< / li>
< / ul>
< / div>
< / nav>

<! - Slide#55 - >
< section class =slide fade kenBurns>
< div class =content>
< div class =container>
< div class =wrap>
< div class =fix-6-12 box-55>
< div class =pad shadow selected ae-3>
<?php if(empty($ success)){?>
< form class =wide centerid =contactformmethod =postaction =<?php echo $ _SERVER ['PHP_SELF'];?> NOVALIDATE>
<?php
if(!empty($ success))
echo< h2 class ='success'>谢谢,您的邮件已成功发送< / h2> ;

if(!empty($ errors)){
echo'< ul clas =errorlist>';
echo< li>。implode(< / li>。PHP_EOL。< li>,$ errors)。< / li>;
echo'< / ul>';
}
?>
< label class =uppercase ae-4for =name37>您的名字< / label>
< input class =stroke round ae-5 wideid =name37type =namename =namevalue =placeholder =Namerequired />
< label class =uppercase ae-6for =email37>电子邮件< / label>
< input class =stroke round ae-7 wideid =email37type =emailname =emailvalue =placeholder =Emailrequired />
< label class =uppercase ae-8for =message37> Message< / label>