I got a little problem. When I got my PHP script without header it\'s fine, I am getting javascript alert box. But when I use header before alert it\'s not working. It\'s re
Maybe try redirect using JavaScript.
if ( $pkt < 1 OR $user_id == 0) {
$message = 'This is a message.';
echo "<SCRIPT> //not showing me this
alert('$message')
window.location.replace('url of the page');
</SCRIPT>";
mysql_close();
}
Aaroniker is correct with the redirect comment. One way to make it work would be to send the message with a session variable and then add the message creation to the next page like so.
session_start();
if ( $pkt < 1 OR $user_id == 0) {
$_SESSION['message']= "this is a message";
header("Location: http://dunno.com/file.php");
On file.php:
session_start();
echo "<SCRIPT> //not showing me this
alert($_SESSION['message']);
</SCRIPT>";
mysql_close();
}
Another option would be to pass them with you header. Like so:
if ( $pkt < 1 OR $user_id == 0) {
$message= "this is a message";
header("Location: http://dunno.com/file.php?message=" . $message . ");
Then on file.php:
echo "<SCRIPT> //not showing me this
alert($_GET['message']);
</SCRIPT>";
mysql_close();
}
this is a redirect:
header("Location: http://dunno.com/file.php");
the code below wont work
Use $_SERVER 'php_self'. It will allow you to create script on same page where you want alert. In this way you won't have to add header.
Here's an example:
<form name="login" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" accept-charset="utf-8" class="form_class">
<ul>
<li>
<label for="usermail">Username</label>
<input type="text" name="username" placeholder="Enter username" required>
</li>
<li>
<label for="password">Password </label>
<input type="password" name="password" placeholder="Enter your password" required>
</li>
<li id="custom">
<input type="submit" value="Sign In">
</li>
</ul>
</form>
$sqluname="root";
$sqlpword="hrhk";
$database="registration";
$server="127.0.0.1";
$db_handle=mysql_connect($server,$sqluname,$sqlpword,$database);
$db_found = mysql_select_db($database, $db_handle);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$uname = $_POST["username"];
$pword = $_POST["password"];
$ulength=strlen($uname);
$plength=strlen($pword);
if($ulength > 5){
$sqlquery="SELECT * FROM members WHERE username='$uname' AND password='$pword'";
$result=mysql_query($sqlquery);
$num_rows=mysql_num_rows($result);
if ($num_rows >0) {
echo "<script>alert('Logged In');</script>";
session_start();
$_SESSION['sid']=$uname;
header("location:Yahoo/yahoo.php");
}
else{
echo "<script>alert('Wrong Username or Password!');</script>";
}
}
else{
echo"<script>alert('Username should be greater than 5 characters.');</script>";
}
}
?>
The below header function will run a 302 redirect by default and redirect the client to http://dunno.com/file.php so any code after the code begin processed after the header function will not be seen as the client is redirected from the page this code is on.
header("Location: http://dunno.com/file.php");
Please read up further on the header function here: http://www.php.net/manual/en/function.header.php
You could make you program sleep/wait for a few seconds(lets say 10 seconds for this example) before redirecting if you need the message to display like so:
if ( $pkt < 1 OR $user_id == 0)
{
$message = 'This is a message.';
echo "<SCRIPT> //not showing me this
alert('".$message."');
</SCRIPT>";
sleep(10);
mysql_close();
header("Location: http://dunno.com/file.php");
}
Your error is in the structure of your code. PHP, even in OOP is procedural and will run each line one after the other. So the way you've structured your code means the page is redirected before your echo. A better method is to use the javascript alert box to redirect your page using:
var prompt=window.confirm("message here. \r\n"+"Do you want to redirect?");
if(prompt)document.location.href='http://dunno.com/file.php');