Ajax table row to require authentication

Multi tool use
Multi tool use


Ajax table row to require authentication



I have another question since everyone was so nice last time.
How can I make a certain row in a table in ajax to be shown only if you are logged in? I ask this here because I didn't find the answer anywhere and I have no idea how to do that on myself. So if you guys can explain that would be thankful.



Here is the ajax table


/* Add new Post table row */
function manageRow(data) {
var rows = '';
$.each( data, function( key, value ) {
rows = rows + '<tr>';
rows = rows + '<td>'+value.title+'</td>';
rows = rows + '<td>'+value.icon+'</td>';
rows = rows + '<td>'+value.user+'</td>';
rows = rows + '<td>'+value.details+'</td>';
rows = rows + '<td>'+value.created_at+'</td>';
rows = rows + '<td data-id="'+value.id+'">';
rows = rows + '<button data-toggle="modal" data-target="#edit-item" class="btn btn-warning edit-item">Edit</button> ';
rows = rows + '<button class="btn btn-danger remove-item">Delete</button>';
rows = rows + '</td>';
rows = rows + '</tr>';
});
$("tbody").html(rows);
}



And I want those 2 rows to not be seen if you are not logged in.


rows = rows + '<button data-toggle="modal" data-target="#edit-item" class="btn btn-warning edit-item">Edit</button> ';
rows = rows + '<button class="btn btn-danger remove-item">Delete</button>';



I hope you guys can help me. Thank you in advance.





How do we know if your user is logged in?
– MadSkunk
Jul 3 at 9:13





expand your function by parameter "signedIn" and check if it is true. ofc you have to pass that parameter, too!
– Manticore
Jul 3 at 9:16





using ternary operator, you could write rows += (signedIn ? '<button data-toggle="modal" data-target="#edit-item" class="btn btn-warning edit-item">Edit</button> ' : ''); rows += (signedIn ? '<button class="btn btn-danger remove-item">Delete</button>' : '');
– Manticore
Jul 3 at 9:17



rows += (signedIn ? '<button data-toggle="modal" data-target="#edit-item" class="btn btn-warning edit-item">Edit</button> ' : ''); rows += (signedIn ? '<button class="btn btn-danger remove-item">Delete</button>' : '');





@Manticore Please post your answers as answers (instead of comments)…
– feeela
Jul 3 at 11:23





@feeela actually i did not really believe this was the answer...
– Manticore
Jul 4 at 15:11





3 Answers
3



Personally, I'd go for something like this:


/* Add new Post table row */
function manageRow(data, isLoggedIn)
{
var rows = '';
$.each(data, function(key, value)
{
rows += '<tr>';
rows += '<td>' + value.title + '</td>';
rows += '<td>' + value.icon + '</td>';
rows += '<td>' + value.user + '</td>';
rows += '<td>' + value.details + '</td>';
rows += '<td>' + value.created_at + '</td>';
rows += '<td data-id="' + value.id + '">';

if (isLoggedIn)
{
rows += '<button data-toggle="modal" data-target="#edit-item" class="btn btn-warning edit-item">Edit</button> ';
rows += '<button class="btn btn-danger remove-item">Delete</button>';
}

rows += '</td>';
rows += '</tr>';
});

$("tbody").html(rows);
}



However, this depends on your security concerns, it would be trivial for a hacker to mess with this and gain access to the two buttons.





Thanks you it worked!
– Seth93
Jul 3 at 9:45





You can mark it as the answer if it helped.
– MadSkunk
Jul 3 at 9:58



First you need a variable to determine if you have logged in. 'isLogin'.




if(isLogin){// Need your background pass value
rows = rows + '<button data-toggle="modal" data-target="#edit-item" class="btn btn-warning edit-item">Edit</button> ';
rows = rows + '<button class="btn btn-danger remove-item">Delete</button>';
}




//Please use a local variable
var IsUserLoggedin = false;
//Set it to true when your logged in

function manageRow(data) {
var rows = '';
$.each( data, function( key, value ) {
rows = rows + '<tr>';
rows = rows + '<td>'+value.title+'</td>';
rows = rows + '<td>'+value.icon+'</td>';
rows = rows + '<td>'+value.user+'</td>';
rows = rows + '<td>'+value.details+'</td>';
rows = rows + '<td>'+value.created_at+'</td>';
rows = rows + '<td data-id="'+value.id+'">';
if(IsUserLoggedin)
{
rows = rows + '<button data-toggle="modal" data-target="#edit-item" class="btn btn-warning edit-item">Edit</button> ';
rows = rows + '<button class="btn btn-danger remove-item">Delete</button>';
}
rows = rows + '</td>';
rows = rows + '</tr>';
});
$("tbody").html(rows);
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

wdJdzC9rfuiB3IIm16KOw rK9YpZZ0FjLTNE,P,zO9DUA
FmRY klRbeWtjE5jsZz24DEh2 E,a,Gj55V qCo3vu

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications