php - Customized validation messages not working? -
am trying add custom validation messages form, cannot figure out life of me.. help/advice appreciated...thank you.
i have tried few tutorials, nothing seems clicking me, sure simple, cant seem figure out.
thanks
form validation:
<?php if (isset($_post['insert'])) { require_once('connection.php'); $ok = false; $sql = 'insert students (studenttitle, studentfirstname, studentlastname) values(:studenttitle, :studentfirstname, :studentlastname)'; $stmt = $conn->prepare($sql); $stmt->bindparam(':studenttitle', $_post['studenttitle'], pdo::param_str); $stmt->bindparam(':studentfirstname', $_post['studentfirstname'], pdo::param_str); $stmt->bindparam(':studentlastname', $_post['studentlastname'], pdo::param_str); $stmt->execute(); $ok = $stmt->rowcount(); if ($ok) { header('location: http://localhost/mysqlquiz/student.php'); exit; } else { $error = $stmt->errorinfo(); if (isset($error[2])) { $error = $error[2]; } } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>add student details</title> <link href="css/style.css" rel="stylesheet" type="text/css"> </head> <body> <h1 class="header">new student details</h1> <p><a href="student.php">student listing </a></p> <?php if (isset($error)) { echo "<p class='warning'>error: $error</p>"; } ?> <?php // define variables , set empty values $studenttitleerr = $studentfirstnameerr = $studentlastnameerr = ""; $studenttitle = $studentfirstname = $studentlastname = ""; if ($_server["request_method"] == "post") { if (empty($_post["studenttitle"])) { $studenttitleerr = "a title required"; } else { $studenttitle = test_input($_post["studenttitle"]); } if (empty($_post["studentfirstname"])) { $studentfirstnameerr = "first name required"; } else { $studentfirstname = test_input($_post["studentfirstname"]); } if (empty($_post["studentlastname"])) { $studentlastnameerr = "last name required"; } else { $studentlastname = test_input($_post["studentlastname"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <form method="post" action="<?php echo htmlspecialchars($_server["php_self"]); ?>"> <p> <label for="studenttitle">title:</label> <select name="studenttitle" id="studenttitle" ><span class="error">* <?php echo $studenttitleerr;?></span> <option value="mr">mr.</option> <option value="mrs">mrs.</option> <option value="ms">ms.</option> <option value="miss">miss.</option> </select> </p> <p> <label for="studentfirstname">first name:</label> <input type="text" name="studentfirstname" id="studentfirstname" ><span class="error">* <?php echo $studentfirstnameerr;?></span> </p> <p> <label for="studentlastname">last name:</label> <input type="text" name="studentlastname" id="studentlastname" ><span class="error">* <?php echo $studentlastnameerr;?></span> </p> <p> <input type="submit" name="insert" value="add details" id="insert"> <input type="reset" name="clear" value="clear" id="clear"> <input name="studentid" type="hidden" value="<?php echo $studentid; ?>"> </p> </form> </body> </html>
i have updated code. should check validation first insert.
<?php // define variables , set empty values $studenttitleerr = $studentfirstnameerr = $studentlastnameerr = ""; $studenttitle = $studentfirstname = $studentlastname = ""; if (isset($_post['insert'])) { require_once('connection.php'); $error = $ok = false; if (empty($_post["studenttitle"])) { $studenttitleerr = "a title required"; $error = true; } else { $studenttitle = test_input($_post["studenttitle"]); } if (empty($_post["studentfirstname"])) { $studentfirstnameerr = "first name required"; $error = true; } else { $studentfirstname = test_input($_post["studentfirstname"]); } if (empty($_post["studentlastname"])) { $studentlastnameerr = "last name required"; $error = true; } else { $studentlastname = test_input($_post["studentlastname"]); } if($error == false) { $sql = 'insert students (studenttitle, studentfirstname, studentlastname) values(:studenttitle, :studentfirstname, :studentlastname)'; $stmt = $conn->prepare($sql); $stmt->bindparam(':studenttitle', $_post['studenttitle'], pdo::param_str); $stmt->bindparam(':studentfirstname', $_post['studentfirstname'], pdo::param_str); $stmt->bindparam(':studentlastname', $_post['studentlastname'], pdo::param_str); $stmt->execute(); $ok = $stmt->rowcount(); if ($ok) { header('location: http://localhost/mysqlquiz/student.php'); exit; } else { $error = $stmt->errorinfo(); if (isset($error[2])) { $error = $error[2]; } } } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>add student details</title> <link href="css/style.css" rel="stylesheet" type="text/css"> </head> <body> <h1 class="header">new student details</h1> <p><a href="student.php">student listing </a></p> <?php if (isset($error)) { echo "<p class='warning'>error: $error</p>"; } ?> <?php function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <form method="post" action="<?php echo htmlspecialchars($_server["php_self"]); ?>"> <p> <label for="studenttitle">title:</label> <select name="studenttitle" id="studenttitle" ><span class="error">* <?php echo $studenttitleerr;?></span> <option value="mr">mr.</option> <option value="mrs">mrs.</option> <option value="ms">ms.</option> <option value="miss">miss.</option> </select> </p> <p> <label for="studentfirstname">first name:</label> <input type="text" name="studentfirstname" id="studentfirstname" ><span class="error">* <?php echo $studentfirstnameerr;?></span> </p> <p> <label for="studentlastname">last name:</label> <input type="text" name="studentlastname" id="studentlastname" ><span class="error">* <?php echo $studentlastnameerr;?></span> </p> <p> <input type="submit" name="insert" value="add details" id="insert"> <input type="reset" name="clear" value="clear" id="clear"> <input name="studentid" type="hidden" value="<?php echo $studentid; ?>"> </p> </form> </body> </html>
Comments
Post a Comment