Skip to content

Instantly share code, notes, and snippets.

@mars13code
Created March 7, 2018 15:05
Show Gist options
  • Select an option

  • Save mars13code/02a62e5f132c4f475652624c4129d63a to your computer and use it in GitHub Desktop.

Select an option

Save mars13code/02a62e5f132c4f475652624c4129d63a to your computer and use it in GitHub Desktop.
fonction de traitement centralisé des formulaires
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// variable globale
// dossier qui va grouper les fichiers de traitement de formulaire
$cheminController = __DIR__ . "/../private/controller";
function traiterForm (...$tabGoal)
{
global $cheminController;
static $feedback = "";
static $formGoal = "";
if (empty($tabGoal))
{
// CONTROLLER
$formGoal = trim(strip_tags($_REQUEST["--formGoal"] ?? ""));
if ($formGoal != "")
{
// http://php.net/manual/fr/function.ob-start.php
ob_start();
// ON NE PREND PAS EN COMPTE LE SUFFIXE
// POUR PERMETTRE DE RASSEMBLER PLUSIEURS TRAITEMENTS DANS UN SEUL FICHIER
// exemple: CRUD
// http://php.net/manual/fr/function.pathinfo.php
$formName = pathinfo($formGoal, PATHINFO_FILENAME);
// sécurité: enlever les caractères spéciaux
// http://php.net/manual/fr/function.preg-replace.php
$formName = preg_replace("/[^a-zA-Z0-9-_]/i", "", $formName);
// http://php.net/manual/fr/function.glob.php
$tabFichier = glob("$cheminController/form-$formName.php");
foreach($tabFichier as $fichier)
{
require_once($fichier);
}
// http://php.net/manual/fr/function.ob-get-clean.php
$feedback = ob_get_clean();
}
}
elseif (in_array($formGoal, $tabGoal))
{
// VIEW
// http://php.net/manual/fr/function.in-array.php
echo $feedback;
}
}
// CONTROLLER
traiterForm();
// VIEW
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<main>
<section>
<h2>FORMULAIRE 1</h2>
<form>
<!-- .ext EST OPTIONNEL POUR PHP -->
<input type="hidden" name="--formGoal" value="nom.ext">
<button>ENVOYER</button>
<?php traiterForm("nom.ext") ?>
</form>
</section>
<section>
<h2>FORMULAIRE 2</h2>
<form>
<!-- .ext EST OPTIONNEL POUR PHP -->
<input type="hidden" name="--formGoal" value="nom.ext2">
<button>ENVOYER</button>
<?php traiterForm("nom.ext2") ?>
</form>
</section>
<section>
<h2>FORMULAIRE 3</h2>
<form>
<!-- .ext EST OPTIONNEL POUR PHP -->
<input type="hidden" name="--formGoal" value="nom3">
<button>ENVOYER</button>
<?php traiterForm("nom3") ?>
</form>
</section>
<section>
<h2>FORMULAIRE 1,2,3</h2>
<?php traiterForm("nom.ext", "nom.ext2", "nom3") ?>
</section>
</main>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment