Insertion de donné d'un formulaire PHP en JSON avec une API
Bonjour,
Voila j'ai un problème je suis actuellement en alternance et je développe un projet de gestion d'étiquette, l'utilisateur à la possibilité avant tout de remplir un formulaire afin d'ajouter un Lots (de produit)
afin de sortir une étiquette qui se basera sur ces information en base de donnée. Je n'arrive pas à comprendre concrètement comment faire l'ajout de ce qui est renseigné dans les champs du formulaire dans la BDD postgre..
Je me doute bien que cela va se faire avec un POST mais c'est la seule chose que j'ai réussi à tirer de la doc de l'API
Je dois travailler avec : https://github.com/mevdschee/php-crud-api
Voici le code html du formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <div class="container champ_saisie background-color-lots">
<form name="tracaform" method="post" action="traitementAjout.php">
<div class="row">
<div class="col-md-3">
<label for="choix_article">Date</label>
<input type="date" id="date" class="form-control" name="date" required>
</div>
<div class="col-md-3">
<label for="choix_article">Article</label>
<select id="choix_article" class="form-control" name="choix_article" size="1" required>
</select>
</div>
<div class="col-md-2">
<label for="choix_article">Quantité</label>
<input type="text" id="quantite" class="form-control" name="quantite" required>
</div>
<div class="col-md-4">
<label for="choix_article">Commentaire</label>
<input type="text" id="commentaire" class="form-control" name="commentaire">
</div>
</div>
<?php include("generatetracaform.php"); ?>
</div> |
Le code de mon "générateur de formulaire" :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| <?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://localhost/Projet_Etiquette/api.php/records/traca_log_criteres/?order=ordre&filter1=traca_log_atelier_id,eq,2&filter1=valide,eq,1&filter2=traca_log_atelier_id,eq,-1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: localhost",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else
{
$aChamps= json_decode($response,true)['records'];
$i=0;
echo '<div class="row">';
foreach ($aChamps as $champ)
{
echo '<div class="col-md-2">';
$i++;
switch ($champ['type'])
{
case 'DATE':
echo GenerateDateForm($champ);
break;
case 'SELECT':
echo GenerateSelectForm($champ);
break;
case 'TEXT':
echo GenerateTextForm($champ);
break;
case 'NUMBER':
echo GenerateNumberForm($champ);
break;
default:
break;
}
echo '</div>';
if ($i%6==0)
{
echo "</div><div class='row'>";
}
}
echo '<input type="submit" name="btn_ajout_lots" VALUE="+" class="btn btn-dark" id="btn-ajouter">';
echo '</form>';
}
function GenerateDateForm($champ)
{
$sReturn = '<label for="'.$champ['type'].'_'.$champ['id'].'">'.$champ['libelle'].'</label>';
$required =($champ['obligatoire'])?'required':"";
$sReturn .= '<input type="date" id="'.$champ['type'].'_'.$champ['id'].'" '.$required.' name="'.$champ['type'].'_'.$champ['id'].'"';
$sReturn .= 'value="'.date("d/m/Y").'" ';
$validation= json_decode($champ['validation']);
$pattern=isset($validation->pattern)?'pattern="'.$validation->pattern .'"':"";
$sReturn .= $pattern;
$title=isset($validation->title)?'title="'.$validation->title .'"':"";
$sReturn .= $title;
$sReturn .= 'min="" max="" class="form-control" >';
return $sReturn;
}
function GenerateSelectForm($champ)
{
$sReturn = '<label for="'.$champ['type'].'_'.$champ['id'].'">'.$champ['libelle'].'</label>';
$required =($champ['obligatoire'])?'"required"':"";
$sReturn .= '<select id="'.$champ['type'].'_'.$champ['id']." ".$required.' class="form-control">';
$values= json_decode($champ['valeurspossibles']);
foreach ($values as $value) {
$sReturn .= '<option value="'.$value.'">'.$value.'</option>';
}
$sReturn .= '</select>';
return $sReturn;
}
function GenerateTextForm($champ)
{
$sReturn = '<label for="'.$champ['type'].'_'.$champ['id'].'">'.$champ['libelle'].'</label>';
$sReturn .= '<input type="text" id="'.$champ['type'].'_'.$champ['id'].'" name="'.$champ['type'].'_'.$champ['id'].'" class="form-control"';
$validation= json_decode($champ['validation']);
$sReturn .= 'value="" ';
$sReturn .=($champ['obligatoire'])?"required ":"";
$pattern=isset($validation->pattern)?'pattern="'.$validation->pattern .'"':"";
$sReturn .= $pattern;
$title=isset($validation->title)?'title="'.$validation->title .'"':"";
$sReturn .= $title;
$sReturn .= '>';
return $sReturn;
}
function GenerateNumberForm($champ)
{
$sReturn = '<label for="'.$champ['type'].'_'.$champ['id'].'">'.$champ['libelle'].'</label>';
$sReturn .= '<input type="number" id="'.$champ['type'].'_'.$champ['id'].'" name="'.$champ['type'].'_'.$champ['id'].'" class="form-control"';
$validation= json_decode($champ['validation']);
$sReturn .= 'value="" ';
$sReturn .=($champ['obligatoire'])?"required ":"";
$sReturn .= 'min="'.$validation->min .'"';
$sReturn .= 'max="'.$validation->max .'"';
$sReturn .= '>';
return $sReturn;
} |
Et la page ou faire mon traitement d'insertion qui pour le moment au vu du code ne sert pas à grand chose :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://localhost/Projet_Etiquette/api.php/records/traca_log_lot",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: localhost",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
} |
J'espère avoir été assez clair,
Merci d'avance pour vos réponse