Commit 9808c3bf authored by Raulin's avatar Raulin

journal

parent e1d98ab8
<?php
require_once("template.php");
// if($_POST['id']!=""){ //si l'id est renseigné il s'agit d'une modification
// if(isset($_POST["date"]) && isset($_POST["quantite"]) && isset($_POST["select_aliment"])){
// $requete= "UPDATE `journal` SET `aliment` = '".$_POST['aliment']."', `calories` = '".$_POST['calories']."', `type` = '".$_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["date"]) && isset($_POST["quantite"]) && isset($_POST["select_aliment"])){
$id=$_SESSION['id'];
$a='SELECT `id_aliment` FROM `aliments` WHERE `aliment` ="'.$_POST["select_aliment"].'"';
$b = $mysqli->query($a);
$c=$b->fetch_all();
$idaliment= $c[0][0];
$requete="INSERT INTO `journal` (`date`,`id`, `id_aliment`,`quantite`) VALUES ( '" .$_POST['date'] . "', '" .$id . "', '" .$idaliment . "', '" . $_POST['quantite'] . "')";
$mysqli->query($requete);
$msg="L'élément a bien été ajouté au journal";
} else{
$msg="Il manque des informations";
$a='SELECT `id_aliment` FROM `aliments` WHERE `aliment` ="'.$_POST["select_aliment"].'"';
$b = $mysqli->query($a);
$c=$b->fetch_all();
$idaliment= $c[0][0];
$id=$_SESSION['id'];
if(isset($_POST["date"]) && isset($_POST["quantite"]) && isset($_POST["select_aliment"])){
$requete="INSERT INTO `journal` (`date`,`id`, `id_aliment`,`quantite`) VALUES ( '" .$_POST['date'] . "', '" .$id . "', '" .$idaliment . "', '" . $_POST['quantite'] . "')";
$mysqli->query($requete);
$msg="L'élément a bien été ajouté au journal";
} else{
$msg="Il manque des informations";
}
//
echo $msg;
......
......@@ -2,6 +2,6 @@
require_once("template.php");
$id=$_SESSION['id'];
$requete="DELETE FROM `journal` WHERE `journal`.`id_aliment` = ".$_POST['varIdAliment']."AND `journal`.`id` = ".$id;
$requete="DELETE FROM `journal` WHERE `journal`.`id_aliment` = ".$_POST['varIdAliment']." AND `journal`.`id` = ".$id;
$result = $mysqli->query($requete);
......@@ -12,10 +12,9 @@
} );
function resetForm() {
document.getElementById("submit_button").value="Ajouter";
document.getElementById("submit_button_journal").value="Ajouter";
document.getElementById("quantite").value = "";
document.getElementById("date").value = "";
// document.getElementById("mode").value = "";
};
......@@ -41,7 +40,6 @@
});
$(document).ready( function() {
// getId();
$.getJSON('https://eden.imt-lille-douai.fr/~charlotte.raulin/Projet/api/read_journal.php',
function(json){
table = $('#tbody_journal');
......@@ -50,38 +48,28 @@
// cf. https://api.jquery.com/jQuery.getJSON/
$.each( json, function( key, val ) {
table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td>'+val[3]*val[4]+'</td><td>'+val[4]+'</td><td><button '+
'onclick="onEdit(\''+val[0]+'\',\''+val[1]+'\',\''+val[4]+'\')">Modifier</button></td><td><button '+
'onclick="onDelete('+val[5]+','+val[6]+')">Supprimer</button></td></tr>');
// table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td>'+(val[3]*val[4])+'</td><td>'+val[4]+'</td><td><button '+
// 'onclick="onEdit(\''+val[0]+'\',\''+val[1]+'\',\''+val[4]+'\')">Modifier</button></td><td><button '+
// 'onclick="onDelete(\''+val[0]+'\','+val[5]+')">Supprimer</button></td></tr>');
table.append('<tr><td>'+val[0]+'</td><td>'+val[1]+'</td><td>'+val[2]+'</td><td>'+(val[3]*val[4])+'</td><td>'+val[4]+'</td><td><button '+
'onclick="onDelete(\''+val[0]+'\','+val[5]+')">Supprimer</button></td></tr>');
});
}
);
});
// function getId(){
// var varlogin= sessionStorage.getItem('para');
// $.ajax({
// url: 'api/read_journal.php',
// type: 'POST',
// data: {'varlogin' : varlogin},
// success: function(varlogin){
// alert(varlogin);// reponse contient l'affichage du fichier PHP (soit echo)
// }
// })
// };
function onEdit(date, aliment, quantite) {
document.getElementById("date").value = date;
document.getElementById("aliment").value = aliment;
document.getElementById("quantite").value = quantite;
document.getElementById("mode").value = "modifier";
document.getElementById("submit_button").value='Modifier';
};
// function onEdit(date, aliment, quantite) {
// document.getElementById("date").value = date;
// document.getElementById("select_aliment").value = aliment;
// document.getElementById("quantite").value = quantite;
// document.getElementById("mode").value = "modifier";
// document.getElementById("submit_button_journal").value='Modifier';
// };
function onDelete(id_aliment, id_user){
var varIdAliment= id_aliment;
var varIdUser= id_user;
function onDelete(date,ali){
var varIdAliment= ali;
$.ajax({
url: 'api/delete_journal.php',
type: 'POST',
......@@ -90,14 +78,6 @@
alert(varIdAliment); // reponse contient l'affichage du fichier PHP (soit echo)
}
});
$.ajax({
url: 'api/delete_journal.php',
type: 'POST',
data: {'varIdUser' : varIdUser},
success: function(varIdUser) {
alert(varIdUser); // reponse contient l'affichage du fichier PHP (soit echo)
}
});
location.reload();
}
......@@ -116,7 +96,6 @@
<th>Type</th>
<th>Calories (KCal)</th>
<th>Quantité</th>
<th>Modifier</th>
<th>Supprimer</th>
</tr>
</thead>
......@@ -147,12 +126,9 @@
<label>Quantité</label>
<input type="number" id="quantite" name="quantite" required>
</div>
<div>
<input type="text" id="mdifier" name="modifier" >
</div>
<div class="form-action-buttons">
<input type="submit" value="Ajouter" id='submit_button'>
<input type="submit" value="Ajouter" id='submit_button_journal'>
</div>
</form>
......
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