简体   繁体   English

我的表单的PHP脚本的Noob问题

[英]Noob Problems with PHP Script for My Form

Thank you in advance to all: 预先感谢大家:

When I test the form page it loads with an error "undefined variable lines...." for $myname and $email corresponding to the first four if/else statements. 当我测试表单页面时,它会为$ myname和$ email加载与前四个if / else语句相对应的错误“ undefined variable lines ....”。

When I move the first endif to the bottom of the script the undefined variables go away, but then the error messages no longer show up when I don't fill out the form properly, and the "Thanks for filling out the form" message no longer shows up when I do :( 当我将第一个endif移到脚本的底部时,未定义的变量消失了,但是当我没有正确填写表单时,错误消息不再显示,并且“感谢您填写表单”消息否当我这样做时会更长

Also, when I have the first endif at the top the error messages "Your name must be in the format: Last, first" and "your email must be in the proper format" are already present before I even touch the form. 另外,当我在顶部使用第一个endif时,甚至在触摸表格之前,都会出现错误消息“您的姓名必须采用以下格式:姓氏,第一个”和“您的电子邮件必须采用正确的格式”。

Ok so this is the form and PHP script that I have on a separate file: 好的,这是我在单独文件中拥有的表单和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> 

This is what I have on my PHP file: 这就是我的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



?>

The problem is in this part: 问题出在这部分:

if (isset($_POST['myname'])) { $myname = $_POST['myname']; } 
if (isset($_POST['phone'])) {$phone = $_POST['phone']; }  
if (isset($_POST['email'])) {$email = $_POST['email']; } 

You are setting the variables only if these values are set. 仅在设置了这些值的情况下才设置变量。 Now, in case of invalid form submission, the code execution does not happen inside the if block and continues to the next part without your variables being set. 现在,在表单提交无效的情况下,代码执行不会在if块内进行,而是继续进行下一部分而不设置变量。

Here's how you can fix it. 解决方法如下。 Initialize all variables to '' in the beginning of the script before you read the $_POST values: 在读取$ _POST值之前,请在脚本开头将所有变量初始化为'':

$myname = ''
$phone = ''
$email = ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM