Jquery works but only for a moment - javascript

When a row's select button is clicked, a confirmation box pops up.
Upon confirming the box, the select-button shall become disabled, and a message is appended that shall fade out.
Both, button disabling and message appending work but only for a second.
The button does not remain disabled. Also the message appends correctly but only for a second too.
I could change the fadeOut(Int) to any Integer but still the message only shows up for a second. Why does functionality only work for a second?
<script>
$('#button_<?php echo $platform->id; ?>').click(function() {
var choice = confirm('Please confirm that you wish to do the following Platform: <?php echo $platform->company; ?>');
if (choice == true) {
$('#button_<?php echo $platform->id; ?>').prop("disabled", true);
$('.job_confirm').css('visibility', 'visible');
$('.job_confirm').append('The Job has been added to your Userarea -->');
$('.job_confirm').fadeOut(5000);
}
});
</script>

try event.preventDefault();
<script type="text/javascript">
$('#button_<?php echo $platform->id; ?>').click(function(event) {
event.preventDefault();
var choice = confirm('Please confirm that you wish to do the following Platform: <?php echo $platform->company; ?>');
if (choice == true) {
$('#button_<?php echo $platform->id; ?>').prop("disabled", true);
$('.job_confirm').css('visibility', 'visible');
$('.job_confirm').append('The Job has been added to your Userarea -->');
$('.job_confirm').fadeOut(5000);
}
});
</script>

Related

Toggle with an IF statement needs two clicks to fire

This is a toggle of a ‘follow-unfollow’ button of a Twitter like following system. When the button has the class unfollow it takes two clicks to trigger to fire. When the button has the class follow it fires in one click. This is related to the if statement. If the script does not need to run through the if statement it fires well on one click which is the part of the else section of the script. It is worth noting, that when two clicks are needed, if there is no refresh in the page, you then can toggle back and forth with just one click as it is supposed to be. Do you know how can I avoid using two clicks when the script needs to go through the if statement?
Thanks in advance.
<button class="followUnfollow" id="boton<?php echo $member->id; ?>" type="button" data-member_id="<?php echo $member->id; ?>" user_id="<?php echo $id;?>"> <?php echo $status; ?> </button>
<script>
$(document).ready(function() {
$("#boton<?php echo $member->id; ?>").on('click', function() {
var memberId = $(this).attr('data-member_id');
var userId = $(this).attr('user_id');
if($("#boton<?php echo $member->id; ?>").hasClass('unfollow')) { // TWO CLICKS TO FIRE
$.get("follow_actions.php", {unfollow_id:memberId, user_id:userId} , function(data) {
});
$(this).html('follow');
$(this).removeClass('unfollow').addClass('follow');
} else { // WORKS WELL, ONE CLICK TO FIRE
$.get("follow_actions.php", {follow_id:memberId, user_id:userId} , function(data) {
});
$(this).html('unfollow');
$(this).removeClass('follow').addClass('unfollow');
}
});
});
</script>

How to get Delete confirmation from user before the delete query using php

Below is my delete code using php,I want to get confirmation after delete link was clicked from the user using php.
<?php
include('conn.php');
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[st_id]'");
if($query)
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Succesfully deleted')
window.location.href='mark-details1.php';
</SCRIPT>");
}
else
{
echo "Check your Server";
}
?>
Please anyone can tell me how to do this? Thanks in advance!
you can use this javascript event on your html tag.
onclick="return confirm('you sure?');"
you can also use this :
if your link will send a get like "?delete=(id)"
<?php
include('conn.php');
if(isset($_GET['delete']) && is_numeric($_GET['delete'])==1){
echo (a page with a form with confirmation question content that will sent a get for example (?checked_delete=(id)));
}elseif(isset($_GET["checked_delete"]) && is_numeric($_GET["checked_delete"])==1){
// TODO : deleting record.
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[checked_delete]'");
header("location:mark-details1.php")
}else{
echo (normal page);
}
?>
I suggest using Ajax. Here I will use jQuery to do it
<a class="wh" data-id="<?=$rows['student_id']?>" href="edit-mark.php?st_id=<?=$rows['student_id']?>" title="Edit">Edit Marks</a><span class="confirmation"></span>
using
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
$.get(this.href,function(data) { // does the student still exist?
if (confirm("delete" +data+"?")) {
$.get("otherphp.php?st_id="+$(this).data("id"),function(data) {
$(this).next().html(data); // show response
});
}
});
});
});
Or to hide the href from spiders
<a class="wh" href="onlyworkswithjavascript.html"
data-id="<?=$rows['student_id']?>" data-href="edit-mark.php?st_id=<?=$rows['student_id']?>"
title="Delete">Delete Marks</a><span class="confirmation"></span>
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
if (confirm("delete" +data+"?")) {
$.get($(this).data("href"),function(data) {
$(this).next().html(data); // show response
});
}
});
});

Alert msg box display blank page behind

