javascript alert and then a location replace not working - javascript

I trying to use an alert message when someone enters incorrect login details, and I then want to redirect them back to the same login page. I'm doing this inside a php file that is called when someone submits a login form. I'm trying something like this:
echo '<script language="javascript">';
echo 'alert("Invalid Username or Password")';
echo 'location.replace("target.php")';
echo '</script>';
but it goes to the php file itself, which shows nothing.

update it you have missing ; for alert
echo '<script language="javascript">';
echo 'alert("Invalid Username or Password");';
echo 'window.location("target.php")';
echo '</script>';

echo '<script>';
echo 'alert("Invalid Username or Password"); ';
echo 'location.replace("http://mywebsite.com/target.php"); ';
echo '</script>';
language attribute is deprecated
location.replace("target.php") must have valid url: location.replace("http://mywebsite.com/target.php") or you can use history.back()
and as noted, don't miss semicolon

Related

echo alert() in php show everything in the string. octobercms plugin

I'm having issue when echoing alert() in php. It show everything in the string.
echo "<script type='text/javascript'>alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');</script>";
Output
You can use javascript code between ending and starting php tags like below,
?>
<script type="text/javascript">
alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');
</script>
<?php
// rest of the php code will go here.
?>
OR try this please,
<?php
echo '<script type="text/javascript">';
echo 'alert("Thank you for your enquiry, we will contact you as soon as possible, have a nice day!");';
echo '</script>';
?>
Solution: I fixed it by removing everything. and just echo the message directly without the alert(). I'm not sure why it behave like this because this is my custom plugin. I will update if I found the source of this behavior. Thank you for all the replies.
echo 'Email Successfully Sent. We will reply soon!';

PHP header redirect not working if the session isnt set

I'm trying to check if the user hasnt logged in on a cart.php, if they arent -redirect them to login.php.
This isn't working so I tried putting it in an elseif statement. If they are logged in and are on the cart page, redirect them to the cart page, and if they arent redirect them to the login page.
It is working in the sense that it redirects, yet if I login, it still redirects to login.php.
Also I'm not sure why none of the JS alerts are working.
if (isset($_SESSION['username'])){
header("Location: http://localhost/techiteasy/cart.php");
}
else if (!isset($_SESSION['username'])){
header("Location: http://localhost/techiteasy/login.php");
echo '<script language="javascript">';
echo 'alert("Please Login/Register")';
echo '</script>';
}
After a redirect you can't add more code, the new page will call and the script of the new one will run.
You need somthing transfer from a page to another.
Usually i use a cookie with a short time (2-5 seconds), the new page will call check if the cookie is set, if it is show the alert and unset the cookie.
Here in the code you can see i put only an if statement (without else or else if) cause when the script fire the header location the next one code won't run and the new page will load.
if (!isset($_SESSION['username'])){
setcookie('notlogged', true, time() + 3;)
header("Location: http://localhost/techiteasy/login.php");
}
header("Location: http://localhost/techiteasy/cart.php");
In the login.php
if (isset($_COOKIE['notlogged']) && $_COOKIE['notlogged'] == true){
echo '<script language="javascript">';
echo 'alert("Please Login/Register")';
echo '</script>';
unset($_COOKIE['notlogged']);
}
Try this ;)
You can also not set an expired time, it is just to avoid errors, but remind to unset every time, in other case the user only logs without passing from cart will wiew the alert.
You can simply use as like below. Do not need to check negative condition again. Simply use if-else.
if (isset($_SESSION['username'])){
header("Location: http://localhost/techiteasy/cart.php");
}
else{
header("Location: http://localhost/techiteasy/login.php");
echo '<script language="javascript">';
echo 'alert("Please Login/Register")';
echo '</script>';
}
Try this on PHP5
if(isset($_SESSION['username'])){ header("Location: http://localhost/techiteasy/cart.php");
}
else{
if(!isset($_SESSION['username'])){
echo '<script>alert("Login with vaild user and pass");</script>';
header("Location: http://localhost/techiteasy/login.php");
exit;
}}
For PHP7
if(isset($_SESSION['username'])){ header("Location: http://localhost/techiteasy/cart.php, true, 301");
}
else{
if(!isset($_SESSION['username'])){
echo '<script>alert("Login with vaild user and pass");</script>';
header("Location: http://localhost/techiteasy/login.php, true, 301");
exit;
}}

