简体   繁体   中英

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.

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 :(

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.

Ok so this is the form and PHP script that I have on a separate file:

<?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 
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.

Here's how you can fix it. Initialize all variables to '' in the beginning of the script before you read the $_POST values:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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