Alert Box before PHP action = "newpage.php" gets executed - javascript

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

Related

Redirect to Previous Page After Message

I want to go back to previous page after pressing "OK" on the function message prompted.
Php Code
function function_alert($message) {
// Display the alert box
echo "<script>alert('$message');
document.location='javascript://history.go(-1)';
</script>";
}
// Function call
function_alert("This email has already subscribed");
}
Alert Box
Thank you so much!
Tried this
function function_alert($message) {
echo "<script>
alert('$message')
history.back()
</script>";
}

JavaScript not working in mPDF

I am using mPDF for PDF generation in PHP. It is working fine with no issue.
What I want if user is not logged in then I would like to show error in alert box and after that redirect to index.php.
But due to some reason that I don't know it is not showing any alert box nor redirect. It seems like JavaScript is not working.
Here is the code:
<?php
session_start();
$uid=$_SESSION['uid'];
if($uid== "" or $uid == NULL)
{
echo '<script type="text/javascript">window.alert("Please login first to access this page."); </script>';
echo '<script type="text/javascript">window.location.href("/index.php");</script>';
}
Above code is top of the file and now below I have these code for mPDF:
include("pdf/mpdf.php");
$mpdf=new mPDF('');
$stylesheet = file_get_contents('pdf/tablecss.css');
$mpdf->WriteHTML($stylesheet,1);
//==============================================================
//$mpdf->WriteHTML($html);
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetWatermarkText(' www.somewebsite.com ');
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->watermark_font = 'DejaVuSansCondensed';
$mpdf->showWatermarkText = true;
$mpdf->WriteHTML($html);
$html = '
<html>
<head>
<style>
....
I fixed that. What i did is i put mPdf code inside else.
Like this and it works.
if($uid== "" or $uid == NULL)
{
echo '<script type="text/javascript">window.alert("Please login first to access this page."); </script>';
echo '<script type="text/javascript">window.location.replace("/index.php");</script>';
}else{
mpdf code goes here

Echo after header

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>';
}

header by clicking button in JavaScript alert popup

Is there a way to direct to a desired page by clicking the OK button in a JavaScript pop-up?
I am able to achieve it when not within php but when echoed im not sure.
else {
echo '<script language="javascript">';
echo 'alert("Wrong Username or Password")';
echo '</script>';
header("Location:login.php");
}
This headers OK but i would like it only to happen when the user clicks OK in the popup. Any suggestions?
Thanks
Just use this:
else {
?>
<script language="javascript">
alert("Wrong Username or Password");
location.href = "login.php";
</script>
<?php
}
Just use javascript to do this:
else {
var popup = window.confirm("Wrong Username or Password");
if (popup==true)
{
x="You pressed OK!";
window.location.href = "yourdomain.com/login.php";
}
else
{
x="You pressed Cancel!";
}
}
else {
echo '<script language="javascript">';
echo 'alert("Wrong Username or Password")';
echo 'window.location = "/login.php";'
echo '</script>';
}
should work just fine. It won't redirect until OK is pressed.

Display an alert box in php and navigate back

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" );

Categories

Resources