I am displaying a message box at the client browser. It works fine but i see a blank page at the back when this alert message box pops up. But I want to the user to see his browsing page always and pop up this message box. Here is my code.`
if($_POST['submit']=="Save")
{
$language=$_POST['pbx_lan'];
if($language!="")
{
$query="update `account_detail` set `prompt_language`= '$language' WHERE `org_id`='".$_SESSION['account_id']."'";
$result = $mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
// setcookie("msg","Language Successfully Updated",time()+5,"/");
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
// header("location:".SITE_URL."index.php?view=view_service");
}
else
{
setcookie("err","No Language Selected.Please Select Language",time()+5,"/");
header("location:".SITE_URL."index.php?view=view_service");
}
}`
When an confirm() is called the page stops loading. When you add the confirm() at the end of the page, the page is loaded before the confirm() is shown
<body>
TEST123
<script>
confirm('test');
</script>
</body>
instead of
<body>
<script>
confirm('test');
</script>
TEST123
</body>
For your code this means that
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
has to move to the bottom of your body tag
Also copy the required if statements so that the code is only executed when neccesary.
The complete code at the bottom of your body tag should be something like this:
if (!empty($language) && $_POST['submit'] == "Save") {
echo '<script>
confirm("You have selected '.$language.'");
window.location = "'.SITE_URL.'index.php?view=pbx_prompts."
</script>';
}

How to get input of a dialog box to php

When a user wants to delete a row from a database via my webpage, I want to prompt a message-box asking, "Are you sure you want to delete?". Then in my php code, I want to track if the user press Ok or Cancel and execute the delete operation as shown bellow. I am new to web-programming and any help would be appreciated.
<?php
.
.
.
if (user wants to delete a row from database)
{
echo "<script type=\"text/javascript\"> confirm(\"Are you sure you want to delete?\"); </script>";
if(user press OK to delete")//How can I get input from the dialog box?
{
$deleterow = "DELETE FROM ElectronicShop WHERE WorkOrder = $i";
$datadelete = sqlsrv_query($connectString, $deleterow) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));
if($datadelete)
echo "<br>you have deleted: $i Successfully";
else
echo "<br>could not be deleted: $i";
}
else
echo "User pressed Cancel";
}
?>
You can simply add the question to your onclick event
...
or with jquery
...
$(".delete-confirm").click(function(e) {
if (!confirm("are you sure"))
e.preventDefault();
});

jQuery.post: I can't post more than one time unless I refresh the page

I made a small script to record bugs in my projects
this picture will show you a file name and the bugs it have.
then I used javascript-jquery to save myself some time.
so when I click on an error (the red ones) it will turn to green, and if i click on the fixed error (the green ones) it will turn to red.
the problem is sometime I click on the wrong error and can't turn it back unless i refresh the page. ex. if I click on error like tags plugin (* it turns green *) then I click on tags plugin again to turn it back to red it won't turn back, unless I refresh the page then click on it again to turn it.
I checked my code its fine I don't know what the problem is.
in while (fetching the error)
while ($error = mysql_fetch_assoc($find_errors))
here I print the errors
<?php
switch ($error['status']) {
case 'notfixed':
$error_class = "error";
$error_link = "fix".$error['id']."";
break;
case 'fixed':
$error_class = "success";
$error_link = "unfix".$error['id']."";
break;
}
echo "
<a href='".$_SERVER['PHP_SELF']."?del=".$error['id']."'>
<span class='del'>×</span>
</a>
<input id='errorid".$error['id']."' value='".$error['id']."' type='hidden'>
<input id='errorname".$error['id']."' value='".$error['name']."' type='hidden'>
<div id='newdiv".$error['id']."'>
<a id='".$error_link."'>
<span class='".$error_class."'>".$error['name']."</span>
</a>
</div>
<div class='clear'></div>";
?>
inside the same loop before printing the errors I printed this which the javascript
<script type='text/javascript'>
$('document').ready(function(){
$("#fix<?php echo $error['id'] ?>").click(function(){
var errorid = $("#errorid<?php echo $error['id']?>").val();
jQuery.post('fix_error.php',{posterrorid : errorid},
function(data, textStatus){
if(data == 1){
setInterval(function(){
$("#newdiv<?php echo $error['id']?>").html("<a id='unfix<?php echo $error['id']?>'><span class='success'><?php echo $error['name']?></span></a>");
}, 1000);
}else{
setInterval(function(){
$("#newdiv<?php echo $error['id']?>").text('error insert');
}, 1000);
}
});
});
$("#unfix<?php echo $error['id'] ?>").click(function(){
var errorid = $("#errorid<?php echo $error['id']?>").val();
jQuery.post('unfix_error.php',{posterrorid : errorid},
function(data, textStatus){
if(data == 1){
setInterval(function(){
$("#newdiv<?php echo $error['id']?>").html("<a id='fix<?php echo $error['id']?>'><span class='error'><?php echo $error['name']?></span></a>");
}, 1000);
}else{
setInterval(function(){
$("#newdiv<?php echo $error['id']?>").text('error insert');
}, 1000);
}
});
});
});
</script>
Try using the syntax like this :
$("#fix<?php echo $error['id'] ?>").live('click',function(){
.....
});
$("#unfix<?php echo $error['id'] ?>").live('click',function(){
.....
});
Because when you click for the first time, you are replacing the html code dynamically,
so for the next time, when you click that dynamically generated html code will not be noticed, so you have to use .live function.
REFER

Categories

Resources