Show a timed message between submit and reload

I have a "Bootstrap" form. When the user press "Submit" data is sent to file.php If the database is updated, I have an alert in that php file which echos an alert with "success". Now I'd like to alert a timed message. Not the alertbox with "OK" button. Is there some small simple code for this?
This is what I have now!
PHP
//Echo succes
echo "<script type='text/javascript'>";
echo "alert('Välkommen ".$row['usr_fname']." ".$row['usr_lname']."');";
echo 'window.location = "../back_to_form.html"';
echo "</script>";
die();
You could potentially use something like Jquery UI to fake a dialog box. Then the reloading could happen just like you want it to. The code would look something like this (untested, you'd need to adapt it to your app):
echo "<html><body>";
echo '<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>';
echo "<div id="dialog" title="Basic dialog">";
echo "<p>Välkommen ".$row['usr_fname']." ".$row['usr_lname']."</p>";
echo "</div>";
echo "<script type='text/javascript'>window.settimeout(window.reload(), 5000)</script>";
echo "</body></html>";
More info with a better javascript example for the jquery ui message box can be found here

Javascript in php not working

I'm trying to make the page go back when the user presses the OK button on the alert box. The alert box appears but it doesn't go back to the previous page
Would i have to use a confirm window because i don't want to provide the cancel option, i just want a window to appear so the user has to click OK and it goes back to the previous page
print '<script type="text/javascript">';
print 'alert("Invalid please try again.")';
print 'history.back();';
print '</script>';
Just to provide a little more information I'm doing this after OR
$link =mysqli_connect(*sensitive info*) OR print '<script type="text/javascript">';
print 'alert("Invalid please try again.")';
print 'history.back();';
print '</script>';
exit();
Try this, You have missed to add ;
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
.........^
print 'history.back();';
print '</script>';
I think you're missing a semicolon ; after alert().
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
print 'history.back();';
print '</script>';
You have missed the ';' after your alert
print '<script type="text/javascript">';
print 'alert("Invalid please try again.");';
print 'history.back();';
print '</script>';
To find our errors in code you can use websites such as http://www.jslint.com/ to catch these errors easily.
echo "<script type=\"text/javascript\">";
echo "alert(\"Invalid please try again.\")";
echo "window.location= \"/*Write ur page Link Here*/\"";
echo "</script>";
Hope above code will work for you. and yeah ";" is missing after your alert line.

Embedding multiline alert box in php code

I would like to embed a JavaScript alert box into php code and would like the text to report variable values. The following code gives multiline but does not enable me to use variables.
?>
<script type="text/javascript">
alert("Total size of all files too large.\nConsider uploading files in smaller sets.\nYou can append to existing sets.");
document.location.href="SomewhereElse.php";
</script>
<?php
The following code lets me use variables but does not give multiline.
$Str="Total size cannot be not more than " . ini_get('post_max_size');
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
When I try this code, no alert box comes up.
$Str="Unable to load (all of) these files because of their (total) size" .
"\nPlease make the upload set conform to the following parameters";
echo "<script type='text/javascript'>\n";
echo "alert('" . $Str . "');";
echo "</script>";
I do get an alert box if I leave the \n (before the Please) out but it is not multiline. I am wondering if the \n dismisses the alert box. At any rate, how do I get a multiline alert box with variables?
try something like this:
<?php
$max_size = ini_get('post_max_size');
echo '<script>';
echo 'alert("Total size cannot be more than ' . $max_size . '!\nPlease make sure...");';
echo '</script>';
?>

Categories