Commit 84db5bf3 authored by Raulin's avatar Raulin

aliments complet (crud)

parent ede81403
No preview for this file type
......@@ -7,32 +7,57 @@
<script type="text/javascript">
$(document).ready(function() {
$('#table_aliments').DataTable();
resetForm();
} );
function resetForm() {
document.getElementById("id").value=""
document.getElementById("aliment").value = "";
document.getElementById("cat").value = "";
document.getElementById("submit_button").value="Ajouter";
};
</script>
<script type="text/javascript">
$(document).ready( function() {
$.getJSON('https://eden.imt-lille-douai.fr/~charlotte.raulin/Projet/api/afficher_aliments.php',
function(json){
// console.log(json);
table = $('#tbody_aliment');
table.html('');
// cf. https://api.jquery.com/jQuery.getJSON/
$.each( json, function( key, val ) {
table.append('<tr><td>'+val[1]+'</td><td>'+val[2]+'</td></tr>');
// table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td><button '+
// 'onclick="onEdit('+val[0]+',\''+val[1]+'\',\''+val[2]+'\')">Modifier</button></td></tr>');
table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td><button '+
'onclick="onEdit('+val[0]+',\''+val[1]+'\',\''+val[2]+'\')">Modifier</button></td><td><button '+
'onclick="onDelete('+val[0]+')">Supprimer</button></td></tr>');
});
}
);
// $.getJSON('https://eden.imt-lille-douai.fr/~charlotte.raulin/Projet/api/afficher_aliments.php',
// function(json){
// $.('#tbody_aliment').html('');
// for (let i = 1; i < json.lenght; i++){
// $('#tbody_aliment').append('<tr><td>'json[i][1]+'</td><td>'+json[i][2]+'</td></tr>');
// }
// };
// );
});
function onEdit(id, nom, type) {
document.getElementById("id").value = id;
document.getElementById("aliment").value = nom;
document.getElementById("cat").value = type;
document.getElementById("submit_button").value='Modifier';
};
function onDelete(id){
var varID= id;
$.ajax({
url: 'api/delete_aliment.php',
type: 'POST',
data: {'varID' : varID},
success: function(varID) {
alert(varID); // reponse contient l'affichage du fichier PHP (soit echo)
}
});
location.reload();
}
</script>
</head>
......@@ -43,9 +68,12 @@
<thead>
<tr>
<th>ID</th>
<th>Aliment</th>
<th>Type</th>
<th>AjoutAuJournal</th>
<th>Modifier</th>
<th>Supprimer</th>
<th>Ajouter au Journal </th>
</tr>
</thead>
<tbody id="tbody_aliment"><tbody>
......@@ -58,6 +86,9 @@
<tr>
<td>
<form id="form_aliment" action="api/ajouter_aliment.php" method="POST" >
<div>
<input type="text" id="id" name="id" style="display : none">
</div>
<div>
<label>Aliment</label>
<input type="text" id="aliment" name="aliment" required>
......@@ -72,7 +103,8 @@
</div>
<div class="form-action-buttons">
<input type="submit" value="Ajouter">
<input type="submit" value="Ajouter" id='submit_button'>
</div>
</form>
</td>
......
<?php
require_once("template.php");
if(isset($_POST["aliment"]) && isset($_POST["cat"])){
$requete="INSERT INTO `ALIMENTS` (`ID_ALIMENT`,`NAME_ALIMENT`, `TYPE_ALIMENT`) VALUES (NULL, '" .$_POST['aliment'] . "', '" . $_POST['cat'] . "')";
$mysqli->query($requete);
$msg="L'aliment a bien été ajouté";
} else{
$msg="Il manque des informations";
if(isset($_POST['id']) && $_POST['id']!=""){ //si l'id est renseigné il s'agit d'une modification
if(isset($_POST["aliment"]) && isset($_POST["cat"])){
$requete= "UPDATE `ALIMENTS` SET `NAME_ALIMENT` = '".$_POST['aliment']."', `TYPE_ALIMENT` = '".$_POST['cat']."' WHERE `ALIMENTS`.`ID_ALIMENT` = ".$_POST['id'];
$mysqli->query($requete);
$msg="L'aliment a bien été modifié";
}
else{
$msg="Il manque des informations";
}
}
else{ //sinon il s'agit d'un ajout
if(isset($_POST["aliment"]) && isset($_POST["cat"])){
$requete="INSERT INTO `ALIMENTS` (`ID_ALIMENT`,`NAME_ALIMENT`, `TYPE_ALIMENT`) VALUES (NULL, '" .$_POST['aliment'] . "', '" . $_POST['cat'] . "')";
$mysqli->query($requete);
$msg="L'aliment a bien été ajouté";
} else{
$msg="Il manque des informations";
}
}
echo $msg;
echo $msg;
<?php
require_once("template.php");
$requete= "DELETE FROM `ALIMENTS` WHERE `ALIMENTS`.`ID_ALIMENT` = ".$_POST['varID'];
$mysqli->query($requete);
$msg= "L'aliment a été supprimé";
echo $msg;
<?php
require_once("template.php");
if(isset($_POST["Id"]) && isset($_POST["aliment"]) && isset($_POST["cat"])){
$requete= "UPDATE `ALIMENTS` SET `NAME_ALIMENT` = '".$_POST['aliment']."', `TYPE_ALIMENT` = '".$_POST['cat']."' WHERE `ALIMENTS`.`ID_ALIMENT` = 23";
$mysqli->query($requete);
$msg="L'aliment a bien été modifié";
} else{
$msg="Il manque des informations";
}
echo $msg;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment