I am using php and javascript. I want to delete a record from database after a giving "Yes" and "No" confirmation to user. I am using confirmbox by google but don't know what should be my code structure inside that. I am new to programing. Can any one help me please
How can I add this php function on javascript
function deleteRecord($id)
{
$sql = "DELETE FROM user_database WHERE user_id = ".$id;
mysql_query($sql) or die("Can not delete");
}
And on page
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
}
</script>
</head>
<body>
<input type="button" onclick="show_confirm()" value="Show a confirm box" />
You can use a simple yes/no confirmation dialogue with javascript :
<a href="job.php?do=delete&id=1"
onclick="return confirm('Are you sure to delete this record?');">Delete</a>
if user confirmed , you can precessed with your function to delete the record, else nothing going to happen in your phpside.
Related
page:content display.php
<td width="100"><a onclick="return confirmSubmit()" href="delete.php?id=<?php echo $row['id']; ?>&table=labour_details&return=content_display.php"><img src="../images/delete.png" border="0" alt="delete"></a></td>
page:delete.php
if(isset($_GET['id']) && isset($_GET['table']))
{
$id=$_GET['id'];
$tablename=$_GET['table'];
$return=$_GET['return'];
if($tablename=="labour_details")
{
$sql=mysql_query("SELECT * FROM `labour_details` WHERE `id`='$id'");
$row_1=mysql_fetch_array($sql);
$labour_name= $row_1['labour_name'];
if($labour_name!="")
{
$str=mysql_query("DELETE FROM `labour_details` WHERE `id`='$id'");
if($str)
{
header("location:$return?msg=Record Deleted Successfully!&id=$id");
}else{
echo "Problem in deleting :";
}
}
}
}
my problem is where to add alert so that before deleting record it show javascript massage if allow then it will delete record.
function confirmSubmit()
{
var agree=confirm("Are you sure you wish to Delete this Entry?");
if (agree)
return true ;
else
return false ;
}
This line will show confirm dialogbox before redirect.
<a onclick="return confirm('Are You sure?')">...</a>
First you need to create the confirmSubmit() function
<script>
function confirmSubmit()
{
if (confirm('Are you sure you want to delete this?'))
{
return true;
}
return false;
}
</script>
Then you attach that to your A tag, as you have done..
blah
You're probably looking for confirm.
Then, the body of the confirmSubmit function would look something like:
function confirmSubmit() {
return confirm ("Are you sure you want to do this?");
}
This works due to how event handlers work in javascript. Confirm returns true when the user clicks OK and false when they click Cancel.
By returning a true/false value from an event handler function you tell the window whether to keep processing that event. Returning false (which happens when the user clicks Cancel) tells the browser to stop processing the event and not follow the link.
<td width="100"><a onclick="confirmSubmit(id)"><img src="../images/delete.png" border="0" alt="delete"></a></td>
function confirmSubmit(delete_id)
{
var r=confirm("Are you sure you want to delete?");
if(r==true)
{
window.location.href = "delete.php?id="+delete_id"; ?>&table=labour_details&return=content_display.php"
}
}
<a onclick="return confirm('Are you sure?')" href="delete.php">delete</a>
I am displaying a JavaScript confirmation dialog that asks if the user is sure they wish to delete the record. However, even when the user clicks "yes" the query is still executed and the record is still deleted what can be the problem here is the code:
<script>
function deleletconfig() {
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
} else {
alert("Record Not Deleted")
}
}
</script>
So even if I click cancel the query/record gets deleted. How can I stop this from happening what am I doing wrong? Still a newbie at JS!:(
You must use the return value of the confirm dialog:
echo"<form method = \"post\" action =\"change.php?change=$productid\">";
echo// form fields here!...
echo"<input type=\"submit\" name = \"delete\" value=\"Delete\" onclick=\"return deleletconfig()\" />";
if (isset($_POST['delete'])){ //delete clicked
//get variables here
//run query delete record from xyz where id=$id
}
<script>
function deleletconfig(){
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("record deleted")
}else{
alert("Record Not Deleted")
}
return del;
}
</script>
See the changes in "onclick" and "deleletconfig".
Create a function and call it:
<script type="text/javascript">
function confirmation() {
return confirm('Are you sure you want to do this?');
}
</script>
Delete
Just copy and paste
it gives an alert box to confirm whatever you want your link to do.
Alert returns true or false based on selected answer, and then will either do what you've set your link to do or will not do what your link is supposed to do.
<td>
<a class="delete_button" href="#">Delete</a>
</td>
<script type="text/javascript">
$('.delete_button').click(function(e){
var result = confirm("Are you sure you want to delete this user?");
if(!result) {
e.preventDefault();
}
});
</script>
Instead of
if (del==true){
do
if(del) {
add return = true for deletion, return = false for no deletion.
i'm currentrly coding in php and when i click a button the message should me Would you like to delete this entry?
This is where I'm currently at
<button onClick="alert('Sure to delete entry?')">Remove Entry</button>
And sure it works. But when I press "x" to close the alert window it still deletes the entry.
How can you make a popup window that has the "Cancel" opition?
I would really appreciate the help! :)
if (confirm("Your question")) {
// do things if OK
}
Use confirm
Click here for examples
You can do this using the following JavaScript code:
if (confirm('Sure to delete entry?') == true) {
//write your code here to delete
}
The HTML
<a href="delete.php?delete=<?php echo $row['guestbook_id'];?>"
onclick="return areYouSure()">Remove Entry</a>
And the JS
function areYouSure()
{
var con = window.confirm("Are You Sure?");
if(con)
{
return true;
}
else
{
return false;
}
}
So I am trying to submit a variable and the name of the variable via a form. I switched a button from submit to button because I need additional validation.
Anyway, here's the button now:
<button type="button" onclick="subForm()" name="del" id="deletebutton" value="'.$org.'">Delete</button>
Here's my current validation:
<script type="text/javascript">
function subForm()
{
if(confirm("Are you sure you want to delete this?"))
document.forms["addorg"].submit();
else
return false;
}
</script>
And here's my script on the other side:
if (isset($_POST["del"]) && ($_POST['del'] !== '')) {
$del = mysql_real_escape_string(html2txt($_POST['del']));
$resfile = mysql_query('SELECT file_loc from organization WHERE org_id = '.$del);
$org_name = mysql_real_escape_string(html2txt($_POST['orgname']));
if (!$resfile)
header('Location: '.$admin.'?error=query');
while ($filerow = mysql_fetch_array($resfile)) {
$fileplace = $filerow['file_loc'];
unlink(".".$fileplace);
rmdir($org_name);
}
mysql_query("DELETE from organization where org_id='".$del."'");
header('Location: '.$admin);
}
It is not currently deleting the records that I want. How do I pass along the "del" name to the other page?
You can use <input type="hidden">:
echo '<input type="hidden" name="org_id" value="'.$org_id.'" />'
This should render something like:
<input type="hidden" name="org_id" value="1" />
Using this code you can access the hidden field data using:
$org_id = $_POST['org_id'];
instead use onsubmit
<form method='POST' onsubmit='return subForm();'>
and
<script type="text/javascript">
function subForm()
{
if(confirm("Are you sure you want to delete this?"))
return true;
else
return false;
}
</script>
edit:
you can also change
if (isset($_POST["del"]) && ($_POST['del'] !== '')) {
to
if ( !empty($_POST['del']) ) {
but i think this line is your problem
$resfile = mysql_query('SELECT file_loc from organization WHERE org_id = '.$del);
try
$resfile = mysql_query("SELECT file_loc from organization WHERE org_id = '".$del."' ");
Looking at http://www.w3schools.com/tags/tag_button.asp suggests that the 'name' of a button isn't submitted as opposed to its 'value' or 'text'. Why not use an input of type hidden as just suggested?
I suggest you rethink using a form at all and consider AJAX. This would solve the problem of knowing which button was clicked and the page doesn't need to reload.
Here is a sample of what you're trying to do:
<html>
<head>
<script type="text/JavaScript">
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
function deleteOrganization(orgID)
{
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
// in your PHP file, have it echo something like "SUCCESS" or "FAIL", and the response will be alerted here
alert(xmlhttp.responseText);
refreshList(); // either call another function to update the list or return the new list after the success/fail response.
}
};
xmlhttp.open("GET", "delete.php?orgID="+ orgID, true);
// OR - xmlhttp.open("GET", "page.php?action=DELETE&orgID="+ orgID, true);
xmlhttp.send();
}
function refreshList()
{
// either delete the row with JavaScript or make another AJAX call to get the new list after the entry was deleted.
}
</script>
</head>
<body>
<button onclick="deleteOrganization('<?php echo $org; ?>')">Delete</button>
</body>
</html>
On my website I give users an option to subscribe to blog authors. Right now it is a php process and requires page refresh each time user clicks "SUBSCRIBE" or "UNSUBSCRIBE" button. I was thinking that it is a time to make this process sort of AJAX based, so when users click "SUBSCRIBE" or "UNSUBSCRIBE" buttons there would be no page refresh, but their subscription arrays would be updated. therefore buttons will change respectively e.g. if user clicks "SUBSCRIBE" button it will change to "UNSUBSCRIBE" and backwards.
The problem is that I never used AJAX before and I can't find useful information to achieve this specific task, because there is a lot of it.
So could anyone suggest how to make this process sort of AJAX, so no page refresh takes place? If possible jQuery based solution would be great.
HTML & PHP for Buttons
//SUBSCRIBE Button
<?php if (($isLogedIN) && ($canSubscribe) && (!$isBlogOwner)) { ?>
<form id="subscribeform" name="subscribeform" method="post" action="blog.php?id=<?php echo $id;?>">
<input type="submit" name="subscribe" value="Subscribe"/>
</form>
<?php } ?>
//UNSUBSCRIBE Button
<?php if (($isLogedIn) && ($canUnSubscribe) && (!$isBlogOwner)) { ?>
<form id ="unsubscribeform" name="unsubscribeform" method="post" action="blog.php?id=<?php echo $id;?>">
<input type="submit" name="unsubscribe" value="Unsubscribe" />
</form>
<?php } ?>
PHP to update database records
//Subscribe
if (isset $_POST['subscribe'])){
//First Update Visitors Subscription Array
if($subscription_array != ""){
$subscription_array = "$subscription_array,$blogauthid";
} else {
$subscription_array = "$blogauthid";}
$updateSubscription_array = mysql_query("UPDATE members SET subs='$subscription_array' WHERE id='$reader'") or die (mysql_error());
//Then Update blog writers subscribers array
$subArray7 = mysql_query("SELECT subscribers FROM members WHERE id='$blogauthid' LIMIT1");
while($subrow7=mysql_fetch_array($subArray7)) {subscription_array7 = $subrow7["subscribers"];}
if ($subscription_array7 !="") {
$subscription_array7 = "$subscription_array7,$reader";
} else {
$updateSubscription_array7 = mysql_query("UPDATE members SET subscribers='$subscription_array' WHERE id='$blogauthid'") or die (mysql_error());
header("location: blog.php?id=$blogid");exit();
//Unsubscribe
if (isset($_POST['unsubscribe'])){
//First Update visitors subscription array
foreach ($subscription_array2 as $key => $value) {
if ($value == $blogauthid)
unset($subscription_array2[$key]);
}
}
$newSubArray = implode(",", $subscription_array2);
$updateSubscription_array = mysql_query("UPDATE members SET subs='$newSubArray' WHERE id='$reader'") or die (mysql_error());
//Than update blog writers subscription array
$subArray9 = mysql_query("SELECT subscribers FROM members WHERE id='$blogauthid' LIMIT 1");
while($subrow9=mysql_fetch_array($subArray9)) {subscriber_array9 = $subrow9["subscribers"];}
$subscriber_array9b = explode(",", $subscriber_array9);
foreach ($subscribe_array9b as $key9 => $value9) {
if ($value9 == $reader) {
unset($subscriber_array9b[$key9]);
}
}
$newSubArray9 = implode(",", $subscriber_array9b);
$updateblogSubsArray = mysql_query("UPDATE members SET subscribers='$newSubArray9' WHERE id='$blogauthid'") or die (mysql_error());
header ("location: blog.php?id=$blogid");exit();
Basically you want to have a JavaScript function like this:
function subscribe(id)
{
if (window.XMLHttpRequest)
{// code for real browsers
xmlhttp=new XMLHttpRequest();
}
else
{// code for abominations
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.status==200 && xmlhttp.readyState==4)
{
document.getElementById("subscribeStatus").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","blog.php?id="+id,true);
xmlhttp.send();
}
Call it and pass it the variable (id) you want with a button like this on the frontend.
<input type="button" name="click" value="Subscribe" onmousedown="subscribe(document.getElementById('id').value);">
Embed the user's ID as a hidden form field with id = "id" (or whatever).
Then on the PHP side (blog.php) just handle it with $_GET instead of $_POST. And duplicate it for unsubscribe or find a way to work it together using a switch (or if) statement.
Hope that helps.
This could be optimized a little, but it's functionality is more clear at each step and it should work. This is Jquery $.post, as requested.
You only need one form element:
<form id="subscribeform" name="subscribeform" method="post" action="blog.php?id=<?php echo $id;?>">
<input type="submit" name="subscribe" value="Subscribe"/>
</form>
At the bottom of your document, just before the closing body tag, link the jquery library, followed by the script:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#subscribeform input:submit').click(function() {
// $.post is the jquery shorthand method for $.ajax - it always uses POST
$.post(
// url
'blog.php?id=<?=$id?>',
// serialized form data to POST
$('#subscribeform').serialize(),
// success callback toggles form value
function(data) {
if ($('#subscribeform input:submit').val() == 'Subscribe') {
$('#subscribeform input:submit').
attr('name','Unsubscribe').val('Unsubscribe');
}
else {
$('#subscribeform input:submit').
attr('name','Subscribe').val('Subscribe');
}
}
);
});
});