/    Sign up×
Community /Pin to ProfileBookmark

The below code is giving me some grief. The snipit below I believe is the cause of h problem. its not redirecting correctly when I submit the form to search. the code stops and does not include the message and the “leftlinks’

any idea how to reorganize the coed to fix this

[code=php]
header(“location: SearchForm.php?msg=$msg”);

exit(); // if the error checking has failed, exit the script!
}
[/code]

[code=php]
<?php
/* Check User Script */
session_start(); // Start Session
include (“Connections/db.php”);
//check if user is already logged in
if (!isset($_SESSION[‘user_id’])){

$msg = ‘You tryed to access a members only page. Login or become a registered member to access that page!’;

header(“Location: index.php?msg=”.$msg);
}

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Search</title>

<link rel=”stylesheet” type=”text/css” href=”design/niftyCorners.css”>
<link rel=”stylesheet” type=”text/css” href=”design/niftyPrint.css” media=”print”>
<script type=”text/javascript” src=”design/nifty.js”></script>

<link href=”design/main.css” rel=”stylesheet” type=”text/css” />

<script type=”text/javascript”>
window.onload=function(){
if(!NiftyCheck())
return;
<?php include(‘design/round.php’); ?>
}
</script>
</head>

<body>

<div id=”outer”>
<?php include(‘design/banner.php’); ?>

<div id=”sub”>
<div id=”center”>
<?php
$msg = (isset($_GET[‘msg’]) AND $_GET[‘msg’]<> “”) ? urldecode($_GET[‘msg’]) : ”;
if ($msg <> “”) {
if (get_magic_quotes_gpc()) {
$msg = stripslashes($msg);
}
echo “<div id=’formmessage’><p style=’margin-left:40px;’>$msg</p></div>”;
}
?>

<form name=”form1″ method=”post” action=”<?php echo $PHP_SELF; ?>”>
<table class=”columntable” cellspacing=”2″>
<th class=’tableheader’ colspan=’2′>Search familyclick.ca</td><br></tr>
<tr>
<td class=”rowLeft” scope=”row”><div align=”left”><strong>First Name*</strong></div></td>
<td class=”rowRight”><input name=”s_firstname” type=”text” id=”s_firstname” size=”15″></td>
</tr>
<tr>
<td class=”rowLeft” scope=”row”><div align=”left”><strong>Last Name*</strong></div></td>
<td class=”rowRight”><input name=”s_lastname” type=”text” id=”s_lastname” size=”15″></td>
</tr>

<tr>
<th colspan=”2″ scope=”row”><div align=”left”>
<input type=”submit” name=”Submit” value=”Submit”>
</div>
</tr>
</table>
</form>

<form name=”rowLeft” method=”post” action=”addbuddy.php”>
<table style=”margin:10px auto 0 auto; width:90%” class=”columnTable”>
<tr>
<td class=”tableheader” colspan=”3″>Search Results</td>
</tr>

<tr>
<td class=”rowLeft” style=”width:5%;”>Buddy</td>
<td class=”rowRight” style=”width:40%;”> Name</td>
<td class=”rowRight” style=”width:55%;”> Address</td>
</tr>

