You're doing an echo and then writing a relocate header. If you did your relocate in the javascript (after the user clicked the alert), it would probably work the way you expect it to.
echo "<script>alert('You are logged out'); window.location.href='..\Main.php';</script>";
Also, the way that you use isset will cause problems because isset returns true or false (it checks if a value is present), rather than returning the value.
So instead of
if(isset($_SESSION['Username']) == "admin")
You need to do:
if(isset($_SESSION['Username']) && $_SESSION['Username'] == "admin")
Answer from Steve K on Stack OverflowYou're doing an echo and then writing a relocate header. If you did your relocate in the javascript (after the user clicked the alert), it would probably work the way you expect it to.
echo "<script>alert('You are logged out'); window.location.href='..\Main.php';</script>";
Also, the way that you use isset will cause problems because isset returns true or false (it checks if a value is present), rather than returning the value.
So instead of
if(isset($_SESSION['Username']) == "admin")
You need to do:
if(isset($_SESSION['Username']) && $_SESSION['Username'] == "admin")
header("Location: ..\Main.php"); tells the browser to go to another page before it even shows the page... if you want the user to see the alert, try this:
session_destroy();
echo "<script>";
echo "alert('You are logged out');";
echo "window.location = '../Main.php';"; // redirect with javascript, after page loads
echo "</script>";
Videos
It's easy: you have Location header:
echo "<script>alert('Addition Successfull !!');</script>";
header("Location: banking.php");
When you send Location header to user, he/she will get redirected and script won't be interpreted!
P.S: As @Quentin said, mysql_ functions are deprecated, they're old and no longer maintained. Use mysqli or PDO.
P.S 2: You send content before header, so assume you're using output buffering.
You header away right after the echo, but that should give an error (you can not header() if output (like echo) has been put on the screen).
I asume you use something like ob_start()
I suggest the following method:
header("Location: banking.php?alert=Addition%20Successfull%20!!");
and check in balking.php for isset($_GET['alert']), if so, echo it, if not, dont do anything
It would be better if you use below mentioned code.
if(empty($response)){
//SOME ACTIONS
?>
<script type="text/javascript"> alert("Message´s 1st line\nMessage´s 2nd line");</script>
<?php
//SOME ACTIONS
header('Location: SOMEWEBSITE');
}else{
//ANOTHER ACTION
}
First thing is to fix quotes.
Second thing is remove header() call
http://php.net/manual/en/function.header.php
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.
And don't use location header for 200 response
https://en.wikipedia.org/wiki/HTTP_location
You are not using any response in your ajax request. you need to get response as:
success: function(response){
Don't know why are you using die() here, you can just use echo here as:
if($data_exists){
echo 'The person exist';
}else{
// else part
}
But, still how can you check either getting success or error in ajax response. you can use json here like:
if($data_exists){
echo json_encode(array('status'=>0,'message'=>'The person exist')); // for error
}else{
// else part
echo json_encode(array('status'=>1,'message'=>'Success message')); // for success
}
After this, you need to change your ajax as:
$.ajax({
type:'POST',
url:url,
data:$('#formulario').serialize(),
dataType: "json", // will return json response
success: function(response){
if(response.status){
// your success part
}
else{
alert(response.message); // error response in alert
}
}
});
Other Improvements:
You can use Prepared Statement for preventing your SQL Statements with SQL Injection.
I can't find any alert in ypur success function. So the browser obviously won't alert.
I will try an edit:
success: function(something){
if ($('#pro').val() == 'Registro'){
$('#formulario')[0].reset();
$('#modal-mensaje').addClass('bien').html('Registro completado con exito').show(200).delay(1600).hide(200);
$('#pro').val('Registro');
alert (something);
return false;
}else{
$('#modal-mensaje').addClass('bene').html('Edicion completada con exito').show(200).delay(1600).hide(200);
return false;
}
Your close button is attempting to fire a JavaScript function called killIt() with a parameter of 'error'. I'm going to guess that you haven't included that function on the page, or that there's an error in it.
Just add the killIt javascript function to HTML. It would not remove any PHP code at server-side, but rather HTML output of PHP code in the browser. You haven't shown us the code for killIt, but my guess is that it either hides or removes the DIV that PHP prints.
use this code
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
The problem was:
- you missed
" - It should be
alertnotalery
Try this:
Define a funciton:
<?php
function phpAlert($msg) {
echo '<script type="text/javascript">alert("' . $msg . '")</script>';
}
?>
Call it like this:
<?php phpAlert( "Hello world!\\n\\nPHP has got an Alert Box" ); ?>