Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Eliminate PHP Page display 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
When a user enters a correct or incorrect password, a message correct or incorrect flashes up before it goes to URL's. I have tried removing echo, tried echo off etc but anything I take out does not work or cripples it going to the URL.
Thanks

<html>
<head>
<title>Form Results</title>
</head>
<body>
<?php

// This page receives and handles the data generated by "form.html".

//$array['name'] = $_POST['array']['username'] . '' . $_POST['array']['password'];

$array['name'] = $_POST['array']['username'] ;
$array2['name2'] = $_POST['array']['password'] ;
$array3['name3'] = $_POST['array']['email'] ;

//print ('User name is '. $array['name'] . "<br>\n");
//print ('Password is '. $array2['name2'] . "<br>\n");
//print ('Email is '. $array3['name3'] . "<br>\n");


$db = mysql_connect("localhost", "database", "password");

mysql_select_db("table",$db) or die("Select DB Error: ".mysql_error());


$sql = "select * from Validnames where Names = '" . $array['name'] . "'";


if ( $qry =mysql_query($sql) ) {
$numrows = mysql_num_rows($qry);
if ( $numrows > 0 ) {
printf("<tr><td>Valid Name %s</td>",$username);
$url = "Main.htm";
echo "<script>location.href='$url'</script>";


}
else {
printf("<tr><td>Not a Valid Name %s</td>",$colvalue);
//$url = "redirect_page.php";
$url = "

echo "<script>location.href='$url'</script>";
}

}

?>


</body>
</html>
 
location.href is javascript call and as such client side technology which is best avoided if possible. Since it is definitely not needed here, I suggest you use
Code:
header("Location: . $url);
instead.

However, be aware that header function must be called before anything is sent to the browser. That includes but is not limited to echos, prints, html code outside (before) <?php ?> tags and any whitespace (space, carriage return) before opening php tags. You can have as much code as you want before the header() call but make sure that none of the code doesn't send any output.
 
Thanks Vragabond.
I think I understand, your code goes in the header section before any other code? Do I then omit the 2 lines:

echo "<script>location.href='$url'</script>";

Many thanks
 
Yup. You omit that, you also omit all the html code before the opening php tag and you omit the printf calls.
 
I put the code in as:

<html>
<head>

<?php
header("Location: . $url);
<title>Results Form 1</title>
</head>

etc.etc

But it throws it out. Taking the other lines out, I forgot printf would print, and the other line and everything works.
 
Huh, header does not go in the <head> it goes before any output. Any. Even <html><head>. Every document served on the internet is created via two parts, headers and content. All you are displaying here is content. Headers include type of document (text/html) and a bunch of other codes that server requires to properly serve the file -- similar to what you see if you look at the headers in emails. That means as soon as there is some output, headers are closed and content begins. In your code that output begins with <html>. However, when you call your header() function, you're saying: Oh, could you add this to the headers of this file. No can do, says the server, 'headers already sent'. So, if you want to add stuff to the headers (just like header() function does) you need to do it before any output whatsoever. No html tags, no spaces, no enters. Understand?
 
Got you - It works a treat. Many thanks again. Have a star and keep you up there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top