Page 1 of 1

Error when submitting a form

Posted: Sun Nov 12, 2006 6:54 am
by evilchris2003
Hi there

im reletivly new to PHP i am using a web form to register a user

the problem arises when i submit i get a 404: File not found error

im using the below code to handle the form

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="POST">

i know there are no syntax errors on the page

I am willing to post the full page code if anyone feels it is necessary however i did not want to bog the post down with it initially

Posted: Sun Nov 12, 2006 7:17 am
by feyd
If you alter that to use # as the action target, how does the behavior change?

Posted: Sun Nov 12, 2006 7:22 am
by evilchris2003
get the same 404 error

Posted: Sun Nov 12, 2006 7:25 am
by feyd
Do other (relative) links (on this same page) also produce this error? If so, does you page have a base href set (incorrectly)?

Posted: Sun Nov 12, 2006 7:32 am
by evilchris2003
I have just clicked every link on the page (about 8 total)

and each works fine in respect to what is supposed to happen altho these dont perform any function other than the standard HREF link

Posted: Sun Nov 12, 2006 7:39 am
by feyd
Hmm, okay. I think it's time to see more of the code. Please remember to place it in

Code: Select all

tags so our code highlighter will parse it.

Posted: Sun Nov 12, 2006 7:46 am
by evilchris2003
Ok here it is because im a novice large parts are extracted from a book im using
This is the complete page

Code: Select all

<?php # Register.php

$page_title = 'Star Racing Registration';

include ('./header.inc');
?>
<BR><BR> 
<FONT FACE="Tahoma"><B>
<div id="navcontainer">
<ul id="navlist">
<li>| <A HREF="index.php">Home</a></li> |
<li><A HREF="about.php"> About Us</a></li> |
<li><A HREF="tips.php"> Tips</a></li> |
<li><A HREF="past.php"> Past Results</a></li> |
<li><A HREF="account.php"> My Account</A></li> | 
<li><A HREF="faq.php"> FAQ</A></li> | 
<li><A HREF="links.php">Links</A></li> | 
</ul>
</div>
<?php
if (isset($_POST['submit']))
 { // Handle Form

	require_once ('./mysql_connect.php');

	function escape_data ($data)
		{
			global $dbc; 
			if (ini_get('magic_quotes_gpc'))
			{
				$data = stripslashes($data);
			}
			return mysql_real_escape_string ($data, $dbc);
		}
	$message = NULL; //Create Empty message
	
	if (empty($_POST['username']))
		{ $un = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to enter your Username!!</P></FONT>';
		}
		else {
			$un = escape_data($_POST['username']);
		}	

	if (empty($_POST['password']))
		{ $pw = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your Password!!</P></FONT>';
		}
		else {
			if ($_POST['password'] == $_POST['confirmpass'])
				{
				$pw = escape_data($_POST['password']);
				}
				else {
					$pw = FALSE;
					$message = '<P><FONT COLOR="red">Your Password did not match!! Please try again</P></FONT>';
				     }
		}

	if (empty($_POST['first_name']))
		{ $first = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your First name!!</P></FONT>';
		}
		else {
			$first = escape_data($_POST['first_name']);
		}
	
	if (empty($_POST['last_name']))
		{ $last = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to enter your Last name!!</P></FONT>';
		}
		else {
			$last = escape_data($_POST['last_name']);
		}

	if (empty($_POST['address']))
		{ $add = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your Address!!</P></FONT>';
		}
		else {
			$add = escape_data($_POST['address']);
		}

	if (empty($_POST['email']))
		{ $em = FALSE;
		  $message = '<P><FONT COLOR="red">You Need to 
			enter your Email Address!!</P></FONT>';
		}
		else {
			$em = ($_POST['email']);
		}

}

if ($un && $pw && $first && $last && $add && $em)
{
	//Register the user
	
	//Database Query
	$query = "INSERT INTO Customers (username, password, first_name, last_name, address, email, reg_date)
		  VALUES ('$un', PASSWORD ('$pw'), '$first', '$last', '$add', '$em', NOW())";
	$result = @mysql_query ($query); //Execute
	if ($result)
		{
			echo '<P><B>Congratulations you are now registered! <P>';
			echo '<P><B>We have sent an Email to $em to confirm your registration details with us<P><P>';
			include ('./footer.inc');
			exit();
		}
		else
		{
			//If we get problems 
			$message = '<P><B>You could not be registered because of a system error.</P><P>' . mysql_error() . '</P>';
			include ('./footer.inc');
			
		}	
			mysql_close();

		if (isset($message))
		{
			echo '<FONT COLOR="red">' , $message, '</FONT>';
		}
}

?>
<form action="#" METHOD="POST">

<TABLE WIDTH=200 ALIGN=center BORDER=0 CELLSPACING=10 CELLPADDING=15>
<TR>
<TD><B>Username :</TD>
<TD><INPUT TYPE="text" NAME="Username" SIZE="15" MAXLENGTH="40" value="<?php if (isset($_POST['username'])); ?>"></TD></TR>
<TR>
<TD><B>Password :</TD>
<TD><INPUT TYPE="password" NAME="password" SIZE="15" MAXLENGTH="20"></TD></TR>
<TR>
<TD><B>Re-type Password :</TD>
<TD><INPUT TYPE="password" NAME="confirmpass" SIZE="15" MAXLENGTH="20"></TD></TR>
<TR>
<TD><B>FirstName :</TD> 
<TD><INPUT TYPE="text" NAME="first_name" SIZE="15" MAXLENGTH="40" value="<?php if (isset($_POST['first_name'])); ?>"></TD></TR>
<TR>
<TD><B>Surname :</TD> 
<TD><INPUT TYPE="text" NAME="last_name" SIZE="15" MAXLENGTH="40" value="<?php if (isset($_POST['last_name'])); ?>"></TD></TR>
<TR>
<TD><B>Address :</TD>
<TD><TEXTAREA NAME="address" ROWS=5 COLUMNS=20 WRAP ALIGN="top" value="<?php if (isset($_POST['address'])); ?>"></TEXTAREA></TD></TR>
<TR>
<TD><B>Email :</TD> 
<TD><INPUT TYPE="text" NAME="email" SIZE="40" MAXLENGTH="100" value="<?php if (isset($_POST['email'])); ?>"></TD></TR>
<TR><TD COLSPAN=2>
<FONT SIZE=2> <B>Please Check the box to indicate you have read and understood the <A HREF="terms.html" TARGET="_blank">Terms and Conditions</A>
 of using Star Racing <INPUT TYPE="checkbox" NAME="terms" Value="checked"></TD></TR>
<TR>
<TD><INPUT TYPE="submit" VALUE="Register"></TD>
<TD><INPUT TYPE="reset" VALUE="Clear Form"></TD></TR>
</TABLE>

<?php include ('./footer.inc'); ?>

Posted: Mon Nov 13, 2006 1:03 am
by RobertGonzalez
Change that $_SERVER['PHP_SELF'] to basename($_SERVER['SCRIPT_FILENAME']). Load the page and before you post it, view the source to see what the page name is that the form is expecting to post to. Make sure that it is the same name as the name of the file you are in and if it is, post it and see what happens. Post your findings back here.

Posted: Mon Nov 13, 2006 2:46 am
by evilchris2003
right many thanks that works

only problem now is pointing require_once('./mysql_connect.php'); at the right place on the server
i was thinking it was similar to the redirect using
$_SERVER['HTTP_HOST'] . dirname($_SERVER['foldername']) . "/register.php")

i will attempt to figure that out later work beckons

many thanks for you help and to feyd it is much appreciated