I want to display a javascript alert box in php and navigate back to the main page. Here is my code
session_start();
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">alert(" You are not allowed to view this contnent"); </script>';
header('Location: mainPage.php');
}
//the rest of my page
With this code the alert popup is not displayed only the redirection. I also try this
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">window.alert(" You are not allowed to view this contnent");</script>';
echo'<script type="text/javascript> window.navigate("mainPage.php"); </script>';
}
//the rest of my page
Now the popup is displayed but the redirection does not work.
$name= $_SESSION['name'];
$id= $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">window.alert(" You are not allowed to view this contnent");</script>';
echo'<script type="text/javascript> window.location.href="mainPage.php"; </script>';
}
please review this code
session_start();
$name = $_SESSION['name'];
$id = $_SESSION['id'];
//check if is a guest
if($name=="Guest"){
echo'<script type="text/javascript">
alert(" You are not allowed to view this contnent");
self.location="mainPage.php";
</script>';
}
Try this
try this.Just add after how many seconds the page need to redirect . so
change
header('Location: mainPage.php');
to
header( "refresh:5; url=mainPage.php" );
Related
I am new to php . and i am trying to pop up an alert box before the page action = "newpage.php" gets executed using the header function , but it isn't working and every time without the alert box being popped up, i get redirected to the next page on successful form submissions.
what should i do to pop an alert box on successful submission of data in my database before going to the next page
here's my code snippet that i have used to do so---->
if($conn->query($insertQuery)){
if($_POST) {
echo '<script> alert("Registered"); </script>';
}
else{
echo '<script> alert("Not"); </script>';
}
header("location:login.php");
if($conn->query($insertQuery)){
if($_POST) {
echo '<script> alert("Registered"); </script>';
}
else{
echo '<script> alert("Not"); </script>';
}
echo '<script> window.location.href = "login.php"; </script>';
Try This
I have a PHP login system where you can delete your account/profile if you'd like to. When the user clicks on the button I want them to be logged out, their account to be deleted and an alert message (JavaScript) saying that their account has been deleted. The problem is that the alert message doesn't appear when I include the logout.php file. If I remove that line of code the alert message appears, but the user will still be logged in. How can I fix this?
Here is my code.
profile.php:
<form class="form" action="delete.php" method="post">
<button type="submit" name="delete" value="delete" onclick="return confirm('Are you sure you want to delete your profile?')">Delete profile</button>
</form>
delete.php:
<?php
session_start();
if (isset($_POST["delete"])) {
require_once 'dbh.inc.php';
$deleteUser = "DELETE FROM users WHERE usersId='".$_SESSION["userid"]."'";
if ($conn->query($deleteUser) === TRUE) {
echo "<script>alert('The profile has been deleted.');</script>";
include 'logout.php';
} else {
echo '<script>alert("Something went wrong: ")</script>';
}
} else {
header("location: ../index.php");
exit();
}
logout.php:
<?php
session_start();
session_unset();
session_destroy();
header("location: ../index.php");
exit();
Add redirection along with the alert() in js rather than doing in php.
echo "<script>alert('The profile has been deleted.'); window.location.href='logout.php';</script>";
This will show alert then redirect to logout.php
try change the following code:
From:
<?php
session_start();
session_unset();
session_destroy();
header("location: ../index.php");
exit();
To:
<?php
session_start();
session_unset();
session_destroy();
echo "<script>alert('The profile has been deleted.');</script>";
header("location: ../index.php");
exit();
I currently have a ajax script to run a notiload.php file which runs every 5 seconds to check if theres a change in the database, if theres a change it then outputs "1 New" for a message. However as I will be introducing a notification sound in that file. It will chime every 5 secconds when the file is loaded. Of course this will piss people off. So I need the script to check if theres a change in the database. I have a time and date stamp of every message, with the message being viewed as 2 and not as 1. So if theres a change in the database I will need it to call that file or play a sound.
here is my ajax script inside the main page
<script type="text/javascript">
$(document).ready(function(){
refreshTable<?= $FriendName->id ?>();
});
function refreshTable<?= $FriendName->id ?>(){
$('#NotiLoad<?=$FriendName->id?>').load('elements/notiload.php?id=<?=$FriendName->id?>', function(){
setTimeout(refreshTable<?= $FriendName->id ?>, 5000);
});
}
</script>
<div id="NotiLoad<?= $FriendName->id ?>" style="display: inline-block;"></div>
And here is the php of notiload.php
<?php
session_start();
// redirect to index page if not superuser
$con = mysqli_connect('localhost','root','','');
if (!$con) {
die('Could not connect to Socialnetwk DB: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
?>
<?php
$uid = $_GET['id'];
$mid = $_SESSION['user']['id'];
?>
<?php $usrload = $con->query("SELECT * FROM message WHERE sender = $uid AND recipient = $mid AND seen = '2' OR sender = $mid AND recipient = $uid AND seen = '2'"); ?>
<?php while($FriendName = $usrload->fetch_object()): ?>
<h6 style="display:inline-block;font-size: 10px;color:#37BC9B">1 New</h6>
<?php endwhile;?>
This is a part of my code. The code is moving to the location, but the echo below is not working.
I am sure this header is affecting it somehow
else {
header('location: index.php');
echo '<script>
document.getElementById("nick").value = "invalid email";
document.getElementById("nick").className = "invalidemail";
</script>';
If you read something about header in php functions, you will came to know that nothing gets printed after header as you are giving instructions to redirect on index.php
However to achieve the same, you can use session variables.
So, on the page where you are redirecting to index.php:
$_SESSION['error']=true;
header('location: index.php');
On index.php, check if session variable is true:
if($_SESSION['error']== true)
{
echo '<script type="text/javascript">
document.getElementById("nick").value = "invalid email";
document.getElementById("nick").className = "invalidemail";
</script>';
}
What I have is php that updates the database field after a client signs a form. This works, but I want it to redirect to a new page after it is confirmed that the user signed it by clicking OK. It they click CANCEL it will leave them on the same page.
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/x-javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
My problem is the javascript function does not execute after the php updtates the database.
I appreciate any advice or comments you have about this.
The problem is that you are using separate <script> tags and calling the function before it is defined. These two together do not work well. Also, I'm pretty sure that <script type="text/x-javascript"> does not work anyway since it's outdated and that you want <script type="text/javascript">
You can do the following:
Move function up and fix x-javascript:
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
Fiddle: Fiddle
You could post the form via ajax and in your php you can return a response to the javascript. That was you could use a callback to fire the new window.
Heres a jquery example:
$.post('/myscript.php', {foo: 'bar'}).done(function (response) {
window.open(......);
});
I believe you are using same page for landing and updating. What you need is to update your echo to add an onload.
echo "<script> window.onload=confirmFunction(); </script>";
Hope this what you are looking for.
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/javascript">
window.onload = function(){
<?php
if(submitted){
echo "confirmFunction();";
}
?>
}
</script>
Try:
if (r==true) {
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
window.location = "PAGE THAT YOU WANT";
}