php - How to insert data into database only when input are not empty? -
i came problem made me crazy new php. problem is: below timetable submission form works on chrome (every time left email unfilled, submission cannot processed). however, when using on safari, can submit blank form database.
here html form.
<script type="text/javascript" src="/membership/my-form/js/jquery-2.1.1.js"></script> <script type="text/javascript" src="/membership/my-form/js/main.js"></script> <form class="mf-form floating-labels" method="post" action="timetablesubmit.php"> <div> <h1 style="text-align:center">availability form</h1> </div> <div> <p class="mf-select icon"> <select name="timetable-staff" class="user" required> <option value="">select person</option> <option value="amy">amy</option> <option value="tom">tom</option> </select> </p> <div> <input name="location1" value="a" type="hidden"> <input name="location2" value="b" type="hidden"> </div> <div class="amy box">you work in a.</div> <div class="tom box">you work in b.</div> </div> <div class="icon"> <label class="mf-label" for="mf-email">email address</label> <input class="email" type="email" name="timetable-email" id="mf-email" required> </div> </form> <script type="text/javascript"> $(document).ready(function(){ $("select").change(function(){ $( "select option:selected").each(function(){ if($(this).attr("value")=="amy"){ $(".box").hide(); $(".amy").show(); } if($(this).attr("value")=="tom"){ $(".box").hide(); $(".tom").show(); } }); }).change(); }); </script> <style type="text/css"> .box{ padding: 10px; display: none; margin-top: 20px; border: 1px solid #000; font-size:1.6em; text-align:center; background-color: #f1f1f1; } </style>
here timetablesubmit.php:
<?php header("content-type:text/html;charset=utf-8"); session_start(); $timesubmit=date('y-m-d h:i:s'); $staff=$_post['timetable-staff']; $email=$_post['timetable-email']; $con=mysql_connect("localhost","database","password"); if (!$con) { die ('could not connect:' . mysql_error()); } mysql_select_db("database", $con); mysql_query("set names utf8"); mysql_query("insert timetable(staff,email) values('$staff','$email')"); mysql_close($con); sleep(2); ?> <html> <? require 'header.php'; ?> <div class="tk-reg"> <p>thak <?php echo $_post['timetable-staff']; ?><p> <p> time availability submitted successfully.<p> email address is: <?php echo $_post["timetable-email"]; ?> </div> <div class="tk-regfollow"> <ul style="text-align:center"> <a href="/membership/index.php">back home page.</a> </ul> </div> </html>
then searched internet , changed below, still not working on safari (the alert appears, data still been insert database.
<?php require 'header.php'; $nameerr = $emailerr = ""; $name = $email = ""; if ($_server["request_method"] == "post") { if (empty($_post["timetable-staff"])) { $nameerr = "please select staff."; } else { $staff = test_input($_post["timetable-staff"]); } if (empty($_post["timetable-email"])) { $emailerr = "email address required"; } else { $email = test_input($_post["timetable-email"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } $con=mysql_connect("localhost","database_name","database_password"); if (!$con) { die ('could not connect:' . mysql_error()); } mysql_select_db("database_name", $con); mysql_query("set names utf8"); mysql_query("insert timetable(staff,email) values('$staff','$email')"); mysql_close($con); sleep(2); ?> <html> <style> .error {color: #ff0000;} </style> <h1 style="text-align:center">availability form <?php $d=strtotime("+1 months"); echo date("m", $d) . " 2015 <br>"; ?> </h1> <form class="mf-form floating-labels" method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>"> <div> <p class="mf-select icon"> <select name="timetable-staff" class="user" required> <option value="">select person</option> <option value="amy">amy</option> <option value="tom">tom</option> </select> <span class="error">* <?php echo $nameerr;?></span> </p> </div> <div class="icon"> <label class="mf-label" for="mf-email">email address</label> <input class="email" type="email" name="timetable-email" id="mf-email" required> <span class="error">* <?php echo $emailerr;?></span> </div> <br><br> <input type="submit" name="submit" value="submit"> </form> </html>
i hope me pointing out mistakes. thank you.
======================================================================
i tried lot of ways figure out, still have either 'this' or 'that' problems. use method work, not know whether not recommended.
here code modified in timetablesubmit.php (my flow is: timetable.php ==>timetablesubmit.php==>welcome.php)
$email = trim($_post['timetable-email']); if($email !='' , $email !=null) { $sql1; } else { echo '<html><head> <link rel="stylesheet" type="text/css" href="/membership/style.css"/> <head> <div class="check-error-message"> <p>error: email address required.</p><br> <a href="javascript:history.back(-1);">return</a> </div>'; exit; };
you can check condition before insert operation
if($nameerr =="" && $emailerr == "") { mysql_query("insert timetable(staff,email) values('$staff','$email')"); }
Comments
Post a Comment