projetphp/script.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-12-30 17:41:15 +01:00
$(function() {
$("#allRecord").on("click", ".editUser", function() {
$(this).toggleClass('fa-edit fa-check');
$(this).toggleClass('editUser confirmEditUser');
for(i=0; i<$("td", $(this).parent().parent()).length-1;i++)
{
j= $($("td", $(this).parent().parent())[i]);
j.html('<input '+(j.attr('class')=="pw"?'type="password" \
data-oldpw="'+ltrim(j.html())+'"':'type="text"')+'\
value="'+(j.attr('class')=="pw"?'':ltrim(j.html()))+'" />');
}
});
$("#allRecord").on("click", ".confirmEditUser", function() {
function callBackVerif(data){
var dataToSend = new Object();
dataToSend["updatePW"] = (data===true?false:true);
if($(".pw input", tr).val() === data || data === true)
{
for(i=0; i<tds.length-1;i++)
{
j= $(tds[i]);
className = j.attr('class');
if(className == "pw")
value = $('input', j).data("oldpw");
else
value = $('input', j).val();
dataToSend[className] = value;
}
$.post("post.php", dataToSend, function(data){
try {
if(data.return == true)
{
for(i=0; i<tds.length-1;i++)
{
j= $(tds[i]);
className = j.attr('class');
if(className == "pw")
value = data.pw;
else
value = $('input', j).val();
dataToSend[className] = value;
j.html(value);
}
swal("all done");
$(that).toggleClass('fa-check fa-edit');
$(that).toggleClass('confirmEditUser editUser');
}
else
swal(data);
} catch (e) {
swal( "Oops" , "Something went wrong!<br>"+e , "error" );
}
console.log(data.return)
}, "json");
}
else {
swal( "Oops" , "Password don't match !" , "error" );
}
}
tds = $("td", $(this).parent().parent());
tr = $(this).parent().parent();
that = this;
if ($(".pw input", tr).val() === "") {
swal("no pw update");
callBackVerif(true);
}
else {
swal({
closeOnClickOutside: false,
closeOnEsc: false,
content: {
element: "input",
attributes: {
placeholder: "Confirmer le mot de passe",
type: "password",
},
},
}).then(callBackVerif);
}
});
});
function ltrim(str) {
if(str == null) return str;
return str.replace(/^\s+/g, '');
}