redirect displays alert box - javascript

a page directs users back to a page automatically
<meta http-equiv="refresh" content="1; URL=http://localhost/mywebsite/Untitled9.php">
when it reaches 'Untitled9.php' i want it to display an alert box saying 'Message sucessfully posted'
I only want it to display when it id directed back to the page and not when first load
e.g Home - Message page = No alert
Home - message page - submit - redirect to message page = alert 'post sucessfully posted'

Can't control the DOM of a page externally.
What you could do however is pass a variable in the query string to the page you're redirecting to like this
Untitled9.php?message=success
Then on your new page have
<?php
session_start();
if ($_GET['message'] == "success" && $_SESSION['revisit'] == "0")
{
$_SESSION['revisit'] = "1";
?>
<script type="text/javascript">window.alert('Message successfully posted.');</script>
<?php
}
?>
In your first page change your code to look like this
<?php
session_start();
$_SESSION['revisit'] = "0";
?>
<meta http-equiv="refresh" content="1; URL=http://localhost/mywebsite/Untitled9.php">

If I understand your question correctly, this should work for you (assuming you alter the keys for the $_POST variables accordingly, of course):
<?php (isset($_POST['my_sub_btn'])): ?>
<script type="text/javascript">window.alert('Message successfully posted.');</script>
<?php endif ?>
Assuming you're submitting the message from a form, there should be no reason to redirect using a HTML redirect. Set the action attribute of your form to Untitled9.php and it will handle the rest...

Given your requirements, why not set a parameter on succes? Such as:
<meta http-equiv="refresh" content="1; URL=http://localhost/mywebsite/Untitled9.php?success=1">

Related

Uploading a file from window.onbeforeunload()

In my application I need to make sure that if a user moves away from the application (by closing the tab, closing the browser, or navigation to another page) that if they try to come back to the application (using the back button, history, or typing the link) they are directed to a screen that makes them log back in.
I think I have this all worked out:
On every page, check to see if my session is available.
If it is, move on. If not, redirect to "You must log in again." page.
On the window.onbeforeunload() event, run a destroy.php script.
The destroy script kills the session.
But I can NOT get my destroy script loaded from the onbeforeunload() event.
Here is the index file, that starts things off:
<?php
/***********************
INDEX for CloseTab test
************************/
// Start the session
echo "<pre>";
echo "Starting session now...";
session_start();
// Put something in the variable that we can get on another page
$_SESSION["name"] = "Joe";
echo "\nJust set session var 'name' to 'Joe'.";
?>
<!-- Open that other page -->
<a href= <?php echo "home.php"; ?> > Click here to open Home page</a>
Here is the HOME page, just to have somewhere to go and test for the session:
<head>
<!-- ***********************
HOME for CloseTab test
************************** -->
<!-- Link to jQuery file -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<!-- Link to our custom js code.. -->
<script type="text/javascript" src=<?php echo "my_scripts.js></script>
</head>
<?php
// Start the session
session_start();
// See if there is any indication that a session is not available.
// Test with closing tab, closing browser, navigating away from page.
// We want to see this message if the user is coming here from anywhere except the index page.
// Test back button, typing the URL, browser history.
if ( (!isset($_SESSION)) || (session_status() == PHP_SESSION_NONE) || (count($_SESSION) < 1) ) {
echo "<pre>";
echo "Your session is not longer active. Log in again.";
echo "\n\nPretending to go to a login page...";
exit;
}
?>
<!-- If the session is good, we MUST have come from index page, and can access the session var. -->
<div class="container">
<h1>Home Page</h1>
<h2>Here is the name from the session var: <?php echo $_SESSION["name"]; ?> </h2>
</div>
<div>
<h3><a href= <?php echo "destroy.php"; ?> > Click here to log out</a></h3>
</div>
</html>
Here is my javascript code:
/***********************
my_scripts.js
************************/
console.log("in js file");
// Before a page unloads, run the destroy code
window.onbeforeunload = function() {
console.log("inside onbeforeunload"); // I get this to print
// Try to execute with AJAX call // Nothing in the middle seems to be running
$.ajax({
type: "GET",
url: "destroy.php",
async: false
});
// Try with jQuery syntax
$("container").load("destroy.php");
console.log("Got this far"); // I get this to print
}
Here is the destroy code I am trying to load:
<?php
/******************************************************************
Destroy
- Clears the session var and destroys the session if user leaves app.
******************************************************************/
// Unset all of the session variables and destroy the session.
session_start();
$_SESSION = array();
session_destroy();
?>
<script type="text/javascript">
// Place message in the console that we can see even if the window closes.
console.log("DONE! Session destroyed.")
</script>
You need to be careful with onbeforeunload. Browsers don't let you do too much in this event.
If you want to make an AJAX call, you need to add async: false. That's something that's usually discouraged, but here, if the call is asynchronous, then the browser will probably finish the event and close the page before the call is done.
$.ajax({
type: "GET",
url: "destroy.php",
async: false
});

Jquery Automatically Click on hyperlink

