Help with form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Help with form

Post by tommy1987 »

Hello, I am trying to design a user feedback form and do some validation when the submit button is pressed, this all works fine, except when the button is pressed all entered data is lost and the pottential customer/client would have to reenter all data. Please can someone take the time to explain how to have the text remain in the input boxes of the form. The form doesnt pass the data to a seperate script, it processes it all in the same php script, using global variables.

The format of the code is (where $sub is the submit button):

if (isset($sub))
{

//validation here

}

Any help anyone can give to me (PHP n00b) is greatly appreciated!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Without seeing more code I would have no idea. My guess is you just need to output into the form elements

Code: Select all

<input type="text" name="email" value="<?php echo (htmlentities($_POST['email'])) ?>" />
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Here is the code...

Post by tommy1987 »

There are two things I want to do, firstly I want to keep the form data when the button is pressed instead of losing it and also to pass the recieved information to another PHP file when it gets to storedata() where it can be formatted and displayed to the user before they finally submit it.

Thanks, here is the code:

Code: Select all

<HTML>
<HEAD>
<TITLE> User Feedback Form </TITLE>

	<style type="text/css">

<!--Basic Text Style -->

.sentence 
{
  background-color: white;
  font-family: arial,sans-serif;
  font-size: 12px;
  font-style: bold;
  color: black;
  text-align: left;
}
	</style>
<!--Basic Error Style -->
	<style type="text/css">
DIV.error123 
{
  background-color: white;
  font-family: arial,sans-serif;
  font-size: 12px;
  color: red;
  text-align: left;
}

	</style>

</HEAD>

<BODY>
	<font face="arial" size="5"><b>Customer Feedback</b></font>
	<hr style="background" width="230" align="left">
	<div class="sentence"><b>*All fields are required</b></div><br />
	<?php
	resume();
	function no_name()
	{

		echo '<DIV class="error123"><LI>First name is required!</DIV>';
			endprog();
	}
	function no_sname()
	{
		echo '<DIV class="error123"><LI>Surname is required!</DIV>';
			endprog();
	}
	function no_check()
	{
		echo '<DIV class="error123"><LI>Aggreement to the terms and conditions is required!</DIV>';
			endprog();
		
	}
	function no_comment()
	{
		echo '<DIV class="error123"><LI>Comment is required!</DIV>';
			endprog();
	}
	function no_email()
	{
		echo '<DIV class="error123"><LI>Email is required!</DIV>';
			endprog();
	}

	?>
	<?php
	function resume()
	{
		$date = date('jS F'. " @ ". 'H:i');
	
	}
	?>
	<p>
		<table border="0">
			<form method="post">
				<tr>
					<td class="labels">First Name </td>
					<td><input type="text" name="fname" maxlength="42" size="42"></td>
				</tr>
				<tr>
					<td class="labels">Surname </td>
					<td><input type="text" name="sname" maxlength="42" size="42"></td>
				</tr>
				<tr>
					<td class="labels">E-mail </td>
					<td><input type="text" name="email" maxlength="42" size="42"></td>
				</tr>
				<tr>
					<td class="labels">Comment </td>
					<td><textarea maxlength="512" rows="10" cols="32" name="comment"></textarea></td>
				</tr>
				<tr>
					<td></td>
					<td class="labels"><input type="checkbox" name="check">I agree to the <a href="#">Terms and Conditions</a></td>
					
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="Submit" name="sub"></td>
				</tr>
				
			</form>
			
		</table>
</BODY>
</HTML>

<?php 

	if (isset($sub))
		
		//Event handler: if the submit button is pressed...
	{
		

		if ($check == "" || $fname == "" || $sname == "" || $comment == "" || $email == "")
		{
			//Check what information has not been provided WITHOUT LOSING INFO ALREADY ENTERED TO FORM (HOW???)

		echo '<DIV class="error123">';
		echo '<b>All of the fields must be entered!<br /></b></DIV><OL>';

		if ($check == "")
			no_check();

		if ($fname == "")
			no_name();

		if ($sname == "")
	
			no_sname();
	
		if ($comment == "")
		
			no_comment();

		if ($email == "")

			no_email();
			
			
			
		
		}
		
		else
		{


			storedata();
		}

	}
	function storedata()
	{
		//Code to pass the information to another PHP script to tabulate and display it all nice for the
		//user to confirm they want to send it!

		echo '<div class="sentence"><b>Thank you '. $_POST['fname']. ' your feedback has been sent!</b></div>';
		

	}

	function endprog()
	{
		//Provide a function to use to jump to the end of the program
	}


		?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Yep the thing I just posted should satisfy your first requirement. If you use include() to include another PHP file it will be able to see all your variables already.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

like jshpro2 said, echo out the submitted data

Code: Select all

<td class="labels">First Name </td>
<td>
<input type="text" name="fname" value="<?php echo (isset($_POST['fname'])) ? htmlentities($_POST['fname']) : '';?>">
</td>
Post Reply