html - Undefined Variable on this tutorial on PHP with Bootstrap -
please me fix undefined variable copy , pasted code on create.php , when hit create button on home page this:
notice: undefined variable: fnameerror in c:\xampp\htdocs\testcrud\create.php on line 70
notice: undefined variable: lnameerror in c:\xampp\htdocs\testcrud\create.php on line 75
notice: undefined variable: ageerror in c:\xampp\htdocs\testcrud\create.php on line 80
notice: undefined variable: gendererror in c:\xampp\htdocs\testcrud\create.php on line 89
<?php if ( !empty($_post)) { require 'db.php'; // validation errors $fnameerror = null; $lnameerror = null; $ageerror = null; $gendererror = null; // post values $fname = $_post['fname']; $lname = $_post['lname']; $age = $_post['age']; $gender = $_post['gender']; // validate input $valid = true; if(empty($fname)) { $fnameerror = 'please enter first name'; $valid = false; } if(empty($lname)) { $lnameerror = 'please enter last name'; $valid = false; } if(empty($age)) { $ageerror = 'please enter age'; $valid = false; } if(empty($gender)) { $gendererror = 'please select gender'; $valid = false; } // insert data if ($valid) { $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); $sql = "insert users (fname,lname,age,gender) values(?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute(array($fname,$lname,$age,$gender)); $pdo = null; header("location: index.php"); } } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="row"> <h3>create user</h3> </div> <form method="post" action=""> <div class="form-group <?php echo !empty($fnameerror)?'has-error':'';?>"> <label for="inputfname">first name</label> <input type="text" class="form-control" required="required" id="inputfname" value="<?php echo !empty($fname)?$fname:'';?>" name="fname" placeholder="first name"> <span class="help-block"><?php echo $fnameerror;?></span> </div> <div class="form-group <?php echo !empty($lnameerror)?'has-error':'';?>"> <label for="inputlname">last name</label> <input type="text" class="form-control" required="required" id="inputlname" value="<?php echo !empty($lname)?$lname:'';?>" name="lname" placeholder="last name"> <span class="help-block"><?php echo $lnameerror;?></span> </div> <div class="form-group <?php echo !empty($ageerror)?'has-error':'';?>"> <label for="inputage">age</label> <input type="number" required="required" class="form-control" id="inputage" value="<?php echo !empty($age)?$age:'';?>" name="age" placeholder="age"> <span class="help-block"><?php echo $ageerror;?></span> </div> <div class="form-group <?php echo !empty($gendererror)?'has-error':'';?>"> <label for="inputgender">gender</label> <select class="form-control" required="required" id="inputgender" name="gender" > <option></option> <option value="male" <?php echo $gender == 'male'?'selected':'';?>>male</option> <option value="female" <?php echo $gender == 'female'?'selected':'';?>>female</option> </select> <span class="help-block"><?php echo $gendererror;?></span> </div> <div class="form-actions"> <button type="submit" class="btn btn-success">create</button> <a class="btn btn-default" href="index.php">back</a> </div> </form> </div> <!-- /row --> </div> <!-- /container --> </body> </html>
you're displaying errors when variables haven't been set. brings notice see. can avoid them checking if set first using isset
or empty
<?= isset($someerror) ? $someerror : '' ?>
Comments
Post a Comment