Hii Everyone,
Here is my code for facebook Signin.i got this example from http://www.codexworld.com/login-with-facebook-using-php/. here is my index page code
PHP code
<?php
include_once("config.php");
include_once("includes/functions.php");
//destroy facebook session if user clicks reset
if(!$fbuser){
$fbuser = null;
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
$output = '<a class="myLink" href="'.$loginUrl.'">Click Here To Proceed</a>';
}else{
$user_profile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,birthday,picture');
$user = new Users();
$user_data = $user->checkUser('facebook',$user_profile['id'],$user_profile['first_name'],$user_profile['last_name'],$user_profile['email'],$user_profile['gender'],$user_profile['birthday'],$user_profile['picture']['data']['url']);
if(!empty($user_data)){
$output = 'Thanks For Register With Spark.You Should Receive Confirmation Mail Shortly';
}else{
$output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spark Login with Facebook</title>
<style type="text/css">
h1{font-family:Arial, Helvetica, sans-serif;color:#999999;}
</style>
</head>
<body>
<div>
<?php echo $output; ?>
</div>
</body>
</html>
Here i use a href to show facebook login.after te click of href it will redirecting to facebook login instead automatically want to click and it will redirect the page.Is there any possible way to do that.If anyone know the solution for the problem please help me!!
Why not do this with header location? Here´s how that works: PHP header(Location: ...): Force URL change in address bar
For example:
$loginUrl = $facebook->getLoginUrl(array('redirect_uri' => $homeurl, 'scope' => $fbPermissions));
header("Location: " . $loginUrl);
Official docs: http://php.net/manual/function.header.php
No need for using an $output variable, or for creating a login page (if you want to auto-redirect anyway). Just redirect the user if he is not logged in yet. No need to do something shady like "auto-clicking the login button". It´s not a solution, it´s a bad workaround. Although, it would be a lot better if you would let the user click it. Redirecting to the login page without a proper intro page, where he can see what the App is about, is not a good idea.
Even better: Use the JavaScript SDK for login: http://www.devils-heaven.com/facebook-javascript-sdk-login/
If you want to change the webpage's URL, you do not need to use jQuery to click the link. Instead, there is a simpler way.
JS:
window.location = "http://www.example.com/";
By changing the window.location, you can "redirect" to a new URL.
If you want to redirect only after an event, you can put that in a function of yours.
Example:
window.location = "http://www.example.com"

window.location seems to cancel ajax post request

I am trying to send data to php with ajax and at the same time redirect to that php file but when I redirected to the php file, the ajax didn't seem to send the data. I wanted to achieve something wherein a button is clicked and a data is sent to php using ajax and at the same time redirect to that php file to see the data sent by displaying it. The reason I didn't use something like window.location="ajaxtest.php?data=data" is because I'm gonna be using it in a google map api wherein if I click a button of a place, then I will redirect to the maps page and display the marker of the specific place depending on the id the ajax sent to the php file and the coordinates generated based on that id.
ajaxtest.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3
/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function(){
var data = "test";
$.post("ajaxtest.php",{
data: data
},function(data, status){
window.location="ajaxtest.php";
});
});
});
</script>
<button id="button">test</button>
</body>
</html>
ajaxtest.php
<?php
if(isset($_POST['data'])){
echo $_POST['data'];
}
?>
So, that's not how HTTP, AJAX, or PHP work. There isn't any POST data when you do your redirect, because the redirect is a separate request from the AJAX post. What you want is to do an AJAX post and somehow consume the response on your page, without doing a redirect at all.
It doesn't seem like ajax is the way to go here. If you want to redirect to a different page just do window.location.href = 'ajaxtest.php?id='+locationId; and in your ajaxtest.php:
<?php
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;
if ( ! $id) {
die('ID missing');
}
// Show marker
?>

Retain in the same page and do JS operations

My file structure
front-page
homepage.html
lib
.js
.php
I send the user information to server and doing server side validation. If server side validation fails, i want to load my html page and open log in window if email already exists. I have done the following
if(isset($_POST['submit']) && $_POST['email']!='')
{
$email= $_POST['email'];
$sql1 = "SELECT * FROM Users WHERE emailID = '$email'";
$result1 = mysqli_query($connection,$sql1) or die("Oops");
if (mysqli_num_rows($result1) > 0)
{
//echo "This Email is already used.";
//Write code to go back to register window
//header('Location: ../homepage.html');
echo '<script type="text/javascript">
console.log("cdsafcds");
//jQuery.noConflict();
$(".login_links_login").trigger("click");
</script>';
}
}
I don't see any errors and it isnot triggering the click too. I don't have any restrications. Please provide me the best way to do these kinda stuff. I checked with few sites they loads the separate page if php call fails. But i want to know why can't we do it in the same page if so.
HTML:
<!----head>
<script type="text/javascript" src="lib/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="lib/jquery-validate.min.js"></script>
<script type="text/javascript" src="lib/homepage.js"> </script>
<link rel="stylesheet" type="text/css" href="lib/homepage.css" />
</head!--------->
<form method="POST" action="lib/login_validate.php" class="loginDiv right" id="loginForm">
content goes here
</form>
Submit click: is handled by jquery
Please let me now if any data further needed

Php/Javascript cross connection issue

So i have a contact page that runs some php and in that php if the email sends through correctly it runs a javascript command. I have two javascript commands one to change page and one to alert the person sending the email. Here is the code:
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
window.location.assign("http://dtc.bz/twitch/index.html");
$('.alertfeed').show();
</script>
<?php
}else { ?>
<script language="javascript" type="text/javascript">
</script>
<?php
}
?>
When I run it. It redirects me to the correct page but the second command within the php page. So how do I get the second line of the javascript run on the page I want it to redirect to?
After the page redirection no code will be executed, that's the reason "$('.alertfeed').show();" is not getting triggered. To do this you have to redirect the page first and on the page you have redirected then execute the next command.
Something like
window.location.assign("http://dtc.bz/twitch/index.html?redirected=yes");
now on the index.html check if redirected param and execute this command in php
<?php if($_GET['redirected']=='yes')
{
echo "<script>$('.alertfeed').show();</script>";
}?>
Update: Using Jquery
$(document).ready(function(){
var url = window.location.pathname;
var pieces = url.split("?");
if(pieces[1]=='redirected=yes'){
$('.alertfeed').show();
}
});
Working Demo:
http://jsfiddle.net/sLEgJ/4/
Your are getting redirected because there is no condition given for redirecting.
Thats why its redirecting you every time you run it.

Categories