Refresh page after "OK" alert popup with javascript - javascript

I tried to refresh the page after deleting an item from my back-end list.
Here's the HTML
<a href="index.php?id=<?php
echo $array[id_news];
?>&?action=delete" onClick="return conferma()">Remove</a>
Here's the PHP
if ($_POST['action'] = "delete") {
$sql="DELETE FROM news WHERE id_news=".$_GET['id'];
if (!mysql_query($sql)) {}
}
Here's the Javascript
function conferma() {
return confirm('Confermi di voler cancellare la news selezionata?');
window.location.reload();
}
The popup appears but after clicking OK the page don't refresh.

You are returning on the confirm() line, so the reload never gets executed. Change to:
function conferma() {
if(confirm('Confermi di voler cancellare la news selezionata?')){
// call the delete script via ajax now.....
window.location.reload();
}
return false;
}
It looks like you need to use AJAX to call the delete script, because otherwise the reload will happen and the anchor's href will never be visited.

You are return the boolean result from confirm dialog action, and then reloading, so the script never reach the reload
function conferma() {
ritorno = confirm('Confermi di voler cancellare la news selezionata?');
if(ritorno)
window.location.reload();
else console.log('ok nothing to do');
}

To Fix:
if ($_POST['action'] = "delete") {
by
if ($_POST['action'] == "delete") {

Related

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!

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

Reloads page once then reloads again without content change

I have a bit of javascript that posts to a php, every second, which checks a schedule and basically returns "refresh" if the start time is met then returns refresh when the end time is met.
if "refresh" it reloads the page showing what its suppose to.
function checkSchedules(){
if(sch_timer)
clearInterval(sch_timer)
$.ajax({
url:'checkSchedule_test.php?mediaID='+mediaIDArray[schCount],
type:'GET',
complete: function (response) {
if(response.responseText == "refresh"){
location.reload(true);
}
},
error: function () {
alert('Sorry there was an error!');
},
})
sch_timer = setTimeout(checkSchedules,1000);
schCount = (schCount >= urlArray.length - 1)? -1 : schCount
++schCount
}
in the php
if ($current_date>=$startDate && $current_date<=$endDate){
if ($current_time >= $startTime && $current_time <= $endTime){
$timeout = $endTime - $current_time;
if ($current_time >= $startTime && $current_time <= ($startTime+3000)){
echo "refresh";
}
if($timeout<=3000){
echo "refresh";
}
}
}
when the start time is met it refreshes fine, works great.
then when the end time is met it refreshes but the contents the same.
if I hit refresh it works fine.
I've tried different methods of reload() (document.reload, etc...)
Has anyone got any ideas?
Cheers
use console to understand what is returning from server
use debug() or console.log() for understand what is returning from php ... reload script is correct issue will be during ajax

log out confirmation box in 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);
}

Categories

Resources