I'm working on a small little social network project. So far i have a message system that all works fine, users can send and receive messages. I'm looking for a way to let users delete their own messages.
A 'delete' row has been set to display down the side of each message through an echo command.
Is there a way to make this text clickable so that when a user clicks it this then changes the value in the database from '0' to '1'?
Code:
<?php
$page_title = "Messages";
include('includes/header.php');
confirm_logged_in();
include('includes/mod_login/login_form.php');
?>
<div class="modtitle">
<div class="modtitle-text">Inbox</div>
</div>
<div class="modcontent">
<strong>Inbox</strong> | Sent Messages
| Deleted <br /><br />
<table
width="100%" border="0" cellpadding="5" cellspacing="0">
<tr
bgcolor="#CCCCCC">
<td width="30%"><strong>Recieved</strong></td>
<td width="20%"><strong>From</strong></td>
<td width="28%"><strong>Subject</strong></td>
<td width="0%"><strong>Read/Unread</strong></td>
<td width="0%"><strong>Delete</strong></td>
</tr>
<?php
$inbox_set = get_inbox();
while ($inbox =
mysql_fetch_array($inbox_set)) {
?>
<?php if ($inbox['read'] == 0) { ?> <tr bgcolor="#6666B3"> <?php }
if ($inbox['read'] == 1) { ?> <tr>
<?php } ?>
<td><?php
$datesent1 = $inbox['date_sent'];
echo "$datesent1"; ?></td>
<a href="profile.php?id={$inbox['from_user_id']}"><td><?php echo "<a
href=\"profile.php?id={$inbox['from_user_id']}\">{$inbox['display_name']}";
?></td></a>
<td><?php echo "<strong>{$inbox['subject']}</strong>";
?></td>
<td><?php if ($inbox['read'] == 0) {
echo "Unread";
}
if ($inbox['read'] == 1) {
echo "Read";
}
?></td>
<td>
<?php
if ($inbox['delete'] == 0) {
echo "Delete";
}
if ($inbox['delete'] == 1) {
echo "$deleted_set;";
}
; ?></td>
</td>
<?php }
?>
</tr> </table>
</div>
<?php include('includes/footer.php'); ?>
Use AJAX request to trigger proper action on server side.
Remember to check authorization otherwise one may delete messages owned by other users.
Use jQuery + Ajax to do this task similar like this
$.ajax({
type:"POST",
url: url, // action page url
data: "id="+id,
success: function(msg){
if(msg) {
//action here
}
});
Related
I have a foreach loop on a tale row. Each row has an "edit" button to edit that row only. Using JS, I want to get a hidden open and edit the row.
Bellow is my code that I have tried but it works on one element only.
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction()" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction() {
<?php
foreach($sofas as $sofa)
{
?>
var x = document.getElementById("<?php echo $sofa["id"];?>");
<?php
}
?>
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
I'm still learning, so please help me resolve my issue. I'm expecting the code to work on each individual table row.
Why don't you use something like this:
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction('<?php echo $sofa["id"];?>')" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
Code is very simple, you only need to send the element id to the js function.
Hy,
I want to ban a user when the admin clicks on "bannir" (this will collect the id of the user and put "banned" at 1 in the database)
to print the user informations I'm using a while loop, but when I try to collect the id of the user where in the html tag with the class "idUser", it is always sending the first id and I don't know why..
image of membership area
<div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Prénom</th>
<th>Nom</th>
<th>Email</th>
<th>Admin</th>
<th>Banni</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while($userInfo = $req->fetch()){?>
<tr>
<td class="idUser"><?= $userInfo['id_user'] ?></td>
<td><?= $userInfo['last_name'] ?></td>
<td><?= $userInfo['first_name'] ?></td>
<td><?= $userInfo['email'] ?></td>
<td><?php if($userInfo['admin'] == 1){ ?>
<img src="../img/ok.png">
<?php } else { ?>
<img src="../img/no.png">
<?php } ?></td>
<td><?php if($userInfo['banned'] == 1){ ?>
<strong style="color:#E04949;">OUI</strong>
<?php } else { ?>
<strong style="color:#6AC259;">NON</strong>
<?php } ?></td>
<td>Modifier | <?php if($userInfo["banned"] == 0){ ?> Bannir <?php }else{ ?> Débannir <?php } ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
$('.banMemberJquery').click(function(){
var idUser = $( ".idUser" ).html();
alert('Utilisateur Numero ' + idUser + ' banni avec succès.');
$.ajax({
url:"banMemberRequest.php",
data:'idUser='+idUser,
}).done(function(data){
$('#result').html(data);
});
});
PS : When I click on "bannir", the request in the file "banMemberRequest.php" is working correctly.
Thank's in advance for helping
The issue is because you're selecting all .idUser elements. Calling html() on that will only read the HTML of the first one in that collection.
To fix this you need to use DOM traversal to find only the .idUser element which is related to the .banMemberJquery element which was clicked. To do that you can use closest() and find(), like this:
$('.banMemberJquery').click(function() {
var idUser = $(this).closest('tr').find('.idUser').text(); // note text(), not html()
$.ajax({
url: 'banMemberRequest.php',
data: { idUser: idUser },
}).done(function(data) {
$('#result').html(data);
});
});
when i clicked the link it does not redirect to the page that specified.i have the sqlite database and there is a database column for status(added,updated like wise).i want to redirect to the page when i clicked the status link in my link.php.Now Object not found error is getting.
this is my link.php code below.
Please can you help me guys.
<?php
// Includs database connection
include "db_connect.php";
// Makes query with rowid
$query = "SELECT rowid, * FROM registration";
// Run the query and set query result in $result
// Here $db comes from "db_connection.php"
$result = $db->query($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Data List</title>
<script>
function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=1076,height=768,directories=no,location=no')
}
</script>
</head>
<body>
<br>
<br>
<!-- Add New-->
<table>
<tbody>
<tr>
<th style="">status</th>
<th style="">submitted</th>
<th style="">department</th>
<th style="">head</th>
<th style="">title</th>
<th style="">applicant</th>
<th style="">date</th>
</tr>
<?php while($row = $result->fetchArray()) {?>
<!--<tr class="table-row" data-href="update.php?id=<?php echo $row['rowid'];?>" data-target="_blank">-->
<tr>
<td>
</td>
<td>view
<?php
if ($row['rowid'] == "added"){
echo "<a href='updated.php" . $row['submitted'] . "updated'> </a>";
} else {
echo "<a href='next.php" . $row['submitted'] . "next'> </a>";
}
?>
</td>
<td><?php echo $row['status'];?></td>
<td><?php echo $row['submitted'];?></td>
<td><?php echo $row['department'];?></td>
<td><?php echo $row['head'];?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['applicant'];?></td>
<td><?php echo $row['date'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script>
var tableRows = document.getElementsByClassName('table-row');
for (var i = 0, ln = tableRows.length; i < ln; i++) {
tableRows[i].addEventListener('click', function() {
window.open(this.getAttribute('data-href'), this.getAttribute('data-target'));
});
}
</script>
<br>
<br>
</body>
</html>
I'm using keypress via this OOP function below to update my sites shopping cart and quantity value. Sometimes it works, but most of the time when I press the enter key it doesn't a) update the cart nor b) update the qty value. I think it's refreshing the page, which could be the issue.
I took a look at the request header value by inspecting the qty element and that doesn't update when enter key pressed, if that helps.
function initBinds() {
if ($('.remove_basket').length > 0) {
$('.remove_basket').bind('click', removeFromBasket);
}
if ($('.update_basket').length > 0) {
$('.update_basket').bind('click', updateBasket);
}
if ($('.fld_qty').length > 0) {
$('.fld_qty').bind('keypress', function(e) {
var code = e.keyCode ? e.keyCode : e.which;
if (code == 13) {
updateBasket();
}
});
}
}
And here's the updateBasket function
function updateBasket() {
$('#frm_basket :input').each(function() {
var sid = $(this).attr('id').split('-');
var val = $(this).val();
$.ajax({
type: 'POST',
url: '/mod/basket_qty.php',
data: ({ id: sid[1], qty: val }),
success: function() {
refreshSmallBasket();
refreshBigBasket();
},
error: function() {
alert('An error has occurred');
}
});
});
}
And, this is the page...
Note, you can see on line 41 the class of fld_qty is used for the initiBinds keypress function if statement.
<?php
$session = Session::getSession('basket');
$objBasket = new Basket();
$out = array();
if (!empty($session)) {
$objCatalogue = new Catalogue();
foreach ($session as $key => $value) {
$out[$key] = $objCatalogue->getProduct($key);
}
}
require_once('_header.php'); ?>
<div id="cat_prod"><h1>- BASKET -</h1></div>
<?php
if (!empty($out)) { ?>
<div id="big_basket">
<form action="" method="post" id="frm_basket">
<table cellpadding="0" cellspacing="0" border="0" class="tbl_repeat">
<tbody id="basket_table">
<tr style="background-color: #f2f3ee;">
<th class="ta_left">Item</th>
<th class="ta_r">Qty</th>
<th class="ta_r col_15">Price</th>
<th class="ta_r col_15"></th>
</tr>
<?php foreach ($out as $item) { ?>
<tr>
<td class="ta_left_name"><?php echo Helper::encodeHTML($item['name']); ?></td>
<td class="ta_left_qty"><input type="text" name="qty-<?php echo $item['id']; ?>"
id="qty-<?php echo $item['id']; ?>" class="fld_qty"
value="<?php echo $session[$item['id']]['qty']; ?>" /></td>
<td class="ta_r">£<?php echo number_format($objBasket->itemTotal($item['price'], $session[$item['id']]['qty']), 2); ?></td>
<td class="ta_r"> <?php echo Basket::removeButton($item['id']); ?></td>
</tr>
<?php } ?>
<?php if ($objBasket->_vat_rate != 0) { ?>
<tr style="border-bottom: dashed 1px #aaa">
<td class="ta_left" colspan="2">Sub-total :</td>
<td class="ta_r bt_td">£<?php echo number_format($objBasket->_sub_total, 2); ?></td>
<td class="ta_r bt_td"> </td>
</tr>
<tr style="border-bottom: dashed 1px #aaa">
<td class="ta_left" colspan="2">VAT (<?php $objBasket->_vat_rate; ?>%) :</td>
<td class="ta_r bt_td">£<?php echo number_format($objBasket->_vat, 2); ?></td>
<td class="ta_r bt_td"> </td>
</tr>
<?php } ?>
<tr>
<td class="ta_right" colspan="2"><strong>Total :</strong></td>
<td class="ta_r bt_td">£<?php echo number_format($objBasket->_total, 2); ?></td>
<td class="ta_r bt_td"> </td>
</tr>
</tbody>
</table>
<div class="dev br_td"> </div>
<div class="dev br_td"> </div>
<div class="sbm sbm_blue fl_r">
Checkout
</div>
<div class="sbm sbm_blue fl_l update_basket">
<span class="btn">Update</span>
</div>
</form>
</div>
<?php } else { ?>
<br />
<br />
<p><em>Your basket is currently empty.</em></p>
<?php } ?>
<?php require_once('_footer.php'); ?>
I have looked through some statckflow pages regarding this and have tried keydown and just using e.which and e.keyCode || e.which instead, but they all render the same issue of not working 100% of the time when you press enter key.
I understand some browsers may not support this, so is there a better approach for this operation? I have tested Firefox, Chrome and Safari (all latest).
Thanks for the help, appreciated! :)
Edit;
Here's the mod/basket_qty.php also...
<?php
require_once('../inc/autoload.php');
if (isset($_POST['qty']) && isset($_POST['id'])) {
$out = array();
$id = $_POST['id'];
$val = $_POST['qty'];
$objCatalogue = new Catalogue();
$product = $objCatalogue->getProduct($id);
if (!empty($product)) {
switch($val) {
case 0:
Session::removeItem($id);
break;
default:
Session::setItem($id, $val);
}
}
}
Looks like I needed to add e.preventDefault(); in the initBinds function for if (code == 13) { as pointed out by cmorrissey. It seems to be working fine with this. Thanks!!
I have created custom plugin in wordpress for get data from the database. And I have added two link to perfrom action on records .One is Edit and another is Delete. I have written code for delete link like this
Delete
When I click on delete link it will show error like
You do not have sufficient permissions to access this page
My plugin code is
function submit_review()
{
add_options_page("Submit Review","Submit Reviews",1,"submit_review","submit_review");
global $wpdb;
$id = get_current_user_id();
$q = "select * from wp_submit_review where user_id = " . $id;
$result = $wpdb->get_results($q,OBJECT);
if(!empty($result))
{
?>
<div class="wrap">
<h1>Submitted Reviews</h1>
<div style="width: 100%;">
<table cellpadding="5" class="wp-list-table widefat fixed pages">
<thead>
<tr>
<th><b>Review Id</b></th>
<th><b>User Name</b></th>
<th><b>Submitted Category</b></th>
<th><b>New Listing Name</b></th>
<th><b>New Listing Address</b></th>
<th><b>New Listing City</b></th>
<th><b>New Listing Description</b></th>
<th><b>Image</b></th>
<th><b>R-option</b></th>
<th><b>New Listing Rating</b></th>
<th><b>New Listing Review</b></th>
<th><b>Actions</b></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $res)
{
?>
<td><?php echo $res->review_id; ?></td>
<td><?php echo get_the_author_meta('user_login',$res->user_id); ?></td>
<td><?php
if($res->submit_category == 1)
{
echo "Service";
}
else if($res->submit_category == 2)
{
echo "Restaurant";
}
else
{
echo "Product";
}
?>
</td>
<td><?php echo $res->newlistingname; ?></td>
<td><?php echo $res->newlistingaddress; ?></td>
<td><?php echo $res->newlistingcity; ?></td>
<td><?php echo $res->newlistingdesc; ?></td>
<td><img src="<?php echo home_url()."/uploads/".$res->image; ?>" height="50px" width="50px"/></td>
<td><?php echo $res->roption; ?></td>
<td><?php echo $res->newlistingrating; ?></td>
<td><?php echo $res->newlistingreview; ?></td>
<td>Edit | Delete</td>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<?php
}
else
{
echo "No review find";
}
}
function delete_my_review()
{
echo $_GET['id'];
}
add_action("admin_menu","delete_my_review");
function submit_review_menu()
{
add_menu_page("Submit Review","Submit Review","manage_options","submit_review", submit_review);
}
//add_action("admin_menu","submit_review");
add_action("admin_menu","submit_review_menu");
So how to call delete_my_review() function to perform delete action?
simply create a function with ajax and php. following example worked for me.
html delete button
<td><button onclick="delete_ab(<?php echo $results->id; ?>)">Delete</button></td> // pass your id to delete
javascript function
function delete_ab(str)
{
jQuery.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : 'myajax_submit',
// other parameters can be added along with "action"
postID : str
},
function(response)
{
document.getElementById(str).style.display='none';
}
);
}
main function in php
function myajax_submit() {
// get the submitted parameters
$postID = $_POST['postID'];
global $wpdb;
$qry=$wpdb->query("DELETE FROM `price_table` WHERE id=$postID");
// generate the response
echo "hello";
// IMPORTANT: don't forget to "exit"
exit;
}