Rookie PHP, receiving an undefined variable error -
i reading book , going through examples, unfortunately keep getting error if leave field empty when submit form. checked errata , there nothing, tried posting on books forums didn't responses.
i believe maybe have declare variables first take away variables being automatically generated.
any appreciated, rookie trying learn php.
per request, here $required , $expected. thanks!
$expected = array('name', 'email', 'comments'); $required = array('name', 'email', 'comments'); <?php foreach ($_post $key => $value){ //assign temporary variable , strip whitespace if not array $temp = is_array($value) ? $value : trim($value); //if empty , required, add $missing array if (empty($temp) && in_array($key, $required)){ $missing[] = $key; } elseif(in_array($key, $expected)){ //otherwise, assign variable of same name $key ${$key} = $temp; } } <?php include('./includes/title.inc.php'); $errors = array(); $missing = array(); //check see if form has been submitted if (isset($_post['send'])){ //email processing script $to = 'drewdin1@hotmail.com'; //use email address $subject = 'feedback japan journey'; //list expecting fields $expected = array('name', 'email', 'comments'); //set required fields $required = array('name', 'email', 'comments'); include('./includes/processmail.inc.php'); } ?> <!doctype html> <html> <head> <meta charset=utf-8"> <title>japan journey<?php if (isset($title)){echo "—{$title}";} ?></title> <link href="styles/journey.css" rel="stylesheet" type="text/css" media="screen"> </head> <body> <div id="header"> <h1>japan journey</h1> </div> <div id="wrapper"> <?php include('./includes/menu.inc.php'); ?> <div id="maincontent"> <h2>contact us</h2> <?php if ($missing || $errors){ ?> <p class="warning">please fix item(s) indicated.</p> <?php } ?> <p>ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. mollit anim id est laborum. sunt in culpa duis aute irure dolor excepteur sint occaecat.</p> <form id="feedback" method="post" action=""> <p> <label for="name">name: <?php if ($missing && in_array('name', $missing)){ ?> <span class="warning">please enter name</span> <?php } ?> </label> <input name="name" id="name" type="text" class="formbox"<?php if ($missing || $errors){ echo ' value="' . htmlentities($name, ent_compat, 'utf-8') . '" '; } ?> /> </p> <p> <label for="email">email: <?php if ($missing && in_array('email', $missing)){ ?> <span class="warning">please enter email address</span> <?php } ?> </label> <input name="email" id="email" type="text" class="formbox"<?php if ($missing || $errors){ echo ' value="' . htmlentities($email, ent_compat, 'utf-8') . '" '; } ?> /> </p> <p> <label for="comments">comments: <?php if ($missing && in_array('comments', $missing)){ ?> <span class="warning">please enter comments</span> <?php } ?> </label> <textarea name="comments" id="comments" cols="60" rows="8"><?php if ($missing || $errors){ echo htmlentities($comments, ent_compat, 'utf-8'); } ?></textarea> </p> <p> <input name="send" id="send" type="submit" value="send message"> </p> </form> <pre> <?php if ($_post && $missing) {print_r($_post);} ?> </pre> </div> <?php include('./includes/footer.inc.php'); ?> </div> </body> </html>
quite simple, email entry empty in request, variable $email never gets created perform assignment if $_request[$key] not empty.
besides, suggest not create variables based on untrusted $_request, open serious security holes. simple example: call form url , append ?subject=damn
Comments
Post a Comment