Show popup box in if conditions in php - 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');
}
});

Related

How to change alert function to bootstrap modal

I wanted to change the following function to bs modal or any other fancy dialogue box.
if (Form::mailer($email, $fname, $lname)) {
echo
'<script >
alert("Thank you for registration.");
window.location = "../test.php"; <
/script>';
} else {
echo ' <
script >
alert("Error, please try submitting again. Error code 1");
window.history.back(); <
/script>';
}
You can replace alert with BoostrapDialog:
Need to add these bootstrap CDN first
https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css
https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js
And then replace the alert method with show method of BootstrapDialog
BootstrapDialog.show({
message: 'Thank you for registration.'
});
For detail, please refer http://nakupanda.github.io/bootstrap3-dialog/

Open modal window after submitted and returned to page

I have function validate my form and after form submitted
else
{
$('.submit-button').val('Wait please...');
$('.submit-button').attr('disabled', 'disabled');
return true;
}
PHP redirect to main page
function wpcallback_get_target() {
global $wpcallback_plugin_option;
if($value = $callback_plugin_option['target'])
return get_permalink($value);
return get_site_url();
}
PHP get site url then how I can make thanks modal window opened?
I tried with .modal but can't find right place for it.
$('#myModal').modal('show');
place $('#myModal').modal('show'); in the bottom of you view file or you can add following code in head tag
$(document).ready(function(){
$('#myModal').modal('show');
});
to prevent model every time on page load, update your PHP file:
function wpcallback_get_target() {
global $wpcallback_plugin_option;
if($value = $callback_plugin_option['target'])
return get_permalink($value);
$url = get_site_url();
return $url."?response=true";
}
in view file:
<?php if(isset($_GET['response']) && $_GET['response'] == 'true'){ ?>
$(document).ready(function(){
$('#myModal').modal('show');
});
<?php } ?>
use Javascript code:
$(document).ready(function(){
var myURL = window.location;
var res = myURL.search("response=true");
if(res > 0){
$('#myModal').modal('show');
}
});
Try this on your php.
echo "<script>
$(window).load(function(){
$('#yourthanksmodel').modal('show');
});
</script>";

Using jQuery "click" function on an element echoed by 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

hide and show div when page loads based on result generated from php

I am doing API call to box.net.
I wanted to show data from the response into a table.
But sometimes response is empty.
that time i want to show user some message.
So my idea is to show div containing table when i get some response and disable div containing message. and vice versa.
I an new to jquery. Didn't find suitable examples.
I tried something from those examples,but it was not successful.
here is
if (sizeof($folder_entries) == 0)
{
echo '<script>';
echo '$("#message").show()';
echo '$("#content").hide()';
echo '</script>';
}
else
{
echo '<script>';
echo '$("#message").hide()';
echo '$("#content").show()';
echo '</script>';
}
How can i get that?
Thanks in Advance.
seems like you need straightforward javascript or jquery to show and hide the div
<script>
if (some condition..... )
{
document.getElementById("test").style.display = '';
}
else
{
document.getElementById("test").style.display = 'none';
}
</script>
<div name="test" id="test">
jQuery:
$(document).ready(function() {
$('#hideh1').click(function(){
$('div.showhide,h1').hide();
});
$('#showh1').click(function(){
$('div.showhide,h1').show();
});
$('#toggleh1').click(function(){
$('div.showhide,h1').toggle();
});
});

How to run jquery when click submit with php javascript widget

I using PHP/Javascript - Widget of alexmarandon from link http://alexmarandon.com/articles/web_widget_jquery/
And in project of me is:
script.js
html.php
demo.html
In script i using code:
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.1') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main();
}
/******** Our main function ********/
function main() {
jQuery(document).ready(function($) {
/******* Load CSS *******/
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "style.css"
});
css_link.appendTo('head');
/******* Load HTML *******/
var jsonp_url = "data.php?callback=?";
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html(data);
});
$('.submit').click(function(){
alert('test');
});
});
}
})(); // We call our anonymous function immediately
Then i call data in data.php
$str = '<div class="widget_advance">';
$str .= '<form method="post" id="form_widget_advance">';
$str .= '<input type="submit" value="Tìm" class="submit" />';
$str .= '</form>';
$str .= '</div>';
echo $_GET['callback'] . '(' . json_encode($str) . ');';
finally, in demo.html is result:
<script src="script.js" type="text/javascript"></script>
<div id="example-widget-container"></div>
=> When I click on button submit is result not show alert('test'); How to fit it ?
Put this inside as such:
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html(data);
$('.submit').click(function(){
alert('test');
});
});
You see, it has to be inside the callback. You can't bind to the submit button if it's not in the DOM yet. You could also do something along the lines of: $("#someForm").submit(function() { ... });

Categories