Jump to content

Form Validation


himesh
 Share

Recommended Posts

I have changed my form validation from javascript to php. It validates fine and submits the data to the database fine, the error lies when I try to redirect to the thank you page. The error I get is,

Warning: Cannot modify header information - headers already sent by (output started at /customers/himesh.net/himesh.net/httpd.www/communicate/registerphpvalidate.php:128) in /customers/himesh.net/himesh.net/httpd.www/communicate/registerphpvalidate.php on line 158

Line 158 is the header I have put in

header('Location: http://www.himesh.net/communicate/registerok.php');

After googleing the error I found that some people said that the white spaces after the php tags may cause this error so I removed that and the still the same error occurs. Line 128 in the form validation script points to this.

				if(false == $custom_val->DoValidate($form_variables,$this->error_hash))

I have used the php form validator from here.If I am unable to automatically redirect the user, is there another way I could achieve this?

  • Like 1
Link to comment
Share on other sites

could you post all your code? Do you remove the white space and the php tags?In one of my implementations, I have a contact.php page with the form on it. I have the form's action method set to contact.php too. So when the form submits, it calls the page again and if POST isset, then it validates the form fields, sends the email, and display a thank you message above the form. However, you can just make the form submit to a "submit_contact.php" page for example, which can take the incoming POST array and send an email.As for line 128, I think it has to go the other way around, with false on the right.

Edited by thescientist
Link to comment
Share on other sites

You will send a header if you output ANY data at all. This includes HTML, blank spaces, and line breaks anywhere outside your <?php ?> tags. It can be before as well as after.The same is true if you generate any warnings that get output to the browser without terminating your script.

Link to comment
Share on other sites

Here is my entire code.

