Tutoriale PHP
  Comunitatea PHP Romania

 
Tutoriale PHP
Exemple PHP
Categorii Exemple » Exemplu

valideaza formular


Descriere:
validare formular: daca e gol campul, daca e mail | retinerea in sesiune | rescriere in form | semnalare erori
Trimis de:octavian
Voturi:106
Accesari:2748

Sintaxa:
<?php
/* se presupune ca toate-s obligatorii */
$err = ;

$_SESSION[camp1] = $_POST[textfield];
$_SESSION[camp2] = $_POST[radiobutton];
$_SESSION[camp3] = $_POST[mail];
$_SESSION[camp4] = $_POST[select];
$_SESSION[camp5] = $_POST[textarea];

if(isset(
$_POST[formu]) && $_POST[formu] == da) {
    if(empty(
$_POST[textfield]) ||
    empty(
$_POST[radiobutton]) ||
    empty(
$_POST[mail]) ||
    empty(
$_POST[select]) ||
    empty(
$_POST[textarea]) )
        
$err .= "Completeaza toate campurile !\";
    else if (!preg_match(\"/^([a-zA-Z0-9])+([\\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\\.[a-zA-Z0-9_-]+)*\\.([a-zA-Z]{2,6})$/\", $_POST[\'mail\']))
    /* http://www.builderau.com.au/program/php/soa/Simplify_e_mail_validation_routines_with_PHP/0,39028448,39221002,00.htm */
        $err .= \"Mail-ul nu e valid !\";
    if(!empty($err)) print $err; else {
        $te1 = htmlspecialchars($_POST[\'textfield\'], ENT_QUOTES);
        $te2 = htmlspecialchars($_POST[\'radiobutton\'], ENT_QUOTES);
        $te3 = htmlspecialchars($_POST[\'mail\'], ENT_QUOTES);
        $te4 = htmlspecialchars($_POST[\'select\'], ENT_QUOTES);
        $te5 = htmlspecialchars($_POST[\'textarea\'], ENT_QUOTES);
    
        $arraiu[1] = $te1;
        $arraiu[2] = $te2;
        $arraiu[3] = $te3;
        $arraiu[4] = $te4;
        $arraiu[5] = $te5;
    
        for($i = 1; $i <= 5; $i++) {
            print $arraiu[$i].\"<br>\";
        }
    }
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>

<body>
<form name=\"form1\" method=\"post\" action=\"\">
  <p>
    <input name=\"textfield\" type=\"text\" value=\"<?php print $_SESSION[\'camp1\']; ?>\">
    <input name=\"formu\" type=\"hidden\" id=\"formu\" value=\"da\">
  </p>
  <p>
    <input name=\"radiobutton\" type=\"radio\" value=\"da\" <?php if($_SESSION[\'camp2\'] == \'da\') print \"CHECKED\"; ?>>
    da<br>
    <input name=\"radiobutton\" type=\"radio\" value=\"nu\" <?php if($_SESSION[\'camp2\'] == \'nu\') print \"CHECKED\"; ?>>
nu </p>
  <p>
    <input name=\"mail\" type=\"text\" id=\"mail\" value=\"<?php print $_SESSION[\'camp3\']; ?>\">
  </p>
  <p>
    <select name=\"select\">
      <option value=\"\">select value</option>
      <option value=\"va1\" <?php if($_SESSION[\'camp4\'] == \'val1\') print \"SELECTED\"; ?>>val1</option>
      <option value=\"val2\" <?php if($_SESSION[\'camp4\'] == \'val2\') print \"SELECTED\"; ?>>val2</option>
      <option value=\"val3\" <?php if($_SESSION[\'camp4\'] == \'val3\') print \"SELECTED\"; ?>>val3</option>
    </select>
  </p>
  <p>
    <textarea name=\"textarea\" cols=\"50\" rows=\"5\"><?php print $_SESSION[\'camp5\']; ?></textarea>
</p>
  <p>
    <input type=\"submit\" name=\"Submit\" value=\"trimite\">
</p>
</form>
</body>
</html>

Ultimele discutii in forum RSS Forum

Ultimele articole Ultimele articole

Top membri

Pirahna Pirahna
la birou
carco carco
Bucuresti
Birkoff Birkoff
Bucuresti
mihaitha mihaitha
Sibiu
Mascka Mascka
Braila
gabysolomon gabysolomon
Bacau
whooper whooper
Toronto ON
raul_ raul_
dechim dechim
Drobeta Turnu Severin
Amenthes Amenthes

Newsletter


Email:
 inscriere
 renuntare
 
 Arhiva newsletter

Site-ul lunii

blg
 Blgpunctro

Propune un site

Online

253 utilizatori online

Parteneriat



Copyright © 2001-2008 PHP Romania Add PHPRomania to Google Add PHPRomania to Del.icio.us Add PHPRomania to Stumbleupon Add PHPRomania to Yahoo! Add PHPRomania to Digg Add PHPRomania to Blink Loan | Blog5 Game Cheats | Car Finance | Credit Cards | Cheap Loan
Ads: Partener Way2Web Nework: gazduire web | inregistrare domenii | web design | imobiliare | web hosting
Powered by Simplis