Using jQuery "click" function on an element echoed by php - php

I'm making a forum and I want the user panel to change whenever a user is logged in. So by default it says "Sign in or register." But when a user is logged in it says their name and an option to log out.
The idea is that when they click sign out jQuery will ask for a confirm and if its true they will be redirected to ?logout=true where php handles the rest.
The problem is that jQuery just won't work on the element echoed by php.
Here is the PHP:
<div id="userbar">
<span id="userPanel">User Panel</span>
<?php
if (isset($_SESSION['signedIn'])) {
echo '
Sign Out
<span id="welcome">
Hello, <b><a href="#" class="sessName">' . $_SESSION["username"] . '
</b></a></span>
';
} else {
echo '
Sign In
Register
';
}
?>
</div>
And here is the jQuery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(".logout").click(function(){
var exit = confirm("Are you sure you would like to sing out?");
if (exit == true) {
window.location("header.php?logout=true");
}
return false;
});
</script>

First, you need to update your jquery is too old.
this the final function i create, you can try it.
function alertLogOut(){
var exit = confirm("Are you sure you would like to sing out?");
if (exit == true) {
window.location.href="header.php?logout=true";
}
return false;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
Sign Out

You may add your listener on the top of your html. Try to make it this way :
$(document).ready(function() {
$(".logout").click(function(){
var exit = confirm("Are you sure you would like to sing out?");
if (exit == true) {
window.location("header.php?logout=true");
}
return false;
});
});
This way, jQuery waits until your DOM is completely loaded.

As the element is not already created in the DOM, you need to use a delegated event for it to work.
See Understanding delegated events
So you would do like that, but you can attach to another container if you wish :
$(document).on('click', '.logout',
function() {
// Do your things
})
EDIT : I misleaded, everything is already in the DOM with PHP echo

Related

Show popup box in if conditions in php

I am trying to show a popup box in if conditions.
Here is my HTML :
<div id="my-confirm-dialog" class="dialog-overlay">
<div class="dialog-card">
<div class="dialog-question-sign"><i class="fa fa-question"></i></div>
<div class="dialog-info">
<h5>Thank You !</h5>
<p>Your Messag has been sent.</p>
<button class="dialog-confirm-button">OK</button>
</div>
</div>
</div>
Here is my Script :
<script>
$(document).ready(function() {
function showDialog(id){
var dialog = $('#' + id),
card = dialog.find('.dialog-card');
dialog.fadeIn();
card.css({
'margin-top' : -card.outerHeight()/2
});
}
function hideAllDialogs(){
$('.dialog-overlay').fadeOut();
}
$('.dialog-confirm-button, .dialog-reject-button').on('click', function () {
hideAllDialogs();
});
$('.dialog-overlay').on('click', function (e) {
if(e.target == this){
hideAllDialogs();
}
});
$(document).keyup(function(e) {
if (e.keyCode == 27) {
hideAllDialogs();
}
});
$('.dialog-show-button').on('click', function () {
var toShow = $(this).data('show-dialog');
showDialog(toShow);
});
});
</script>
Here is my button :
<span class="dialog-show-button" data-show-dialog="my-confirm-dialog">Dialog Button</span>
Whenever, I click on the button, a popup box is arrived. till now everyythin is fine. Now what i am trying to do is, when I sent mail and the condition is true, This popup box will appear.
Here is the condition and in this alert box is working perfectly.
if ($mail == true){
echo '<script type="text/javascript">alert("Thankyou for Contacting Shagun Sweets")</script>';
}
I just want to show the popup box in the place of alert box, I have already tried something like this but not helpful.
<?php
if ($mail == true){
echo "<script>";
echo "showDialog('my-confirm-dialog');";
echo "</script>";
}
?>
Please help me out.
Define the showDialog function outside the DOM ready function.
And, do this instead:
$script = '<script type="text/javascript">';
$script .= 'showDialog('my-confirm-dialog');';
$script .= '</script>';
echo $script;
If your alert is working, this should then also work
Your php script should have worked. But it is possible that it isn't working because you are actually checking if mail is true before the page loads. here is what I suggest you do, edit your php function to:
<?php if($mail == true){
echo "<script>var show_dialog = true;</script>";
} ?>
in your script:
$(document).ready(function() {
// put me in the end
if(show_dialog != undefined && show_dialog){
showDialog('my-confirm-dialog');
}
});

Fancybox 3 on page load with delay and set cookie not working

I can not get this jQuery to work on page load? I use fancybox 3.
<script>
function openFancybox() {
setTimeout( function() {
$('[data-fancybox data-src="#newsletterFancy"]').trigger('click');
}, 20000);
}
$(document).ready(function() {
var visited = $.cookie('visited');
if (visited == 'yes') {
return false;
} else {
openFancybox();
}
$.cookie('visited', 'yes', { expires: 7 });
$('[data-fancybox data-src="#newsletterFancy"]').fancybox();
});
</script>
I have also added this to my body tag: <body OnLoad="openFancybox()" class="body">
I basically have my pop up in a included file called newsletter.php. The link in my sidebar works fine when i click that. But i want to make it pop up and open on page load with a delay and also for it to set a cookie.
I have also included the cookie js as well:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
This is the line I use in my sidebar link for it to open when you click it:
<a class="buttons" data-fancybox data-src="#newsletterFancy" href="javascript:;">Newsletter Subscribe</a>
Thanks
You are simply not using valid selector. Replace
$('[data-fancybox data-src="#newsletterFancy"]')
with, for example:
$('[data-src="#newsletterFancy"]')

Clicking on a dynamically created link not recognizing click event

When you click on a folder a button/link is prepended to the nav section but when i click on the added button it will not work. It does not recognize my click event for some reason..?
quick video of what i am dealing with. https://www.youtube.com/watch?v=NsW1uLbRd9w
I am clueless on why it is not working ahhhh
Any help would be awesome, thanks.
If you need more code i can send it if needed.
<script>
$(function () {
// When folder is clicked open it
$('button.<?php echo $row->folderName; ?>').bind('click', function() {
$('.open.<?php echo $row->folderName;?>').show();
// if folder is open then Prepend homebtn button to nav
var k = $('.open').css('display');
if(k == 'block') {
$('.nav').prepend('<a class="gohome">HELLO</a>');
}
});
$('.gohome').live('click', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});
});
</script>
To bind to future elements you need to bind the event to a parent that exists at the time that the procedure is executed. As your link as being added to .nav, that's what we can use. I am assuming .nav. exists when this code is run. If not, use $(document).on('click'...
$('.nav').on('click', '.gohome', function() {
$('.open.<?php echo $row->folderName; ?>').hide();
});

