In 1stpage I have a table with full of data, and end of every row there is a delete link. When i click on that link, the link goes to another delete page where mysql delete query delete that particular data from database. That delete page redirect with the 1st page (location:1stpage). Now when i want to apply a javascript delete yes/no checking, every time it pops up with "Are you sure?" either i press yes or no the data is deleted from my table. I think the return false cannot working in this case. The code are in below. Please help me what should i do exactly.
Sorry for my bad english.
Thanks in advance.
1st page:
<?php
include "include.php";
?>
<script type="text/javascript">
function show_confirm()
{
var con = confirm("Are You Sure");
if (con ==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
<table>
<tr>
<td align="center">EDIT OR DELETE DATA</td>
</tr>
<tr>
<td>
<table border="1">
?php
if(isset($_POST['submit']))
{
$order = "UPDATE books SET author='$_POST[author]', title='$_POST[title]', price='$_POST[price]' WHERE isbn='$_POST[isbn]'";
mysql_query($order) or die (mysql_error());
}
$order = "SELECT * FROM books ORDER BY author" or die (mysql_error());
$result = mysql_query($order);
while ($row=mysql_fetch_array($result))
{
echo ("<tr>
<td>$row[isbn]</td>
<td>$row[author]</td>
<td>$row[title]</td>
<td>$row[price]</td>
<td> Edit </td>
<td> <a href=\"delete.php?isbn=$row[isbn]\" onclick='show_confirm()'> Delete </a> </td>
</tr>");
}
?>
</table>
</td>
</tr>
</table>
the page where mysql delete query run :
<?php header("Location: editordeletedata.php"); ?>
<p> <title>delete</title> </p>
<?php
include "include.php";
?>
<?php
if(isset($_GET['isbn']))
{
$order = "DELETE FROM books WHERE isbn = '$_GET[isbn]'";
mysql_query($order) or die (mysql_error());
}
?>
Look at this simple code snippet I wrote, should get you fixed up right.
<html>
<head>
<script type="text/javascript">
function show_confirm() {
return confirm("Are You Sure");
}
</script>
</head>
<body>
<a href="http://www.google.com" onclick='return show_confirm();'> Google </a>
</body>
</html>
Fiddle
I don't see any reason why return false will not work.
function show_confirm()
{
if (confirm("Are You Sure"))
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
return false;
}
}
Disable the delete link and move the redirection to the javascript function. The link should look like this:
<a href=\"javascript:void(0)\" onclick='show_confirm($row[isbn])'> Delete </a>
And the javascript function just like this:
function show_confirm(isbn)
{
if (confirm("Are You Sure"))
{
alert("You pressed OK!");
location.replace('delete.php?isbn='+isbn);
}
else
{
alert("You pressed Cancel!");
}
}
Related
In a table I display several links whose value (user id) is extracted from a database. By clicking on the link appears a modal pop up in which I would like to pass the selected value.
Here is the table:
<table>
<tr>
<td>Username</td>
<td>Id</td>
</tr>
<?php
include ‘db_connection.php’;
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row[userID]?></td>
<td>
<div id='basic-modal'>
<a href="?id=<?php echo $row[userID]?>" class='basic'>Show</a>
</div>
</td>
</tr>
<?php } ?>
</table>
If I click on the link Show appears the modal pop up:
<div id="basic-modal-content">
<?php
if(isset($_GET[‘userID’])){
$userID = $_GET[‘userID’];
echo ‘UsuerID: ‘ .$userID;
}
?>
</div>
This is the script I used
jQuery(function ($) {
$('#basic-modal .basic').click(function (e) {
$('#basic-modal-content').modal();
return false;
});
});
Since I have little familiarity with the jQuery framework I would like to ask you how can I pass the selected value within the modal and then use it.
jquery allows you to use the .data() attribute
<div id='basic-modal'>
<a href="?id=<?php echo $row[userID]?>" data-mydata="<?php echo $row[userID]?>" class='basic'>Show</a>
</div>
and then u can retrieve data from 'data-mydata' attribute:
jQuery(function ($) {
$('#basic-modal .basic').click(function (e) {
$('#basic-modal-content')
.text($(this).data('mydata'))
.modal();
return false;
});
});
I have this code inside a while loop for fetching data from the database. I want to show a confirmation popup when the Delete link is clicked.
echo "<td><a href='delete.php?id=" . $row['serial_no'] . "'>Delete</a></td>";
How can this be done?
Try out confirm function from JavaScript, learn jQuery as fast as possible and try to seperate javascript from html ;).
http://www.w3schools.com/jsref/met_win_confirm.asp
<td>
<a href="delete.php?id=<?php echo $row['serial_no'] ?>" id="a_id">
Delete
</a>
</td>
<script type="text/javascript">
$(function() {
$('td a#a_id').click(function() {
return confirm("Are You sure that You want to delete this?");
});
});
</script>
add onClick attribute for anchor link and call the function on it. Or simply show confirm.
<a onClick = "confirm ('Are You Sure?')"
Complete Example
function disp_confirm()
{
var r=confirm("Press a button!")
if (r==true)
{
alert("You pressed OK!")
}
else
{
alert("You pressed Cancel!")
}
}
<a onclick="disp_confirm()"
try this
function DeleteClick(serial_no)
{
if(confirm('Are you sure to delete ' + serial_no + '?'))
{
alert('Data deleted');
return true;
}
return false;
}
echo "<td><a href='delete.php?id=" . $row['serial_no'] . "' onclick="return
DeleteClick(\''. $row['serial_no'].'\')">Delete</a></td>";
echo "<td>Delete</td>";
try this code,hope it will work-
<td>
<a href="#" onclick="conf("delete.php?id=".<?php echo $row['serial_no']; ?>.")">
Delete
</a>
</td>
<script type="text/javascript">
function conf(str) {
if(confirm("Your Message") == true){ location.replace(str);}
}
</script>
and in the delete.php page-just grab the id from url and run the delete query.
I am getting data from a database that contains some rows of data which is archived data. I'm using php to get data and storing the data in entity objects.
To reduce database calls, I want to be able to display all the rows except the archived data when the page is created but have a link on the page that will change from 'show archived' to 'hide archived' which toggles the archived data on and off respectively.
The data is displayed in a table. I am new to jquery and have found lots of articles but there seems to be a bug when showing and hiding in different browsers and am now very confused about the best way to do this. I need to support most browsers but am using jQuery 1.10.
<table width='100%' border="1" cellspacing="0" cellpadding="4" class="mytable">
<tr>
<th>Product</th>
<th>Supplier</th>
....
</tr>
<?php
if ($product->getDateArchived() != NULL) {
echo "<tr class=\"archivedrow\">";
} else {
echo "<tr> ";
} ?>
<td><?= $product->getProductName() ?></td>
<td><?= $supplier->getSupplierName() ?></td>
....
If on load you are supposed to hiding archived rows.
Call this in document ready
$(document).ready(function(){
$('tr.archivedrow').hide();
});
Now on show archived button just call
$('tr.archivedrow').show();
And on hide archived button just call
$('tr.archivedrow').hide();
There are several ways to do this. I'll share with you three.
Print out all rows in the HTML but hide/show via jQuery
You mentioned you wanted to reduce database load by only returning the non archived rows at first. This solution doesn't reduce the database load on page load as it gets all of them and just uses css to hide them
PHP/HTML
<?php
// ... db connection code etc.
$result = $mysqli->query("SELECT * FROM table");
while ($row = $result->fetch_assoc()) {
?>
<div class="<?php echo $row['archived'] ? 'archived' : 'notarchived' ?>">
<!-- all your html stuff here -->
</div>
<?php } ?>
<a id="toggle-archived" href="#">Toggle archived</a>
<?php
// ... clean up db/result close code etc.
CSS
.archived {
display: none;
}
JS
$(function() {
$('#toggle-archived').click(function() {
$('.archived').toggle();
});
});
Another way to do it is to have the page refresh and change the sql query on refresh
PHP/HTML
<?php
// ... db connection code etc.
$showArchived = isset($_GET['show_archived']) && $_GET['show_archived'];
$sql = "SELECT * FROM table";
if (!$showArchived) {
$sql .= " WHERE archived = 0";
}
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
?>
<div>
<!-- all your html stuff here -->
</div>
<?php } ?>
<?php if ($showArchived) : ?>
Hide archived
<?php else: ?>
Show archived
<?php endif; ?>
<?php
// ... clean up db/result close code etc.
The third way would be to change the content via ajax. The could would be similar to those above but you would have your javascript look something like:
$(function(){
$('#toggle-archived').click(function(){
$.post('page.php', data, function(resp) {
$('#dataTable').html(resp);
});
});
});
Thanks everyone. I didn't want to reload the page as the data is retrieved from various databases and tables and is therefore expensive to run just to toggle viewing data I already have access to. Its working using the following code:
CSS:
tr.archivedrow {
display:none;
}
jQuery:
<script>
$(document).ready(function() {
$(".showarchived").on("click", function(event) {
if ($(this).hasClass("displayed")) {
$(this).html("Show Archived");
$(this).removeClass("displayed");
} else {
$(this).addClass("displayed");
$(this).html("Hide Archived");
}
$(".myTable .archivedrow").toggle();
});
});
</script>
HTML
<a class="showarchived" href="#">Show Archived</a>
<table width='100%' border="1" cellspacing="0" cellpadding="4" class="mytable">
<tr>
<th>Product</th>
<th>Supplier</th>
....
</tr>
<?php
if ($product->getDateArchived() != NULL) {
echo "<tr class=\"archivedrow\">";
} else {
echo "<tr> ";
} ?>
<td><?= $product->getProductName() ?></td>
<td><?= $supplier->getSupplierName() ?></td>
....
sorry if the title is a little.. vague i couldnt pin it down.
So i am developing a friend request system which works i guess similar in concept to facebook. So you get a request and it lists them without a page reload.
However i get the div 'refreshing' or so i think i cant test the php which is where i have a problem, i will post the relevent code and files below.
It may look a little long winded but it shouldnt be too bad in reality. My php code should keep executing the query which is looking at the database in the updateFriendBox.php however it doesnt seem to be doing this. My code may be messy as well so I apologise.
myaccount.php
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript" type="text/javascript">
function refreshDiv()
{
$.get('updateFriendBox.php', function(data){$('#refresh').html(data);});
}
$(function()
{
refreshDiv();
setInterval(refreshDiv, 5000);
});
function box(x){
if($('#'+x).is(":hidden")){
$('#'+x).slideDown(200);
} else {
$('#'+x).hide();
}
}
</script>
<?php
$addBox = '<div style="display:inline; padding:5px;">
Show/Hide Friend Requests
</div>';
// a bit further down in the code where its all called:
<? echo $addBox; ?></span>
<div class="friendSlide" id="fRequ" style="height:240px; overflow:auto;">Your friend requests: <br />
<div id="refresh"> <?php // this is where the refresh call is ?>
</div>
</center>
</div>
</div>
</div>
updateFriendBox.php:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript" type="text/javascript">
function acceptFriendRequest(x) {
var url = "friendParse.php";
$.post(url,{ request: "acceptFriend", reqID: x}, function(data){
$("#req"+x).html(data).show().fadeOut(5000);
});
}
function denyFriendRequest(x) {
var url = "friendParse.php";
$.post(url,{ request: "denyFriend", reqID: x}, function(data){
$("#req"+x).html(data).show().fadeOut(5000);
});
}
</script>
</head>
<body>
<?php
include 'dbc.php';
$sql = "SELECT * FROM friendRecu WHERE mem2='" . $_SESSION['user_id'] . "' ORDER BY id ASC LIMIT 10";
$query = mysql_query($sql)or die(mysql_error());
$num_rows = mysql_num_rows($query);
if($num_rows < 1) {
echo "No friend requests";
} else {
while($row = mysql_fetch_array($query)){
$requestID = $row['id'];
$req = $row['mem1'];
$sqlName = mysql_query("SELECT full_name FROM users WHERE id='$req'");
while($row = mysql_fetch_array($sqlName)){
$requesterName = $row['full_name'];
}
echo '<hr /><table width="100%", cellpadding="5"><tr><td width="17%" align="left"><div style="overflow:hidden; height:50px; color:white;"></div></td> <td width="83%">' . $requesterName . ' added you as a friend
<span id="req' . $requestID . '">
Accept
||
Deny
</span></td></tr>
</table>';
}
}
?>
I think you are having a problem because your updateFriendBox.php is returning too much. Remove all that inline JS code, place it in an include file, and include it from myaccount.php. You also shouldn't have <head> and <body> sections in your updateFriendBox.php file.
The ajax call here doesn't create a whole new page, you're getting additional HTML to add to the original page.
So the only thing you should have there is your SQL query, the loop, and your HTML output for each data row.
I'm trying to make Jquery delete function, here is the code, that I wrote -
$("a.delete").click(function(){
var target = $(this).attr("id");
var c = confirm('Do you really want to delete this category?');
if(c==true) {
$(target+"ul").delay(3000).fadeOut(200);
}
else {
return false;
}
});
Okay now to problem, when I press, button a with class delete, it correctly, shows up the dialog (and the id is correct), but after I press yes, it will delete it with php, without jquery fadeOut effect.
My php code -
public function deleteCategory() {
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
$id = (int)$_GET['id'];
$sql = "DELETE FROM `categories` WHERE `id` = '".$id."'";
mysql_query($sql);
}
}
Not sure where is the problem but I think it's inside $(target+"ul"), since I'm not sure, if it will add the target value and ul together.
In additional, how can I prevent the delete, if I press no? Currently, if I press no, it will still delete the category.
If you need any other information - ask.
Full code -
public function showCategories() {
if(isset($_SESSION['logged_in']) && isset($_SESSION['user_level']) && $_SESSION['user_level'] == 'admin') {
$sql = "SELECT * FROM `categories`";
$q = mysql_query($sql);
if(mysql_num_rows($q) > 0) {
?>
<div class="recentorders" style="display: none;" id="categoryBox">
<h5 class="colr">Categories</h5>
<div class="account_table" style="width: 688px;">
<ul class="headtable" style="width: 680px;">
<li class="order">ID</li>
<li class="action" style="width: 430px;">Category Name</li>
<li class="action nobordr">Options</li>
</ul>
<?php
while($row = mysql_fetch_array($q)) {
?>
<ul class="contable" style="width: 680px;" id="cat<?php echo $row['id']; ?>ul">
<li class="order"><?php echo $row['id']; ?></li>
<li class="action" style="width: 430px;"><?php echo $row['name']; ?></li>
<li class="action nobordr">Edit<a class="delete" id="cat<?php echo $row['id']; ?>" href="?action=delete&id=<?php echo $row['id']; ?>#">Delete</a></li>
</ul>
<?php
}
?>
</div>
</div>
<?php
}
else {
// No categories created, please create one first.
}
}
else {
header('location: account.php');
}
}
public function deleteCategory() {
if(isset($_GET['action']) && $_GET['action'] == 'delete') {
$id = (int)$_GET['id'];
$sql = "DELETE FROM `categories` WHERE `id` = '".$id."'";
mysql_query($sql);
}
}
If you're searching by ID you need to prefix your selector with a "#":
100% working sample on jsFiddle:
http://jsfiddle.net/E7QfY/
Change
$(target+"ul").delay(3000).fadeOut(200);
to
$('#' + target+"ul").delay(3000).fadeOut(200);
OR
$(this).closest('ul').delay(3000).fadeOut(200);
I would also modify the code to this:
$("a.delete").click(function(event){
if(confirm('Do you really want to delete this category?') == true){
$(this).closest('ul').delay(3000).fadeOut(200);
window.location.href = $(this).attr('href'); //OR use AJAX and do an event.preventDefault();
}
else
event.preventDefault();
});
It's because you are using an anchor tag that is causing a postback to the server which makes your page refresh before you see the animation. Make sure your anchor tag has an href defined as "#" to stop the postback from happening.
You should use something else than A tag e.g. span or div. And then after clicking this element make your fadeout and after it redirect to the PHP script that deletes your object:
$("SPAN,DIV.delete").click(function(){
var target = $(this).attr("id");
var c = confirm('Do you really want to delete this category?');
if(c==true) {
$(target+"ul").delay(3000).fadeOut(200);
window.location.href = URL TO PHP FILE
}
else {
return false;
}
});