<?php
if(isset($_POST[‘Submit’])){
$s_firstname = trim($_POST[‘s_firstname’]);
$s_lastname = trim($_POST[‘s_lastname’]);

if((!$s_firstname) || (!$s_lastname))
{

if(!$s_firstname)

{
$msg .= “Enter First Name.<br />”;
}

if(!$s_lastname)
{
$msg .= “Enter Last Name.<br />”;
}

header(“location: SearchForm.php?msg=$msg”);

exit(); // if the error checking has failed, exit the script!
}

$query = (“SELECT * FROM users WHERE first_name LIKE ‘$s_firstname%’ AND last_name LIKE ‘$s_lastname%'”);

$result=mysql_query($query)or die(“Get Search results Error: “.mysql_error());

if(mysql_num_rows($result) < 1) {

$msg .= ‘<p> No matches found</p>’;

header(“location: SeachForm.php?msg=$msg”);
}

while($r=mysql_fetch_assoc($result))
{
$firstname = $r[‘first_name’];
$lastname = $r[‘last_name’];
$street = $r[‘street_address’];
$pobox = $r[‘post_office_box’];
$city = $r[‘city’];
$provice = $r[‘province’];
$postal = $r[‘postal’];
$dob = $r[‘DOB’];
$id = $r[‘user_id’];
?>

<tr>
<td class=”rowRight” style=”width:5%;”><input type=radio name=”addbuddy” value=”<?php echo $id; ?>”></td>

<td class=”rowRight” style=”width:40%;”><a href=”SearchDetails.php?owner_id=<?php echo $id; ?>”><?php echo $firstname.’ ‘. $lastname ?></a></td>
<td class=”rowRight” style=”width:55%;”> <?php echo $street.’ ‘. $pobox.’ ‘. $city.’ ‘. $province.’ ‘. $postal; ?></td>

</tr>
<?php }

}?>

</table>
<p><input style=”text-align:left;” type=”submit” name=”submit” value=”Add Buddies”></p>
</form>

</div>
<div id=”left”>

<?php include(‘design/leftlinks.php’); ?>
</div>

</div>

<div id=”right”></div>
<div id=”footer”>
<?php include(‘design/footer.php’); ?>
</div>
</div>

</body>
</html>

[/code]

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 02.2007 — See if this helps:
[code=php]
header("Location: index.php?msg=" . urlencode($msg));
[/code]
Copy linkTweet thisAlerts:
@kprocauthorFeb 02.2007 — that did not help, its still doing the same thing. stops after the form ends. also if there is not search results it does the something. could this be because I have the header redirecting to its self
Copy linkTweet thisAlerts:
@NightShift58Feb 02.2007 — It didn't help because you didnt have anything critical in $msg - but you should always use it...
Copy linkTweet thisAlerts:
@NightShift58Feb 02.2007 — This:[code=php]if (!isset($_SESSION['user_id'])){

$msg = 'You tryed to access a members only page. Login or become a registered member to access that page!';

header("Location: index.php?msg=".$msg);
}[/code]
should be:[code=php]if (!isset($_SESSION['user_id'])){

$msg = 'You tryed to access a members only page. Login or become a registered member to access that page!';

header("Location: index.php?msg=".$msg);
exit;
}
[/code]
Copy linkTweet thisAlerts:
@kprocauthorFeb 02.2007 — hi

the problem is still occuring., the message is not displayed when the submit button is clicked. if shows the form but end after that. the lift links is not included
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — Strange... but maybe this:[code=php]<?php
/* Check User Script */
session_start(); // Start Session
//check if user is already logged in
if (!isset($_SESSION['user_id'])){
$msg = 'You tryed to access a members only page. Login or become a registered member to access that page!';
header("Location: index.php?msg=" . urlencode($msg));
exit;
} else {
include ("Connections/db.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search</title>

<link rel="stylesheet" type="text/css" href="design/niftyCorners.css">
<link rel="stylesheet" type="text/css" href="design/niftyPrint.css" media="print">
<script type="text/javascript" src="design/nifty.js"></script>

<link href="design/main.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
window.onload=function(){
if(!NiftyCheck())
return;
<?php include('design/round.php'); ?>
}
</script>
</head>

<body>


<div id="outer">
<?php include('design/banner.php'); ?>

<div id="sub">
<div id="center">
<?php
$msg = (isset($_GET['msg']) AND $_GET['msg']<> "") ? urldecode($_GET['msg']) : '';
if ($msg <> "") {
if (get_magic_quotes_gpc()) {
$msg = stripslashes($msg);
}
echo "<div id='formmessage'><p style='margin-left:40px;'>$msg</p></div>";
}
?>

<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<table class="columntable" cellspacing="2">
<th class='tableheader' colspan='2'>Search familyclick.ca</td><br></tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>First Name*</strong></div></td>
<td class="rowRight"><input name="s_firstname" type="text" id="s_firstname" size="15"></td>
</tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>Last Name*</strong></div></td>
<td class="rowRight"><input name="s_lastname" type="text" id="s_lastname" size="15"></td>
</tr>

<tr>
<th colspan="2" scope="row"><div align="left">
<input type="submit" name="Submit" value="Submit">
</div>
</tr>
</table>
</form>

<form name="rowLeft" method="post" action="addbuddy.php">
<table style="margin:10px auto 0 auto; width:90%" class="columnTable">
<tr>
<td class="tableheader" colspan="3">Search Results</td>
</tr>

<tr>
<td class="rowLeft" style="width:5%;">Buddy</td>
<td class="rowRight" style="width:40%;"> Name</td>
<td class="rowRight" style="width:55%;"> Address</td>
</tr>


<?php
if(isset($_POST['Submit'])){
$s_firstname = trim($_POST['s_firstname']);
$s_lastname = trim($_POST['s_lastname']);

if((!$s_firstname) || (!$s_lastname))
{


if(!$s_firstname)

{
$msg .= "Enter First Name.<br />";
}

if(!$s_lastname)
{
$msg .= "Enter Last Name.<br />";
}

header("location: SearchForm.php?msg=$msg");

exit(); // if the error checking has failed, exit the script!
}

$query = ("SELECT * FROM users WHERE first_name LIKE '$s_firstname%' AND last_name LIKE '$s_lastname%'");

$result=mysql_query($query)or die("Get Search results Error: ".mysql_error());

if(mysql_num_rows($result) < 1) {

$msg .= '<p> No matches found</p>';

header("location: SeachForm.php?msg=$msg");
}

while($r=mysql_fetch_assoc($result))
{
$firstname = $r['first_name'];
$lastname = $r['last_name'];
$street = $r['street_address'];
$pobox = $r['post_office_box'];
$city = $r['city'];
$provice = $r['province'];
$postal = $r['postal'];
$dob = $r['DOB'];
$id = $r['user_id'];
?>

<tr>
<td class="rowRight" style="width:5%;"><input type=radio name="addbuddy" value="<?php echo $id; ?>"></td>

<td class="rowRight" style="width:40%;"><a href="SearchDetails.php?owner_id=<?php echo $id; ?>"><?php echo $firstname.' '. $lastname ?></a></td>
<td class="rowRight" style="width:55%;"> <?php echo $street.' '. $pobox.' '. $city.' '. $province.' '. $postal; ?></td>

</tr>
<?php }

}?>

</table>
<p><input style="text-align:left;" type="submit" name="submit" value="Add Buddies"></p>
</form>


</div>
<div id="left">

<?php include('design/leftlinks.php'); ?>
</div>

</div>

<div id="right"></div>
<div id="footer">
<?php include('design/footer.php'); ?>
</div>
</div>

</body>
</html>
<?php
}
?>[/code]
Copy linkTweet thisAlerts:
@kprocauthorFeb 03.2007 — here is a link

http://www.familyclick.ca/SearchForm.php

leave the form blank and click the submit but to see what its doing
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — You meant "familyclick.ca"...
[code=php]<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>"> [/code]is what takes you back to the same form. Apparently, session ID is already set, even if it's empty, and, thus, you skip right through it...
Copy linkTweet thisAlerts:
@kprocauthorFeb 03.2007 — I removed the top code so that you could display the page. Its not displaying the whole page if there are empty fields when a field was left empty
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — [code=php] if((!$s_firstname) || (!$s_lastname)) {
if(!$s_firstname) {
$msg .= "Enter First Name.<br />";
}
if(!$s_lastname) {
$msg .= "Enter Last Name.<br />";
}
header("location: SearchForm.php?msg=$msg");
exit(); // if the error checking has failed, exit the script!
}[/code]
This part is what breaks down.

When the user clicks on submit, he goes through the page again. By the time you try to redrect him, there's alwreay been content sent out and header() doesn't like that. You'll to move this part to the top of the script, before any ouput takes place.
Copy linkTweet thisAlerts:
@kprocauthorFeb 03.2007 — this is driving me crazy, so easy but so complicated, i moved the code to the top and it messes up the page layout
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — Like this?[code=php]<?php
if(isset($_POST['Submit'])){
$s_firstname = trim($_POST['s_firstname']);
$s_lastname = trim($_POST['s_lastname']);

if((!$s_firstname) || (!$s_lastname)) {
if(!$s_firstname) {
$msg .= "Enter First Name.<br />";
}
if(!$s_lastname) {
$msg .= "Enter Last Name.<br />";
}
header("location: SearchForm.php?msg=$msg");
exit(); // if the error checking has failed, exit the script!
}
}

/* Check User Script */
session_start(); // Start Session
//check if user is already logged in
if (!isset($_SESSION['user_id'])){

$msg = 'You tryed to access a members only page. Login or become a registered member to access that page!';
header("Location: index.php?msg=" . urlencode($msg));
exit;

} else {

include ("Connections/db.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search</title>

<link rel="stylesheet" type="text/css" href="design/niftyCorners.css">
<link rel="stylesheet" type="text/css" href="design/niftyPrint.css" media="print">
<script type="text/javascript" src="design/nifty.js"></script>

<link href="design/main.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
window.onload=function(){
if(!NiftyCheck())
return;
<?php include('design/round.php'); ?>
}
</script>
</head>

<body>
<div id="outer">
<?php include('design/banner.php'); ?>
<div id="sub">
<div id="center">
<?php
$msg = (isset($_GET['msg']) AND $_GET['msg']<> "") ? urldecode($_GET['msg']) : '';
if ($msg <> "") {
if (get_magic_quotes_gpc()) {
$msg = stripslashes($msg);
}
echo "<div id='formmessage'><p style='margin-left:40px;'>$msg</p></div>";
}
?>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<table class="columntable" cellspacing="2">
<th class='tableheader' colspan='2'>Search familyclick.ca</td><br></tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>First Name*</strong></div></td>
<td class="rowRight"><input name="s_firstname" type="text" id="s_firstname" size="15"></td>
</tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>Last Name*</strong></div></td>
<td class="rowRight"><input name="s_lastname" type="text" id="s_lastname" size="15"></td>
</tr>

<tr>
<th colspan="2" scope="row"><div align="left">
<input type="submit" name="Submit" value="Submit">
</div>
</tr>
</table>
</form>

<form name="rowLeft" method="post" action="addbuddy.php">
<table style="margin:10px auto 0 auto; width:90%" class="columnTable">
<tr>
<td class="tableheader" colspan="3">Search Results</td>
</tr>

<tr>
<td class="rowLeft" style="width:5%;">Buddy</td>
<td class="rowRight" style="width:40%;"> Name</td>
<td class="rowRight" style="width:55%;"> Address</td>
</tr>


<?php
if(isset($_POST['Submit'])){

$query = ("SELECT * FROM users WHERE first_name LIKE '$s_firstname%' AND last_name LIKE '$s_lastname%'");
$result=mysql_query($query)or die("Get Search results Error: ".mysql_error());

if(mysql_num_rows($result) < 1) {
$msg .= '<p> No matches found</p>';
header("location: SeachForm.php?msg=$msg");
}

while($r=mysql_fetch_assoc($result)) {
$firstname = $r['first_name'];
$lastname = $r['last_name'];
$street = $r['street_address'];
$pobox = $r['post_office_box'];
$city = $r['city'];
$provice = $r['province'];
$postal = $r['postal'];
$dob = $r['DOB'];
$id = $r['user_id'];
?>

<tr>
<td class="rowRight" style="width:5%;"><input type=radio name="addbuddy" value="<?php echo $id; ?>"></td>

<td class="rowRight" style="width:40%;"><a href="SearchDetails.php?owner_id=<?php echo $id; ?>"><?php echo $firstname.' '. $lastname ?></a></td>
<td class="rowRight" style="width:55%;"> <?php echo $street.' '. $pobox.' '. $city.' '. $province.' '. $postal; ?></td>

</tr>
<?php
}
}
?>

</table>
<p><input style="text-align:left;" type="submit" name="submit" value="Add Buddies"></p>
</form>


</div>
<div id="left">

<?php include('design/leftlinks.php'); ?>
</div>

</div>

<div id="right"></div>
<div id="footer">
<?php include('design/footer.php'); ?>
</div>
</div>

</body>
</html>
<?php
}
?>[/code]
Copy linkTweet thisAlerts:
@kprocauthorFeb 03.2007 — once again nightshift58, i owe ya big time. thank you ?
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — That's at least 2 suds...

You're welcome!
Copy linkTweet thisAlerts:
@kprocauthorFeb 03.2007 — the message for if no message is found does not work. if no matches are found nothing re returned.
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — It's the same problem as the previous header()...
Copy linkTweet thisAlerts:
@NightShift58Feb 03.2007 — [code=php]<?php
if(isset($_POST['Submit'])){
$s_firstname = trim($_POST['s_firstname']);
$s_lastname = trim($_POST['s_lastname']);

$msg = "";
if((!$s_firstname) || (!$s_lastname)) {
if(!$s_firstname) {
$msg .= "Enter First Name.<br />";
}
if(!$s_lastname) {
$msg .= "Enter Last Name.<br />";
}
header("location: SearchForm.php?msg=" . urlencode($msg));
exit(); // if the error checking has failed, exit the script!
}

include ("Connections/db.php");

$query = ("SELECT * FROM users WHERE first_name LIKE '$s_firstname%' AND last_name LIKE '$s_lastname%' LIMIT 1");
$result = @mysql_query($query)or die("Get Search results Error: ".mysql_error());

if(mysql_num_rows($result) == 0) {
$msg .= '<p> No matches found</p>';
header("location: SearchForm.php?msg=" . urlencode($msg));
exit(); // if the error checking has failed, exit the script!
}

$r=mysql_fetch_assoc($result)) {
$firstname = $r['first_name'];
$lastname = $r['last_name'];
$street = $r['street_address'];
$pobox = $r['post_office_box'];
$city = $r['city'];
$province = $r['province'];
$postal = $r['postal'];
$dob = $r['DOB'];
$id = $r['user_id'];
}

/* Check User Script */
session_start(); // Start Session
//check if user is already logged in
if (!isset($_SESSION['user_id'])){

$msg = 'You tryed to access a members only page. Login or become a registered member to access that page!';
header("Location: index.php?msg=" . urlencode($msg));
exit;

} else {

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search</title>

<link rel="stylesheet" type="text/css" href="design/niftyCorners.css">
<link rel="stylesheet" type="text/css" href="design/niftyPrint.css" media="print">
<script type="text/javascript" src="design/nifty.js"></script>

<link href="design/main.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
window.onload=function(){
if(!NiftyCheck())
return;
<?php include('design/round.php'); ?>
}
</script>
</head>

<body>
<div id="outer">
<?php include('design/banner.php'); ?>
<div id="sub">
<div id="center">
<?php
$msg = (isset($_GET['msg']) AND $_GET['msg']<> "") ? urldecode($_GET['msg']) : '';
if ($msg <> "") {
if (get_magic_quotes_gpc()) {
$msg = stripslashes($msg);
}
echo "<div id='formmessage'><p style='margin-left:40px;'>$msg</p></div>";
}
?>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<table class="columntable" cellspacing="2">
<th class='tableheader' colspan='2'>Search familyclick.ca</td><br></tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>First Name*</strong></div></td>
<td class="rowRight"><input name="s_firstname" type="text" id="s_firstname" size="15"></td>
</tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>Last Name*</strong></div></td>
<td class="rowRight"><input name="s_lastname" type="text" id="s_lastname" size="15"></td>
</tr>

<tr>
<th colspan="2" scope="row"><div align="left">
<input type="submit" name="Submit" value="Submit">
</div>
</tr>
</table>
</form>

<form name="rowLeft" method="post" action="addbuddy.php">
<table style="margin:10px auto 0 auto; width:90%" class="columnTable">
<tr>
<td class="tableheader" colspan="3">Search Results</td>
</tr>

<tr>
<td class="rowLeft" style="width:5%;">Buddy</td>
<td class="rowRight" style="width:40%;"> Name</td>
<td class="rowRight" style="width:55%;"> Address</td>
</tr>

<?php
if(isset($_POST['Submit'])){
<tr>
<td class="rowRight" style="width:5%;"><input type=radio name="addbuddy" value="<?php echo $id; ?>"></td>
<td class="rowRight" style="width:40%;"><a href="SearchDetails.php?owner_id=<?php echo $id; ?>"><?php echo $firstname.' '. $lastname ?></a></td>
<td class="rowRight" style="width:55%;"> <?php echo $street.' '. $pobox.' '. $city.' '. $province.' '. $postal; ?></td>
</tr>
<?php
}
?>

</table>
<p><input style="text-align:left;" type="submit" name="submit" value="Add Buddies"></p>
</form>

</div>
<div id="left"><?php include('design/leftlinks.php'); ?></div>
</div>
<div id="right"></div>
<div id="footer"><?php include('design/footer.php'); ?></div>
</div>
</body>
</html>
<?php
}
?>[/code]
×

Success!

Help @kproc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...