lundi 20 avril 2015

PHP/HTML form data validation and views

I have three php files one is the controller "index.php" one is for displaying output "people.php" and the other is a form for user input "form.php"

form.php looks like this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://ift.tt/nYkKzf">
<html>

<form action = "" method = "post">

        First name: <input name ="first_name" type = "test" size = "55" maxlength = "55"><p>
        Second name: <input name ="second_name" type = "text" size = "55" maxlength = "55"><p>
        
        


Month: <select name="month">
                <option value="">select month</option>
                <option value="January">January</option>
                <option value="February">February</option>
                <option value="March">March</option><p>
                <option value="April">April</option><p>
                <option value="May">May</option><p>
                <option value="June">June</option><p>
                <option value="July">July</option><p>
                <option value="August">August</option><p>
                <option value="September">September</option><p>
                <option value="October">October</option><p>
                <option value="November">November</option><p>
                <option value="December">December</option><p>
        </select><p>

Year:   <select name ="year">
                <option value = ""> -select year-</option>
                <option value = "2015"> 2015 </option>
                <option value = "2016"> 2016 </option>
                <option value = "2017"> 2017 </option>
        </select><p>

        Date of birth:<input name="date_of_birth" input type="text" id="datepicker"></p>
                        
                        
<input name = "add_person" type="submit" value = "submit">
</form>
</html>

Index.php looks like this

<?php

        if (!isset($_POST['add_person'])){
        include 'form.php';
        }

        elseif (isset($_POST['add_person']) && empty($_POST['first_name']) && empty($_POST['second_name']) && empty($_POST['month']) && empty($_POST['year']) && empty($_POST['date_of_birth'])){
        
        include 'form.php';
        
        }

        elseif (isset($_POST['add_person'])){
        
        if (empty($_POST['first_name'])){
                exit("First name cannot be left empty");
                }

        if(empty($_POST['second_name'])){
                exit("Second name cannot be left empty");
                }

        if(empty($_POST['month'])){
                exit("month cannot be left empty");
                }
        
        if(empty($_POST['year'])){
                exit("year cannot be left empty");
                }

        if(empty($_POST['date_of_birth'])){
                exit("date of birth cannot be left empty");
                }

        $date1 = $_POST['date_of_birth'];
        $date2 = date("Y-m-d");
                
        if($date2 < $date1){
                exit("Invalid date: Date of birth cannot be in the future");
        }


        $authors = $_POST['first_name'];
        $title = $_POST['second_name'];
        $month = $_POST['month'];
        $year = $_POST['year'];


echo "$first_name <br> $second_name <br> $month <br> $year <br> $date1";
?>
}


else{
include "form.php";
}
?>

and people.php looks like this

<html>
<head>
<title></title>

<script>

<!--jquery imebidi(necessary evil)....jquery copy-pasted from here http://ift.tt/1sUhvPK -->
        
        $(function() {
                var date = $('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
        });

  </script>
</head>

<body>
        <div id="header">
                <div class ="center">
                <p>Manuscript management information system</p>
        </div>
                </div>

<div id="content-wrapper">
        <div id="content-main">

                <p></p>

<?php
include 'index.php';

?>


        <p>
        </div>
        <div id="content-secondary">
                <p>mmmmmmmmmmmmm</p>
        </div>
</div>
<div id="sidebar">
        
        <li><a href="Recently recruited">Recently published</a></li>
        <li><a href="awaiting certificates">Submitted to PubCom</a></li>
        <li><a href="Not yet submitted"></a></li>
        
</div>
<div id="footer"><p>Welcome</p>

</div>
</body>
</html>

What i'm trying to achieve is to have the error message displayed on the form without having to be transferred to a new page. I mean, if a user fails to enter first name then he/she should get an error on the same form

Aucun commentaire:

Enregistrer un commentaire