I Have A File named as modal.php Which Contains Two Modal
Login Modal
Feedback Modal
Login Modal Runs in Every 5 Seconds if user isn't logged in.and feedback modal opens on click.
And on My website i have 20 pages and in all pages i have included modal.php So the problem i getting is in those 20 pages i have a page named with signin.php. i also include here an modal.php
So here is code for signin so i dont want to show signin modal here.
So how can i do that ?
Modal Opening Code in modal.php is
$(window).on("load", function () {
<?php if(!isset($_COOKIE['loginuser'])){ ?>
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
And I Tried
<?php if(basename(__FILE__) != 'free-listing-new.php') { ?>
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
<?php } ?>
But This Not working and modal will be shown. i think its maybe because file is included. So What do i have to do
I think your trouble lies someplace else. Your code works. Here is how I tested the same. I included the modal in a modal.php only.
<script>
$(window).on("load", function () {
<?php if(!isset($_COOKIE['loginuser'])){ ?>
console.log($('#signup_mask_popup'));
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
<?php
$cookie_name = "loginuser";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<?php }else { ?>
console.log("cookie is set.");
<?php } ?>
<?php if(basename(__FILE__) != 'free-listing-new.php') { ?>
console.log("here");
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
<?php } ?>
});
</script>
Related
So I have this PHP page, with at the start some unimportant stuff and a session opening :
<?php
session_start();
?>
I want the disconnect button to appear only if the user is already connected, which means that $_SESSION['Pseudo'] is set. If this is the case, I create a button, and bind to that button a function to destroy the session. The problem is, each time I go on this page, the session is destroy, even though it's in a condition.
Here's my code :
<?php
if (isset($_SESSION["pseudo"])) {
echo '<p><button type="button" id="deco">Disconnect</button></p>';
}
?>
<script>
let btn = document.getElementById('deco');
if (btn) {
btn.addEventListener('click', updateBtn);
function updateBtn() {
<?php
session_destroy();
?>
window.location.href = "/"
}
}
</script>
Any clue ?
You cannot combine PHP within Javascript like that.
When you're doing , it's not a part of the Javascript. It's PHP code and is executed as part of the PHP code in the file.
This means that it has nothing to do with the javascript aspect. your updateBtn function is effectively, when the page source code loads:
function updateBtn() { window.location.href = "/" }
What you really want to do is make it so that when someone clicks the button, it takes them to a logout page or a page with a logout query param.
eg.
<?php
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
session_destroy();
echo 'You have successfully logged out!';
}
if (isset($_SESSION["pseudo"])) {
echo '<p><button type="button" id="deco">Disconnect</button></p>';
}
?>
<script>
let btn = document.getElementById('deco');
if (btn) {
btn.addEventListener('click', updateBtn);
function updateBtn() {
window.location.href = "/?action=logout"
}
}
</script>
I have been trying to use this code to create a modal popup that welcomes user in the WordPress dashboard when one logins with the following snippet at the functions.php or a plugin but it doesn't seem to load anything / even the thickbox.
Hope someone helps. Thanks.
add_action('admin_init', 'open_modal');
function open_modal(){
$id = get_current_user_id();
$user = get_userdata($id);
if(in_array('administrator', $user->roles)){
?>
<script>
function call_to_open_modal(){
//your code goes here...
}
call_to_open_modal();
</script>
<?php } }
Are you getting any errors in console? Try to echo your javascript inside the function :
add_action('admin_init', 'open_modal');
function open_modal(){
$id = get_current_user_id();
$user = get_userdata($id);
if(in_array('administrator', $user->roles)){
echo
"<script>
function call_to_open_modal(){
//your code goes here...
}
call_to_open_modal();
</script>";
} }
I have made a function.php and I included a redirect (to another page) after processing the function.
It works great - I click / function executes and it redirects.
BUT
I want it not to instant redirect and to delay a couple of seconds, since I have a message "Succesuful added.. "
Last part it includes the JS:
//Display ID
echo "<div align='center'> <h4>*Command completed! NR: ".$id_receptie. </h4></div>";
echo "<script type='text/javascript'>
window.onload = function () { top.location.href = 'fisa_service.php?id=" . $id_receptie . "'; };
</script>";
Try the setTimeout function.
setTimeout(function(){
// redirect
}, 2000);
The 2000 are the seconds in milliseconds.
Hope this helps.
You can use the sleep() function in PHP : http://php.net/manual/fr/function.sleep.php
Try next script:
<?php
$timeout = 2; // Timeout in the seconds
$id_receptie = 0;
?>
<div align='center'><h4>*Command completed! NR: <?= $id_receptie ?> </h4></div>
<script type='text/javascript'>
setTimeout(function() {
top.location.href = 'fisa_service.php?id=<?= $id_receptie ?>';
}, <?= $timeout ?>*1000);
</script>
<?php
exit;
I try to create a javascript to click on the link(id="bigPicLink") once the page load but it not working. New in javascript here. Please help. Thank you.
<?php
// display modal
if (isset($_GET[id]) && $_GET[id] != "" ) {
$result = mysqli_query($dbc, "SELECT * FROM tattoo WHERE `productno` = '$_GET[id]'");
$tattoo = mysqli_fetch_assoc($result);?>
adasdsada
<?php
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#bigPicLink').click();
document.getElementById('bigPicLink').click();
});
</script>";
} ?>
Translate for #mitogh answer :)
$( document ).ready(function() {
$('#bigPicLink').trigger('click')
});
Maybe when you was calling $('#bigPicLink').click() it wasn't create in DOM, so DOM cannot know #bigPicLink is what?
You can try
$(document).ready(function() {
setTimeout(function() {
$('#bigPicLink').click();
}, 1000);
});
or needn't $(document).ready just only setTimeuut
setTimeout(function() {
$('#bigPicLink').click();
}, 1000);
I am trying to reload a page and retrieve data automatically after 10 seconds or preferably less than that. Please i tried so many codes and it didn't work.
Here is what i am using now...
// script //
<script>
$(document).ready(function(){
// setInterval(function() {
// $("#load_msgs").load("load.php");
// }, 10000);
//var updateInterval = setInterval(function() {
//$('#load_msgs').load('load.php?p=<?php echo $username; ?>');
//},1000);
//});
var autoLoad = setInterval(
function ()
{
$('#load_msgs').load('load.php').fadeIn("slow");
}, 10000);
});
</script>
// php / html div id panel
<div class = 'container me' id = "load_msgs"> </div>
// php load.php file
<?php
include('config.php');
include('func.php');
//$username = $_GET['q'];
$o = "--DATABASE QUERY--";
$z = mysql_query($o) or die(mysql_error());
while ($roow = mysql_fetch_array($z)) {
$date = $roow['date'];
echo $roow['message']. " <span class = 'lil'>" . time_elapsed_string($date)."</span> \n <br />";
}
?>
I'm assuming from your comments that you can navigate directly to load.php and the data is echo'd. If that is the case, here's how I would set it up:
(It's not clear if you trying to load a file every X seconds, or just once after X seconds, Here is an example of both)
index.php:
<?php
$username='someUser'
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Load From PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var username = encodeURI( <?php echo isset($username) ? "'".$username."'" : ''; ?> );
$(function(){
var url='load.php?username='+username;
// load once after 2 seconds
setTimeout(function(){ $.get(url, function(response){ $('#load_msgs').html(response); } ); }, 2000);
// load every 2 seconds
setInterval(function(){ $.get(url, function(response){ $('#load_msgs_repeat').append(response+'<br>'); } ); }, 2000);
});
</script>
</head>
<body>
<h4>Div loaded once</h4>
<div id="load_msgs"></div>
<br>
<h4>Div loaded every 2 seconds</h4>
<div id="load_msgs_repeat"></div>
</body>
</html>
load.php:
<?php
$username= isset($_GET['username']) ? $_GET['username'] : null;
echo $username ? 'here is some data loaded from load.php for user: '.$username : 'here is some data loaded from load.php, but no user was provided' ;
?>
Here is a functioning example
The problem may be in config.php or func.php. Are you connecting to the database before running your query?
Add these two lines to the start of load.php:
ini_set("display_errors", "On");
error_reporting(E_ALL);
Then visit load.php in your browser. You should get some helpful error messages.
window.setTimeout(function(){window.location.replace("page.php")},5000);