log out confirmation box in javascript - javascript

I have a php code for logout page but i need to have a confirmation box pop-up first if i click the navigation box to logout...if I want to logout or not...can any1 help me?? please
code:
<?php
include("connection.php");
// Delete certain session
unset($_SESSION['username']);
// Delete all session variables
// session_destroy();
$uid = $_POST['uid'];
$stat="UPDATE users SET status='logout'";
mysql_query($stat);
// Jump to login page
header('Location: index.php');
?>

maybe this is what you are trying to achieve?
var logout = confirm("Are you sure to logout?");
if(logout){
location.href = "pathtologout.php";
}

Most simple:
Logout

If you just need a simple confirmation, you can do this
$('#logout').click(function(){
var reallyLogout=confirm("Do you really want to log out?");
if(reallyLogout){
location.href="path/to/logout/file.php";
}
});
If you can't use jQuery(!), you can use pure Javascript to attach the event handler
function logout(){
var reallyLogout=confirm("Do you really want to log out?");
if(reallyLogout){
location.href="path/to/logout/file.php";
}
}
var el = document.getElementById("logout");
if (el.addEventListener) {
el.addEventListener("click", logoutfunction, false);
} else {
el.attachEvent('onclick', logoutfunction);
}

Related

Confirmation popup Box according to a condition on submit button

