I'm a noob, and need help with PHP Script

Undefined variable lines 13, 19, 26, and 33 shows up when I load the page, and I have no idea why. Thank you in advance for your help.

[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 = "[email protected]"; $subject = "From $myname "; $message = "$myname has filled out the form."; $replyto = "From: $email \r\n". "Reply-To: [email protected] \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 ?> [/php]

because at the beginning of the script you have this:

[php]if (isset($_POST[‘myname’])) { $myname = $_POST[‘myname’]; }
if (isset($_POST[‘phone’])) {$phone = $_POST[‘phone’]; }
if (isset($_POST[‘email’])) {$email = $_POST[‘email’]; }[/php]

what that does is IF it is passed the name, phone and email it will set those variables. if nothing is passed they don’t get set, and are therefore undefined.

first, know that the real problem is you are supposed to be opening this script via a form action, and not directly. simply find the other half and you’re set. but to throw a little error handling in there to alert you instead of just throwing that message, change the above code to this:
[php]if (isset($_POST[‘myname’])) { $myname = $_POST[‘myname’]; } else { die(‘You forgot the name’);}
if (isset($_POST[‘phone’])) {$phone = $_POST[‘phone’]; } else { die(‘You forgot the phone number’);}
if (isset($_POST[‘email’])) {$email = $_POST[‘email’]; } else { die(‘You forgot the email address’);}[/php]

that way it will die with an error to let you know the information wasn’t passed. if for some reason you want to ignore it when it’s empty you can do that, but that makes no sense with such minimal information needed.

Thank you for your answer digibucc, but that didn’t work :frowning:
Also, it is supposed to be undefined variable on line 15, 21, 28, and 35.

It doesn’t know that the form has been submitted because you’re ending the if to soon. Move that first endif to the bottom of the section, so its covering the validation.

Thank you richei. I tried that, and it does get rid of the unidentified variable problem, but then the error messages and “Thanks for filling out the form” message no longer show up when I don’t fill out the form properly. This is how I have the PHP within the form [php] <?php if (isset($msg)) { echo '

', $msg , '

'; } ?>
<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>
[/php]

Oh Yeah, and of course I have: [php] <?php include "myprocess.php"; ?> [/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service