display a javascript message before proceeding

I have this code that is to fetch a particular item in the database after the user presses a button. If the item is found I would like to display a confirm message via javascript and I don't know how to show it.
After obtaining the item from the database
if(null!=mysqli_fetch_array($res)))
{
echo ""; // how can I call the javascript code here ?
}
and the following confirm box
<script type="text/javascript">
function onConfirm()
{
return confirm("Display this item ?");
}
</script>
Use jquery and ajax to get the value from the database:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
$.get("ajax.php", function(data){
if(data != "0"){
if(confirm("Display this item ?"))
{
//do something
}
}
});
ajax.php:
//other db code here
if(null!=mysqli_fetch_array($res)))
{
echo "1";
}
else{
echo "0";
}
There are number of ways you can solve this. A good way is to:
Send an ajax request to a page
The page does the mysql stuff and returns(echo in case of PHP) the message
On the callback function use simple way such as alert() to show the message.

Auto click event, simulating a users click

if ($amLanguage['id'] == AM_DEFAULT_LANGUAGE_ID){
?>
<input id="default_language" type="image" src="<?php echo DIR_WS_CATALOG_LANGUAGES . $amLanguage['directory'] . '/images/' . $amLanguage['image'];?>" title="<?php echo AM_AJAX_CHANGES;?>"
<?php echo 'onclick="return amSetInterfaceLanguage(\''.AM_DEFAULT_LANGUAGE_ID.'\');"';?> >
<?php
}else{....
And I have:
$(document).ready(function() {
function language_def() {
$('type#default_language').trigger('click');
}
});
The purpose is to trigger the click event, simulating a users click, for the id="default_language"
I have been trying for days, but nothin works...:(
Somebody plz
Sara
You put the trigger code in a function. Either call it or Remove the function.
$(document).ready(function(){
$('#myObject').click()
});
Also, everything before the # id selector is irrelevant and just wastes bits.
This won't work?
$(document).ready(function() {
function language_def() {
$('type#default_language').click();
}
});
Actually, why do you need the function language_def? Simply doing this may work:
$(document).ready(function() {
$('type#default_language').click();
});

Categories