i have 2 php pages page1.php and page2.php
page1.php redirects to page2.php with an id paramater
page2.php?id=8923423672222
page1.php code
<?
$id = "4556745767";
echo "<script language=\"javascript\">function gopage2(){top.window.location.href=\"http://www.example.com/page2.php?id=".$id."\";}setTimeout('gopage2()',1000);</script>";
?>
page2.php code
<?
$id = $_GET['id'];
if($id!='')
{
section = "section1";
}
else
{
section = "section2";
}
echo "<script language=\"javascript\">function gosite(){top.window.location.href=\"http://www.example.com/".$section."/\";}setTimeout('gosite()',1000);</script>";
?>
in some cases page1.php doesn't redirect to page2.php
the reason of using javascript here that i want to be sure the user has javascript enabled, but in my tracking systems its report that page1 not redirecting in about 40% of requests and this is not reasonable , most of request comes from real hits
any help if there is any syntax or semantic wrong here
Related
I want to get my application when a item is deleted pop up a messege and redirect to another page. I used javascipt for the popup and php header for the redirection. Now its only doing or the popup or the redirect depending which one is listed first. how do i fix this?
<?php
session_start();
require_once('../../includes/mysql_config.php');
$id = isset($_SESSION['id']) ? $_SESSION['id'] : header('location: ../../login.php');
$Cursist = mysqli_query($con, "SELECT id FROM users WHERE id =".$_SESSION['id']);
if(!$Cursist){
header('location: ../login.php');
}
$test = $_GET['id'];
$sql = "DELETE FROM cursus WHERE id = $test";
$result = mysqli_query($con, $sql);
if ($result) {
echo "<script type='text/javascript'>alert('Verwijdert!')</script>";
header("Location: ../cursussen.php?destoyed=true&destroyed_id=".$_GET['id']);
}else {
echo "mislukt";
}
?>
If you send sometrhing before header will not work. You can use only header before sending sometrhing to the client.
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
http://php.net/manual/en/function.header.php
You could do with javascript but It not recommended because the user could have javascript disabled:
echo "<script type='text/javascript'>";
echo "alert('Verwijdert!')";
echo "document.location.href='index.html'";
echo "</script>";
The best way is to use session and header, you can save a var in session and show a message when the var is true and when you show the messasge delete the session var
delete.php
$_SESSION['deleted'] = true;
header("Location: index.php);
index.php
<?php if($_SESSION['deleted']){ ?>
<?php unset($_SESSION['deleted']) ?>
<div>Item was deleted</div>
<?php } ?>
Well, the problem is that the redirect immediately moves you to a new page, so any javascript on the old page becomes irrelevant. You might be able to use a delay before redirect so that the javascript alert can display.
Otherwise, introduce a variable that you send to the redirect destination page, and use this variable to trigger a javascript popup there.
I want to redirect to view with script in codeigniter. Script first and then page redirection.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function login()
{
$this->load->view('login');
}
}
I tried this code but it's not working because page is redirected before script completes it's executions. So, How to show notification in alert.
echo "<script>";
echo "alert('User not Found');";
echo "</script>";
redirect('home/login');
I tried with this also,
echo "<script>";
echo "alert('User not Found');";
echo "</script>";
echo "<script>setTimeout(\"location.href = 'http://localhost/dealsnow/index.php/home/login';\",300);</script>";
Second way is working for me but i think it's not good way. So is there any other option available for redirection to view after script completes it's execution.
Example : If i insert Form Data and then I want to show user that data is inserted properly in script and then i want the page to redirect.
Script first and then page redirection.
Solve my question,
If we want to execute script first and then after some timeout.
Ex. : if we want to show user that data is inserted properly in script and then i want the page to redirect.
echo "<script>";
echo "alert('Data Insereted Properly..!');";
echo "</script>";
$url = base_url().'/index.php/home/login';
header("refresh:3;url=$url");
use flash data in your code.
// Set flash data
$this->session->set_flashdata('error', 'User not Found');
// After that you need to used redirect function instead of load view such as
redirect("home/login");
// Get Flash data on view
$this->session->flashdata('error');
Hope it will help.
I am trying to display an alert box before redirecting to another page, here is my code, when I remove the header function it works properly but when it is here it will just redirect to the page without showing the alert box.
<html>
<body>
<?php
include("dbconfig.php");
$tempid = mysqli_real_escape_string($dbconfig, $_POST['tempid']);
$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig, $sql_query);
if ($result) {
echo '<script language="javascript">';
echo 'alert("visit deleted successfully")';
echo '</script>';
header("location:../SearchCountry/search.php");
}
?>
</body>
</html>
PHP is executed at the server side. It renders HTML/JS/CSS and sends it to the web browser, the web browser then parses and executes the JavaScript (In your case, show the alert dialog.)
However, once you call
header ("location:../SearchCountry/search.php");
The browser will be informed to redirect the user to ../SearchCountry/search.php immediately, without a chance to parse and execute the JavaScript. That's why the dialog will not show up.
Solution: redirect your user to another page with JavaScript instead of PHP.
<html>
<?php
include("dbconfig.php");
$tempid = mysqli_real_escape_string($dbconfig,$_POST['tempid']);
$sql_query = "DELETE FROM Visits
WHERE visitid = '$tempid'";
$result = Mysqli_query($dbconfig,$sql_query);
if($result){
echo '<script language="javascript">';
echo 'alert("visit deleted successfully");\n';
echo 'window.location.href="../SearchCountry/search.php"'; //Redirects the user with JavaScript
echo '</script>';
die(); //Stops PHP from further execution
}
?>
</body>
</html>
echo "<script>
alert('visit deleted successfully');
window.location.href='SearchCountry/search.php';
</script>";
and get rid of redirect line below.
You were mixing up two different worlds.
So Basically this is what i want to do , I want to take data from my database and then put that data on a page after the user logs in , however i am havinng problems understanding a way , So here's some sample code for clarity :
//user has logged in:
//fetched data from database to php file
$name = $result['name'] //result being the object after the queried row is fetched
now i want to add this name in lets say a div in my HTML
<div id="name"><!--$name comes here--></div>
So what is the way to do this using JS ? is AJAX the answer?
I guess that you have $name in the login.php file and the div is in profile.php. You can use session variables or ajax:
By php:
login.php
<?php
session_start();
$_SESSION["name"] = $result['name']
?>
profile.php
<?php
session_start();
?>
<div id="name"><?php echo $_SESSION["name"]; ?></div>
By ajax:
getName.php
$name = $result['name'];
echo $name;
profile.js
$.get("getName.php", function(data, status){
$("#name").append(data);
});
I want to check if the user is not logged in then he cannot access to the patient-dashboard page and redirect to index.php thats working fine but when i add an alert of javascript in patient-dashboard page where i redirecting the user to index.php if not logged in then it will first show alert box then it will go to index page thats not working... kindly help..
patient-dashboard.php
if(!isset($_SESSION['username'])) {
echo "<script type='text/javascript'>";
echo "alert('Login First');";
echo "</script>";
header('Location: index.php');
}
if(!isset($_SESSION['username'])) {
echo '<script type="text/javascript">;alert("Login First");window.location = "index.php";</script>';
}
You must send headers before anything else. The script is preventing the header from working. Redirect to an informational page instead of using a script, and use meta refresh to take them back to the index.
The informational page is just an ordinary HTML page that says, "You have to log in." You get them back to the index page like this:
<meta http-equiv="refresh" content="5;URL=http://www.exmaple.com/index/.html">
The "5" is five seconds. You can probably just put index.html in the URL; I haven't tested that.
Try
// patient-dashboard.php
if (!isset($_SESSION['username'])) {
$_SESSION['login_failed'] = 1;
header('Location: ./index.php');
exit; // eliminate errors and prevent the user from going any further
}
// index.php
if (isset($_SESSION['login_failed'])) {
unset($_SESSION['login_failed'];
die("Login First");
}
Or
// patient-dashboard.php
if (!isset($_SESSION['username'])) {
die("<script type='text/javascript'>alert('Login First'); location.href = './index.php';</script>");
}
Try this, you don't need to have the different parts of the alert in different echos, and the header redirect is now before the alert.
**EDIT**This refresh way works as a redirect and gives the alert
<?php
$var1 = 1;
$var2 = 2;
if($var1 < $var2) {
header( "Refresh:1; url=index.php");
echo "<script type='text/javascript'>alert('Login First')</script>";
}else{
echo "<script type='text/javascript'>alert('Logged In')</script>";
}
?>