Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation form with PHP and Database
    text
    copied!<p>I need to validate a form with database and empty field. I've made a form with empty field filter, so if user skips to fill a field, it will be redirected back and user just put in the one empty field before. I need to check with the database if it already exists or not, and if not the user must fill all the fields. </p> <p>Here's the code</p> <pre><code>&lt;?php session_start(); if(isset($_SESSION['error'])) { $error = $_SESSION['error']; $_POST = $_SESSION['post']; unset($_SESSION['error']); unset($_SESSION['post']); } ?&gt; &lt;head&gt; &lt;script type="text/javascript" src="jquery-1.5.1.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="jquery-ui-1.8.11.custom.min.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" type="text/css" href="jquery-ui-1.8.11.custom.css"&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $("#date").datepicker( { dateFormat: "dd-mm-yy", changeMonth: true, changeYear: true, yearRange: "-0:+100" }); }); &lt;/script&gt; &lt;/head&gt; &lt;form name="form" method="post" action="process.php"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Name:&lt;/td&gt; &lt;td&gt; &lt;input name="name" id="name" type="text" value="&lt;?php echo isset($_POST['name']) ? $_POST['name'] : '';?&gt;"/&gt; &lt;div style="color:red"&gt;&lt;?php echo isset($error['name']) ? $error['name'] : '';?&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Phone:&lt;/td&gt; &lt;td&gt; &lt;input name="phone" id="phone" type="text" value="&lt;?php echo isset($_POST['phone']) ? $_POST['phone'] : '';?&gt;"/&gt; &lt;div style="color:red"&gt;&lt;?php echo isset($error['phone']) ? $error['phone'] : '';?&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Date:&lt;/td&gt; &lt;td&gt; &lt;input name="date" id="date" type="text" value="&lt;?php echo isset($_POST['date']) ? $_POST['date'] : '';?&gt;"/&gt; &lt;div style="color:red"&gt;&lt;?php echo isset($error['date']) ? $error['date'] : '';?&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Time:&lt;/td&gt; &lt;td&gt; &lt;input name="time" id="time" type="text" value="&lt;?php echo isset($_POST['time']) ? $_POST['time'] : '';?&gt;"/&gt; &lt;div style="color:red"&gt;&lt;?php echo isset($error['time']) ? $error['time'] : '';?&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input name="submit" id="submit" type="Submit" value="Submit" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>And this is php process file</p> <pre><code>&lt;?php session_start(); if($_POST) { $name = $_POST['name']; $phone = $_POST['phone']; $date = $_POST['date']; $time = $_POST['time']; $error = array(); if(empty($name)){ $error['name'] = 'Name must be filled'; } if(empty($phone)){ $error['phone'] = 'Phone Number must be filled'; } if(empty($date)){ $error['date'] = 'Date must be filled'; } if(empty($time)){ $error['time'] = 'Time must be filled'; } if(empty($error)) { $check = mysql_query("SELECT * FROM event WHERE event='$time' AND date='$date'"); $in = mysql_num_rows($check); if($in &gt; 0) { $error['date'] = 'Select other date'; echo '&lt;script type="text/javascript"&gt;'; echo 'alert("Sorry, it is already filled\n Please select the other date");'; echo 'window.location.href="cal.php";'; echo '&lt;/script&gt;'; } else { //insert into database } }else { $_SESSION['error'] = $error; $_SESSION['post'] = $_POST; header("location: cal.php"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload