Hi all,i need help with validating php form..I got 3 php files.How do i show the error messages beside the text input..Here are the codes
that i have made.Thanks..

output_fns.php

<?php
function do_kepala($title){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?php echo $title ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="include/style.css" type="text/css">
</head>
<body>
<?php
};
function do_body()
{
?>

<br>
<?php
};
function do_footer()
{
?>
 
</body>
</html>
<?php
};
function display_login_form()
{
?>
<div id="stylized" class="myform">
  <form id="form1" name="form1" method="post" action="reg.php">
    <fieldset>
    <legend >Data keanggotaan</legend>
    <br>
    <label>Email anda
        <span class="small">Jadi login id anda</span>
    </label>
    <input type="text" name="email" id="email" />
    <?php $_SESSION['var']; ?>
    <label>Password
        <span class="small">6-12 karakter</span>
    </label>
    <input type="password" name="pwd1" id="password" />
    <div class="spacer"></div>
    <label>Ulangi Password
        <span class="small">6-12 karakter</span>
    </label>
    <input type="password" name="pwd2" id="password" />
    <div class="spacer"></div>
    </fieldset>
    <fieldset>
    <legend>Data pribadi</legend>
    <br>
    <label>Nama 
    <span class="small">Nama lengkap anda</span>
    </label>
    <input type="text" name="nama" id="nama"/>
    <div class="spacer"></div>
    <label for="tgl">Tanggal lahir
    <span class="small">dd/mm/yyyy</span>
    </label>
    <select name="tgl" id="tgl">
        <option value="1" selected="selected">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
        <option value="14">14</option>
        <option value="15">15</option>
        <option value="16">16</option>
        <option value="17">17</option>
        <option value="18">18</option>
        <option value="19">19</option>
        <option value="20">20</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
        <option value="29">29</option>
        <option value="30">30</option>
        <option value="31">31</option>
      </select>
        <label for="bln"></label><select name="bln" id="bln">
          
	  <option value="1" selected="selected">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
        </select>
        <label for="thn"></label>
	<select name="thn" id="thn">
	  <option value="2000" selected="selected">2000</option>
          <option value="1999">1999</option>
          <option value="1998">1998</option>
          <option value="1997">1997</option>
          <option value="1996">1996</option>
          <option value="1995">1995</option>
          <option value="1994">1994</option>
          <option value="1993">1993</option>
          <option value="1992">1992</option>
          <option value="1991">1991</option>
          <option value="1990">1990</option>
          <option value="1989">1989</option>
          <option value="1988">1988</option>
          <option value="1987">1987</option>
          <option value="1986">1986</option>
          <option value="1985">1985</option>
          <option value="1984">1984</option>
          <option value="1983">1983</option>
          <option value="1982">1982</option>
          <option value="1981">1981</option>
          <option value="1980">1980</option>
          <option value="1979">1979</option>
          <option value="1978">1978</option>
          <option value="1977">1977</option>
          <option value="1976">1976</option>
          <option value="1975">1975</option>
          <option value="1974">1974</option>
          <option value="1973">1973</option>
          <option value="1972">1972</option>
          <option value="1971">1971</option>
          <option value="1970">1970</option>
          <option value="1969">1969</option>
          <option value="1968">1968</option>
          <option value="1967">1967</option>
          <option value="1966">1966</option>
          <option value="1965">1965</option>
          <option value="1964">1964</option>
          <option value="1963">1963</option>
          <option value="1962">1962</option>
          <option value="1961">1961</option>
          <option value="1960">1960</option>
        </select>
        <div class="spacer"></div>
        <label>Jenis Kelamin
	<span class="small">Jenis kelamin anda</span>
	</label>
        <select name="jekel">
	 <option value="Pilih" selected="selected">Pilih salah satu... </option>
	 <option value="laki-laki" id="jekel">Laki-laki</option>
	 <option value="perempuan" id="jekel">Perempuan</option>
	 
        </select>
        <div class="spacer"></div>
        <label>Alamat 
        <span class="small">Alamat lengkap anda</span>
        </label>
        <input type="text" name="alamat" id="alamat"/>
        <div class="spacer"></div>
        <label>Kota 
        <span class="small">Kota anda</span>
        </label>
        <input type="text" name="kota" id="kota"/>
        <div class="spacer"></div>
        <label>Propinsi 
        <span class="small">Propinsi anda</span>
        </label>
        <select name="propinsi">
        <option value="Pilih" selected="selected">Pilih salah satu....</option>
	 <option>Nanggroe Aceh Darussalam</option>
	<option>Sumatera Utara</option>
	<option>Sumatera Barat</option>
	<option>Bengkulu</option>
	<option>Riau</option>
	<option>Kepulauan Riau</option>
	<option>Jambi</option>
	<option>Sumatera Selatan</option>
	<option>Lampung</option>
	<option>Kepulauan Bangka Belitung</option>
	<option>DKI Jakarta</option>
	<option>Jawa Barat</option>
	<option>Banten</option>
	<option>Jawa Tengah</option>
	<option>DI Yogyakarta</option>
	<option>Jawa Timur</option>
	<option>Kalimantan Barat</option>
	<option>Kalimantan Tengah</option>
	<option>Kalimantan Selatan</option>
	<option>Kalimantan Timur</option>
	<option>Bali</option>
	<option>Nusa Tenggara Barat</option>
	<option>Nusa Tenggara Timur</option>
	<option>Sulawesi Barat</option>
	<option>Sulawesi Utara</option>
	<option>Sulawesi Tengah</option>
	<option>Sulawesi Selatan</option>
	<option>Sulawesi Tenggara</option>
	<option>Gorontalo</option>
	<option>Maluku</option>
	<option>Maluku Utara</option>
	<option>Papua Barat</option>
	<option>Papua</option>
        </select>
	</fieldset>
    <br>
    <button type="submit">Sign-up</button>
    </form>
</div>
<?php
};
?>

reg.php

<?php 
$email=$_POST['email'];
$pwd1=$_POST['pwd1'];
$pwd2=$_POST['pwd2'];
$tgl=$_POST['tgl'];
$bln=$_POST['bln'];
$thn=$_POST['thn'];
$tgl_lahir=$tgl.'-'.$bln.'-'.$thn;
$jekel=$_POST['jekel'];
$alamat=$_POST['alamat'];
$kota=$_POST['kota'];
$propinsi=$_POST['propinsi'];
?>

register.php

<?php
include('output_fns.php');
do_kepala('Register');
do_body();
display_login_form();
?>

By the way,what i want to do is to validate the email etc,such like if the email field is empty,then it will show errors beside the email textfield..

Member Avatar for diafol

This looks like a case for both client-side and server-side validation. To save time and frustration for your prospective members, you should have js validation to check for errors before the form is sent. Any errors should be noted at the top of the form or on top of /beside the form widget.

Server-side validation (pHp) MUST take place if data is to be passed to DB or even back to HTML. Again, a mechanism should be in place if an error occurs.

This is quite involved and you would do well to get an off-the-shelf js checker (I've used YAV in the past: http://yav.sourceforge.net). pHp reporting is more straightforward:

if(isset($_SESSION['formerror']['mywidget']))echo$_SESSION['formerror']['mywidget'];

If you're going to produce your own php validation scripts, you'll need to learn about regular expressions (regexp). Looks horrendous, but ain't that bad.

Thanks a lot...By the way i want to ask your opinion.Which one is better?Put the validation code on a within with the output_fns.php or seperate it??Thanks...

Member Avatar for diafol

If I were you I'd create a new include file for your form validation functions. This way you keep it available for all your form validations.

For example:

In "form_validate.inc.php":

function validate($data_type){
 ...(this is your validation script)...
 ...it should return true or false...
 ...(this is the simplest validation function, it could be developed to be far more complex)...
}

In your form handler:

$date_valid =validate($_POST['mydate']);
(etc).

This ain't particularly clever code, but you get the idea. If you Google 'php form validation' you should get loads of good hits.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.