<?php require_once('Connections/conn_comm.php'); ?><?php require_once "formvalidator.php"; ?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {  if (PHP_VERSION < 6) {	$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;  }  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);  switch ($theType) {	case "text":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;		case "long":	case "int":	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";	  break;	case "double":	  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";	  break;	case "date":	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";	  break;	case "defined":	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;	  break;  }  return $theValue;}}$colname_usernamecheck = "-1";if (isset($_POST['username'])) {  $colname_usernamecheck = $_POST['username'];}mysql_select_db($database_conn_comm, $conn_comm);$query_usernamecheck = sprintf("SELECT username FROM tbluser WHERE username = %s", GetSQLValueString($colname_usernamecheck, "text"));$usernamecheck = mysql_query($query_usernamecheck, $conn_comm) or die(mysql_error());$row_usernamecheck = mysql_fetch_assoc($usernamecheck);$totalRows_usernamecheck = mysql_num_rows($usernamecheck);?><!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=utf-8" /><title>Communicate - the best in mobile communications</title><link rel="stylesheet" type="text/css" href="_css/main.css" /><link rel="stylesheet" type="text/css" href="_css/style.css" />	<script src="jquery-1.2.1.min.js" type="text/javascript"></script>	<script src="menu.js" type="text/javascript"></script><!--[if IE]><style type="text/css"> /* place css fixes for all versions of IE in this conditional comment */.thrColElsHdr #sidebar1, .thrColElsHdr #sidebar2 { padding-top: 30px; }.thrColElsHdr #mainContent { zoom: 1; padding-top: 15px; }/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */</style><![endif]--></head><body class="thrColElsHdr"><div id="container">  <div id="header">   <div id="search">   </div>     <!-- end #header -->  </div>      <div id="sidebar1"><ul id="menu">						<li><a href="http://www.himesh.net/communicate/index.php">Home</a></li>									 														<li>							<a href="#">Manufacturer</a>							<ul> 							  <li><a href="http://www.himesh.net/communicate/allmanufacturer.php">All</a></li> 							<li><a href="http://www.himesh.net/communicate/apple.php">Apple</a></li> 						   	<li><a href="http://www.himesh.net/communicate/lg.php">LG</a></li> 							<li><a href="http://www.himesh.net/communicate/nokia.php">Nokia</a></li> 							<li><a href="http://www.himesh.net/communicate/samsung.php">Samsung</a></li> 							<li><a href="http://www.himesh.net/communicate/se.php">Sony Ericsson</a></li> 							</ul> 						</li>													 <li>							<a href="#">Type</a>								<ul> 							<li><a href="http://www.himesh.net/communicate/alltype.php">All</a></li> 							<li><a href="http://www.himesh.net/communicate/slide.php">Slide</a></li> 							<li><a href="http://www.himesh.net/communicate/candy.php">Candy Bar</a></li> 							<li><a href="http://www.himesh.net/communicate/flip.php">Flip</a></li>					  							</ul> 	  </li>																  <li>							<a href="#">Features</a>							<ul> 							<li><a href="http://www.himesh.net/communicate/camera.php">Camera</a></li> 							<li><a href="http://www.himesh.net/communicate/music.php">Music</a></li> 							<li><a href="http://www.himesh.net/communicate/internet.php">Internet</a></li>												  							</ul> 						</li>									<li><a href="http://www.himesh.net/communicate/aboutus.php">About us</a></li>							<li><a href="http://www.himesh.net/communicate/contact.php">Contact us</a></li>	</ul>  <!-- end #sidebar1 -->  </div>      <div id="sidebar2">      <!-- end #sidebar2 --></div>      <div id="mainContent">	<h1> Register</h1><?php$show_form=true;if(isset($_POST['Submit'])){	$validator = new FormValidator();	$validator->addValidation("Password","eqelmnt=Password2","Your passwords do not match");	$validator->addValidation("Firstname","req","Please enter your First Name");	$validator->addValidation("Firstname","maxlen=15","Are you sure your First Name is longer than 15 letters?");	$validator->addValidation("Surname","req","Please enter your Surame");	$validator->addValidation("Surname","maxlen=15","Are you sure your Surname is longer than 15 letters?");	$validator->addValidation("Address","req","Please enter your Address");	$validator->addValidation("Address","alnum_s","Please enter only numbers and letters for your address");	$validator->addValidation("Town","req","Please enter your Town");	$validator->addValidation("Town","alnum_s","Please enter only numbers and letters for your Town");	$validator->addValidation("County","req","Please enter your County");	$validator->addValidation("County","alnum_s","Please enter only numbers and letters for your County");	$validator->addValidation("Postcode","req","Please enter your Postcode");	$validator->addValidation("Postcode","maxlen=8","Your postcode cannot exceed 8 characters");	$validator->addValidation("Postcode","alnum_s","Are you sure your postcode contains non letters and numbers?");	$validator->addValidation("Email","email","The input for Email should be a valid email value");	$validator->addValidation("Phone","num","Please emter only numbers for your phone number");	if($validator->ValidateForm())	{		$insert = sprintf("INSERT INTO tbluser (username, password, title, firstname, surname, address, address2, town, county, postcode, email, phone) 					VALUES ('$_POST[username]','$_POST[password]','$_POST[title]','$_POST[firstname]','$_POST[surname]','$_POST[address]','$_POST[address2]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[email]','$_POST[phone]')");mysql_select_db($database_conn_comm, $conn_comm);$Result1 = mysql_query($insert, $conn_comm) or die(mysql_error());header('Location: http://www.himesh.net/communicate/registerok.php');		$show_form=false;	}	else	{		echo "<B>Validation Errors:</B>";		$error_hash = $validator->GetErrors();		foreach($error_hash as $inpname => $inp_err)		{		  echo "<p>$inpname : $inp_err</p>\n";		} 	}}if(true == $show_form){?>	<p align="left">Please fill out the form below to register for an account</p><form name='register' action="registerphpvalidate.php" method="post" accept-charset='UTF-8'>	<table border="0">	  <tr>	  <td>	  <?php if($totalRows_usernamecheck !== 0){		echo '<script type="text/javascript">alert("Sorry that username has been taken, please choose another");</script>'; }?>	  </td>	  </tr>	  	  <tr>	  <td colspan="2">		<div align="right">* Denotes a required field </div></td>	  </tr>	  <tr>		<td>Username</td>		<td><label>		  <input type="text" name="username" id="username" value="" />		</label></td>		<td>*</td>	  </tr>	  <tr>		<td>Password</td>		<td><label>		  <input type="password" name="Password" id="Password" />		</label></td>		<td>*</td>	  </tr>	  <tr>		<td>Password</td>		<td><label>		  <input type="password" name="Password2" id="Password2" />		</label></td>		<td>*</td>	  </tr>	  <tr>		<td colspan="2"><div align="center">----------------------------------------</div></td>		<td> </td>	  </tr>	  <tr>		<td>Title</td>		<td><label>		  <select name="title" id="title">			<option selected="selected">Mr</option>			<option>Mrs</option>			<option>Ms</option>			<option>Dr</option>			<option>Sir</option>			<option>Rev</option>		  </select>		</label></td>		<td>*</td>	  </tr>	  <tr>		<td>First Name</td>		<td><input type="text" name="Firstname" id="Firstname" value=""/></td>		<td>*</td>	  </tr>	  <tr>		<td>Surname</td>		<td><input type="text" name="Surname" id="Surname" value=""/></td>		<td>*</td>	  </tr>	  <tr>		<td>Address</td>		<td><input type="text" name="Address" id="Address" value=""/></td>		<td>*</td>	  </tr>	   <tr>		<td>Address 2</td>		<td><input type="text" name="Address2" id="Address2" value=""/></td>		<td> </td>	  </tr>	   <tr>		<td>Town</td>		<td><input type="text" name="Town" id="Town" value=""/></td>		<td>*</td>	  </tr>	   <tr>		<td>County</td>		<td><input type="text" name="County" id="County" value=""/></td>		<td>*</td>	  </tr>	   <tr>		<td>Postcode</td>		<td><input type="text" name="Postcode" id="Postcode" value=""/></td>		<td>*</td>	  </tr>	   <tr>		<td colspan="2"><div align="center">----------------------------------------</div></td>		<td><div align="center"></div></td>	  </tr>	   <tr>		<td>Email Address</td>		<td><input type="text" name="Email" id="Email" value=""/></td>		<td>*</td>	  </tr>	   <tr>		<td>Phone Number</td>		<td><input type="text" name="Phone" id="Phone" value=""/></td>		<td> </td>	  </tr>	   <tr>		<td> </td>		<td> </td>		<td> </td>	  </tr>	   <tr>		<td><label>		  <div align="center">			<input type="reset" name="reset" id="reset" value="Reset" />		  </div>		</label></td>		<td><div align="center">		  <input type="submit" name="Submit" id="Submit" value="Submit" />		</div></td>		<td> </td>	  </tr>	</table>	</form><?PHP}//true == $show_form?><!-- end #mainContent -->  </div>		<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />   <div id="footer">	<p>Footer</p>  <!-- end #footer --></div><!-- end #container --></div></body></html><?phpmysql_free_result($usernamecheck);?>

Link to comment
Share on other sites

that's not going to work. You've already ouputted HTML code before that point. You should read up on header, it only allows for a very specific implementation. Looks like you'll have to use another method. There's an example of using Javascript here:http://bytes.com/topic/php/answers/471601-...-page-html-page

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...