Jump to content

Recommended Posts

I wrote the code below to so when a form is submitted it uploads the form data to the database then directs the user to a specific page afterwards.

 

The database query uploads just fine. But the headers dont work. It just sends the user back to the page they are on.

 

Now if i use meta tags to refresh instead of headers it works no problem. But that is kind of a bastardized way of doing it. What am I doing wrong in the code below.

 

**Note this is just a small overview of the total code.

 

<?php
include 'ealcontacts.php';
if($_POST['submit'])
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$contact1 = $_POST['contact1'];
$method1 = $_POST['method1'];
$contact2 = $_POST['contact2'];
$method2 = $_POST['method2'];

$query = "INSERT INTO contacts SET fname='$fname', lname='$lname', contact1='$contact1', method1='$method1', contact2='$contact2', method2='$method2'";

$results = mysql_query($query);
if($results)
{
	header( 'Location: listingthankyou.php?idnumber='.$row->idnumber.''' );
}
else
{
	header( 'Location: siteerror.php' );
}
}
?>

well I made the change to the header with the quotes, still just sits on the page.

 

I dont have the SQL validation, but I know that it is working because I am seeing the results hit the database each time I hit submit. 0.o

I changed the code a bit more to add error checking to SQL insert... no errors.

 

new code changes

 

Also.. i know its not the URL in the header() because for error testing i tried www.google.com and that still made the page refresh onto itself.

 

<?php
include 'ealcontacts.php';
if($_POST['submit'])
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$contact1 = $_POST['contact1'];
$method1 = $_POST['method1'];
$contact2 = $_POST['contact2'];
$method2 = $_POST['method2'];

$query = "INSERT INTO contacts SET fname='$fname', lname='$lname', contact1='$contact1', method1='$method1', contact2='$contact2', method2='$method2'";

$results = mysql_query($query);
if (!$results) {
	   die('Invalid query: ' . mysql_error());
}
else
{
	header( 'Location: listingthankyou.php?idnumber='.$row->idnumber);
}
}
?>

Earlier in the page before some HTML that seperates the aformentioned code I get $row->idnumber from here.

 

<?
$query = "SELECT * FROM Listings WHERE idnumber ='$idnumber'";
$results = mysql_query($query);
while($row = mysql_fetch_object($results))
{
echo '<tr><td>';
echo '<img src="Images/'.$row->idnumber.'a.jpg" width="255" height="150 border="0"></td>';
echo '<td>';
echo '<img src="Images/'.$row->idnumber.'b.jpg" width="255" height="150 border="0"></td></tr>';
echo '<tr><td width="550" height="100" bgcolor="#ffffff" align="right" colspan="2">';
echo '<br>Studio Apartment: <font size="2"><i>'.$row->studiosqft.'</font></i> Sq. Ft. <font size="2"><i>$'.$row->studioprice.'</font>/m  </i><br>';
echo '1 Bedroom '.$row->bathroom1.' Bathroom Apartment: <font size="2"><i>'.$row->sqft1.'</font></i> Sq. Ft. <font size="2"><i>$'.$row->price1.'</font>/m  </i><br>';
echo '2 Bedroom '.$row->bathroom2.' Bathroom Apartment: <font size="2"><i>'.$row->sqft2.'</font></i> Sq. Ft. <font size="2"><i>$'.$row->price2.'</font>/m  </i><br>';
echo '3 Bedroom '.$row->bathroom3.' Bathroom Apartment: <font size="2"><i>'.$row->sqft3.'</i> Sq. Ft. <font size="2"><i>$'.$row->price3.'</font>/m  </i><br>';
echo '<br><div align ="left"><font size="2">'.$row->details.'</font><br><br></td></tr>';
}
?>

Here's how I'd debug this:

Change the header line to:

 

echo "Inside of the ELSE conditional, query succeeded.<br />What's going into the header: Location: listingthankyou.php?idnumber={$row->idnumber}";

 

See what it says, or if it even gets up to that point, and if it's providing the correct URL.

ok well I commented out the header() and replaced it with an echo.... the echo executes no problem so there is a problem with:

 

header( 'Location: listingthankyou.php?idnumber='.$row->idnumber);

 

but I know it is not the variable, because I tried

 

header('Location: www.google.com' );

 

to no avail!

I always use double quotes with headers.  Probably does nothing, but I don't really know.  And use absolute URLs or some browsers don't take the header.

$url = "http://www.domain.com/path/file.php?idnumber={$row->idnumber}";

header("Location: $url");

 

That's how I always do it.  Try making a variable and using THAT in the header.  It worked for someone's problem with exec() once. :o

well i fixed the issue in a different way. Rather than grabbing it from earlier in the page. I just used $_GET['idnumber']; to pull it from the URL again. It's kinda funky way of doing it if i showed you the whole page. but it works nonetheless...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.