Campuri obligatorii

postat acum 4 ani de Stefanescu Mihai in categorie PHP

In lectia precedenta am facut un formular si am stabilit cateva reguli, reguli conform carora numele, emailul si sexul erau obligatorii. Aceste campuri nu pot fi lasate goale in formular.

In codul scris anterior campurile erau optionale.

In codul de mai jos am scris cateva variabile noi ($err_nume, $err_email, $err_sexul). In aceste variabile am sa stochez mesajele de eroare pentru fiecare camp obligatoriu. De asemenea am mai adaugat si cate un if ce verifica daca au fost completate campurile.

$err_nume = $err_email = $err_sexul = "";
$name = $email = $gender = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  if (empty($_POST["nume"])) {
    $err_nume = "Numele este obligatoriu";
  } else {
    $name = testare($_POST["nume"]);
  }

  if (empty($_POST["email"])) {
    $err_email = "Email-ul este obligatoriu";
  } else {
    $email = testare($_POST["email"]);
  }

  if (empty($_POST["sexul"])) {
    $err_sexul = "Sexul este obligatoriu";
  } else {
    $gender = testare($_POST["sexul"]);
  }
}

Pentru a afisa mesajele de eroare punem printam pe ecran variabilele in care sunt pastrate mesajele de eroare (Daca a aparut un mesaj de eroare in variabila a fost trecut acel mesaj, altfel variabila este goala, deci nu va afisa nimic).

Deci fisierul nostru va avea forma:

 <?php
$err_nume = $err_email = $err_sexul = "";
$name = $email = $gender = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  if (empty($_POST["nume"])) {
    $err_nume = "Numele este obligatoriu";
  } else {
    $name = testare($_POST["nume"]);
  }

  if (empty($_POST["email"])) {
    $err_email = "Email-ul este obligatoriu";
  } else {
    $email = testare($_POST["email"]);
  }

  if (empty($_POST["sexul"])) {
    $err_sexul = "Sexul este obligatoriu";
  } else {
    $gender = testare($_POST["sexul"]);
  }
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">
		<span class="error">* <?php echo $err_nume;?></span><br />
		
E-mail:<input type="text" name="email">
		<span class="error">* <?php echo $err_email;?></span><br />

Website:<input type="text" name="website">
		
<label>Comment: <textarea name="comment" rows="5" cols="40"></textarea>

Gender:<input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male
	<span class="error">* <?php echo $err_sexul;?></span><br />

	<input type="submit" name="submit" value="Submit">

</form>

 

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group
Acest articol a fost mutat de pe vechea platforma.
Pentru orice eroare aparuta la mutare va rog sa ma contactati!

Comentariu postate de coach outlet online la data de 14.05.2019
A lot of thanks for every one of your efforts on this website. My mom loves managing investigations and it is obvious why. A number of us notice all regarding the compelling tactic you deliver informative ideas via the blog and as well recommend participation from others on the issue then our own child has been learning a lot. Take advantage of the remaining portion of the year. You are always conducting a really great job.
Comentariu postate de yeezy boost la data de 16.05.2019
I really wanted to write down a remark so as to appreciate you for all of the marvelous information you are placing on this website. My time consuming internet search has now been rewarded with really good facts and techniques to share with my two friends. I would express that we site visitors are undeniably blessed to live in a superb website with many lovely professionals with interesting solutions. I feel pretty fortunate to have discovered the web pages and look forward to many more enjoyable minutes reading here. Thank you once more for all the details.