提前感谢所有人:
当我测试表单页面时,它会为$ myname加载错误“undefined variable lines ....”和对应于前四个if / else语句的$ email。
当我将第一个endif移动到脚本的底部时,未定义的变量消失了,但是当我没有正确填写表单时,错误消息不再显示,并且“感谢您填写表单“当我这样做时,消息不再出现:(
另外,当我在顶部有第一个endif时,错误消息“你的名字必须采用以下格式:最后,第一个”和“你的电子邮件必须是正确的格式”已经存在,直到我触摸表格。
好的,这是我在单独文件中的表单和PHP脚本:
<?php if (isset($msg)) { echo '<div id = "thanks"/><p>', $msg , '</p></div>'; } ?>
<form id="myform" name="theform" class="group" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<span id="firstname"> Name (Last, First):</span> <span id="firstnamefield">
<input type="text" name="myname" size="50" value:"<?php if (isset($myname)) {echo $myname;} ?>"/></span>
<?php if (isset($err_myname)) { echo '<div id = "error"/><p>', $err_myname , '</p></div>'; } ?>
<?php if (isset($err_namematch)) { echo '<div id = "namematch"/><p>', $err_namematch , '</p></div>'; } ?>
<span id="mail">Email:</span> <span id="emailfield"><input type="text" name="email" value="<?php if (isset($email)) {echo $email;} ?>"/></span>
<?php if (isset($err_email)) { echo '<div id = "emailerror"/><p>', $err_email , '</p></div>'; } ?>
<?php if (isset($err_patternmatch)) { echo '<div id = "emailmatch"/><p>', $err_patternmatch , '</p></div>'; } ?>
<span id="telephone">Phone:</span><span id="phonefield"><input type="text" name="phone" value="<?php if (isset($phone)) {echo $phone;} ?>"/></span>
<span id="sub"><input type="image" name="action" value="submit" src="images/submit.png" alt="Submit" /></span>
</form>
这就是我在PHP文件中的内容:
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
endif; //form submitted
if (isset($_POST['myname'])) { $myname = $_POST['myname']; }
if (isset($_POST['phone'])) {$phone = $_POST['phone']; }
if (isset($_POST['email'])) {$email = $_POST['email']; }
$formerrors = false;
if ($myname ===''):
$err_myname = "*Sorry, your name is a required field";
$formerrors = true;
endif; //Input Field Empty
if ($email ===''):
$err_email = "*Sorry, your email is a required field";
$formerrors = true;
endif; //Input Field Empty
if ( !(preg_match('/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a- zA-Z]\.)+[a-zA-Z]{2,9})$/', $email)) ):
$err_patternmatch = "*Your email must be in the correct format";
$formerrors = true;
endif; //Input Must Match Proper Format
if ( !(preg_match('/[A-Za-z]+, [A-Za-z]+/', $myname))):
$err_namematch = "*Your name must be in the format: Last, First";
$formerrors =true;
endif; //Input Must Match Proper Format
if (!($formerrors)):
$to = "test1234@yahoo.com";
$subject = "From $myname --Postcard Landing Page";
$message = "You have a new lead.";
$replyto = "From: $email \r\n".
"Reply-To: donotreply@domain.com \r\n";
if (mail($to,$subject,$message)):
$msg = "Thank you for filling out the form!";
else:
$msg = "Problem sending message";
endif; // mail form data
endif; // Check for For Errors
?>
答案 0 :(得分:1)
问题出在这一部分:
if (isset($_POST['myname'])) { $myname = $_POST['myname']; }
if (isset($_POST['phone'])) {$phone = $_POST['phone']; }
if (isset($_POST['email'])) {$email = $_POST['email']; }
仅在设置了这些值时才设置变量。现在,如果表单提交无效,代码执行不会发生在if块内,并且在没有设置变量的情况下继续执行下一部分。
以下是解决问题的方法。在读取$ _POST值之前,在脚本开头将所有变量初始化为'':
$myname = ''
$phone = ''
$email = ''