I am using Drupal 7. In my list I have 2000 subscribers. Now I want a confirmation box before executing the form. The only issue is If I have more than 20 users I want the confirmation form to ask the question" Do you want to continue with 20 users' If the answer is yes, then perform some php operation on it else go to the main page.
function player_operation_display_form_submit($form, &$form_state)
{
if($total_players > 20){
echo '<script type="text/JavaScript">
var a = confirm("Do you want to continue with more than 20 users");
if(a){ //performing some operation
$operation[] = array('Assign_goals', array($records_value,
$records_key, $assign_username));
}
else{
drupal_goto('exit_operation');
die();
}
</script>';
}
Now I understand that $operation[] and exit_operation are my php functions and wont execute in my js file. I just want to know how to execute these functions.
You can do this on js file just before submit the form.
<script>
$(function() {
var total_players = <?php echo $total_players;?>;
$('#form').submit(function() {
if(total_players > 20){
if (confirm("Do you want to continue with more than 20 users?")){
$('form').submit();
}else{
return false;
}
}
});
});
</script>
For more appropriate solution please share your html form code and java script code.

PHP header(Location:'index.php') and javascript not being executed

My problem is, when the user logout. The login page is being called and the index page is showing but the url is saying
https://mysite/logout.php
instead of
https://mysite/index.php
which means my javascript files included in the index.php ain't being loaded, so you cant log in again without refreshing the page manually.
Link in home.php the page you reach after login
<p class="mc-top-margin-1-5">Logout</p>
I have the following logout page (logout.php)
<?php
session_start();
require_once 'php/class/class.user.php';
$user = new USER();
if(!$user->is_logged_in())
{
$user->redirect('index.php');
exit;
}
if($user->is_logged_in()!="")
{
$user->logout();
$user->redirect('index.php');
exit;
}
?>
my user functions as follow (class.user.php)
public function is_logged_in()
{
if(isset($_SESSION['userSession']))
{
return true;
}
}
public function redirect($url)
{
header("Location: $url");
}
public function logout()
{
session_destroy();
}
what am I missing?
Your function is_logged_in() returns a boolean, TRUE, but you are checking the return value with:
if($user->is_logged_in()!="")
That is, checking if it's an empty string.
Also, and more importantly, is_logged_in() doesn't return anything if the user is not logged in. That function should be something like:
public function is_logged_in()
{
if(isset($_SESSION['userSession']))
{
return true;
}
else { return false; }
}
And the check should be something like:
if(!$user->is_logged_in())
so i ended up with a not very nice solution:
I changed the link in home.php
`<p class="mc-top-margin-1-5">Logout</p> `
To a button
<button id="btn-logout">Logout</button>
and added its functionality in a jquery function
$(function(){
$("#btn-logout").bind('click', function () {
window.location.href = "http://example.com/logout.php";
})
});
my logout.php ended up looking like
<?php
session_start();
unset($_SESSION['user_session']);
session_destroy();
?>
<html>
<head>
<script>
window.location.href = "https://example.com/index.php";
</script>
</head>
<body>
</body>
</html>
So going from refresh page in php using header. I ended up with js and jquery. Its not a nice solution above, but it works!

Updating a calculated field in the parent record after submitting/updating child record in a modal dialogue

I have a bit of an issue updating a calculated field of the parent form when submitting/updating a child record with a bootstrap 3 modal dialogue, from within the parent form.
I have this function in parent.js file:
function update_lineitems_subtotal() {
var qid = $j('[name=SelectedID]').val();
if (!isNaN(qid)) {
$j.ajax({
url: 'hooks/ajax_quote_subtotal.php',
data: {qid: qid},
success: function(data) {
$j('#LineItemsSubtotal').val(data);
$j('#Shipping').keyup();
}
});
} else {
$j('#LineItemsSubtotal').val('0');
$j('#Shipping').keyup();
}
};
And this is the ajax_quote_subtotal.php that runs with the ajax call above:
<?php
$currDir = dirname(__FILE__) . '/..';
include("$currDir/defaultLang.php");
include("$currDir/language.php");
include("$currDir/lib.php");
/* grant access to users with access to quotations table */
$od_from = get_sql_from('quotations');
if(!$od_from){
header('HTTP/1.0 401 Unauthorized');
echo "You do not have permission to access this page. If this behaviour is unexpected, please contact the system administrator";
exit;
}
$qid = intval($_REQUEST['qid']);
if(!qid) exit;
$parts_subtotal = sqlValue("select sum(Subtotal) from product_line_items where QuotationID='{$qid}' ");
$service_subtotal = sqlValue("select sum(Subtotal) from service_line_items where QuotationID='{$qid}' ");
$subtotal = $parts_subtotal + $service_subtotal;
sql("update quotations set LineItemsSubtotal='{$subtotal}' where QuotationID='{$qid}' ", $eo);
echo number_format($subtotal, 2);
?>
This is the function from child.js that closes the modal dialogue when the form is submitted and runs the update_lineitems_subtotal function from the parent.js
if($j('input[name=Embedded]').val()==1){
$j('form').submit(function(){
window.parent.jQuery('.modal').modal('hide');
setTimeout(function(){
parent.update_lineitems_subtotal();
}, 100);
})
}
This works as long as I do not remove setTimeout,however I do not feel like it is a good solution.
If the setTiemout is removed the record is created but it looks like the function runs before the child record is submitted, so the value of the subtotal field in the parent excludes the latest added record.
I have also tried this:
if($j('input[name=Embedded]').val()==1){
$j('form').submit(function(e){
e.preventDefault();
this.submit();
window.parent.jQuery('.modal').modal('hide');
parent.update_lineitems_subtotal();
})
}
This had similar results in Chrome and refused to even create a child record in Firefox.
Any help would be appreciated.
P.S. I am new to this, if you have time to explain the code you may suggest it would be greatly appreciated.

Want to load popup on homepage only using jQuery in wordpress

I am creating a popup plugin in WordPress. In my plugin, a popup opens on every page but I just want to show it on home page.
I know how to get it in PHP but don't know hot to get the home url in jQuery.
setTimeout(function(){
if($.cookie('SFPopup') == null){
$('#sfp_Modal').modal('show');
}else{
$('#sfp_Modal').modal('hide');
}
}, 3000);
This is how I am showing my popup using a cookie condition but I also want to show it only on home page not inner page.
This is my working code
<?php
function sf_display_popup(){
if (is_front_page() == true) {
?>
<script>
jQuery(document).ready(function($) {
$.cookieBar();
setTimeout(function(){
//var site_url = '<?php echo home_url(); ?>';
if($.cookie('SFPopup') == null || $('body').hasClass('home')){
// alert(site_url);
$('#sfp_Modal').modal('show');
}else{
$('#sfp_Modal').modal('hide');
}
}, 3000);
});
</script>
<?php
}
}
?>
try with checking body for class home. If body have home class it is home page.
if ($('body').hasClass('home')) { // show pop up }
I might be completly off, but wouldn't it be enough to check if document.location.pathname == "/" ?
That way you can check whether the adress is www.domain.com (<- would be root) or www.domain.com/something.
So if document.location.pathname == "/" you can execute your script and add the popup.

Logout and Delete Cookies [Redirect to main page]

When user clicks the logout button from the main page, the cookies will be deleted and they will be redirected to window.location = url; . Even if user does not login, but instead clicks logout, they will still be redirected to window.location = url;. My codes are as below, I can't seem to logout even if I click the logout button and I will stay at the main page while being logged in. Can anyone tell what is wrong? I am new to JavaScript and I need help regarding this topic.
$('.logout-btn').click(function(e){
e.preventDefault();
if(isset($_COOKIE['REFERER']) && $_COOKIE['REFERER'] != '') {
window.location = url;
}
else {
$.post(outurl, function( data ) {
}).then(function(r){
$('#popup_ok, .x-close').bind( "click", function() {
window.location = url;
});
if(r.result == 1){
popup_msg('Failed', r.msg);
}
else{
popup_msg('Success', r.msg);
setTimeout(function(){
window.location = url;
},2000);
}
});
}
});
try this code this will execute when user close the tab or close the browser it will automatically destroy session and cookies stored
<body onbeforeunload='destroySession()'>
</body>
<script type='text/javascript'>
function destroySession()
{
$.ajax({
url: 'process/logout.php'
});
}
</script>
logout.php
<?php
session_start();
unset($_SESSION['id']);
header("location:../login.php");
?>
specify the path of your file at place of login.php

Categories

Resources