Solved

How to make posted data display in form that is inside a function

Posted on 2009-07-02
5
221 Views
Last Modified: 2012-05-07
I am trying to create a page that validates form data on the server side so I am using a tutorial at:  http://www.phpf1.com/tutorial/php-form.html?page=3.  The problem I am having is that when the user hits the submit button and there is an error in their submission then the form loses all of it's data and the user has to retype everything... it is annoying.  As you can see below I can get the first name as it was submitted by using       $first_name = $_POST['first_name'];  but I don't know how to make "value=$first_name" work inside of the function... I'm not very good with functions.  Does anyone know if it is possible to get the user data to reappear in the fields after it is validated?  If this functions system won't work can you provide a link to a tutorial that will work.  

Thanks,
<?php include("start.php"); ?>
 

<div id="col2">

<div id="body_header">

Add User

</div>

<br>

<div id="body_font">
 
 

<?

// if the user submitted the form here are the values to repopulate the form fields

	$first_name = $_POST['first_name'];

	$last_name = $_POST['last_name'];

	$user_name = $_POST['user_name'];

	$password = $_POST['password'];

	$email = $_POST['email'];

	$state = $_POST['state'];

	

	echo "First name works here - " . $first_name ;

	

      // Function to display form

      function showForm($error_first_name=false,$error_last_name=false,$error_user_name=false,$error_email=false,$error_password=false,$error_state=false){

      if ($error_first_name) $error_text_first_name = "Please enter a first name between 2 and 20 characters!";

      if ($error_last_name) $error_text_last_name = "Please enter a last name between 2 and 20 characters!";

      if ($error_user_name) $error_text_user_name = "Please enter a user name between 2 and 20 characters!";

      if ($error_email) $error_text_email = "Please enter a valid email address!";

      if ($error_password) $error_text_password = "Please enter a password name between 2 and 20 characters!";

      if ($error_state) $error_text_state = "Please select a state!";

       

      echo '<form action="" method="POST">';

       

      // Display first name field an error if needed

      echo '<label for="first_name">First Name:</label><input type="text" name="first_name" value="'.$first_name.'">';

      if ($error_first_name) echo " * $error_text_first_name";

      echo '<br>';

       

      // Display last name field an error if needed

      echo '<label for="last_name">Last Name:</label><input type="text" name="last_name">';

      if ($error_last_name) echo " * $error_text_last_name";

      echo '<br>';

       

      // Display usre name field an error if needed

      echo '<label for="user_name">User Name:</label><input type="text" name="user_name">';

      if ($error_user_name) echo " * $error_text_user_name";

      echo '<br>';

      

      // Display email field an error if needed

      echo '<label for="email">Email:</label><input type="text" name="email">';

      if ($error_email) echo " * $error_text_email";

      echo '<br>';

      

      // Display password field an error if needed

      echo '<label for="password">Password:</label><input type="text" name="password">';

      if ($error_password) echo " * $error_text_password";

      echo '<br>';

      

      // Display email field an error if needed

?>  <label for="state">State:</label>

	<select name="state" size="1" value="">

	<option value="0"></option>

	<option value="1">Alabama</option>

	<option value="2">Alaska</option>

	<option value="3">Arizona</option>

	<option value="4">Arkansas</option>

	<option value="5">California</option>

	<option value="6">Colorado</option>

	<option value="7">Connecticut</option>

	<option value="8">Delaware</option>

	<option value="9">Dist of Columbia</option>

	<option value="10">Florida</option>

	<option value="11">Georgia</option>

	<option value="12">Guam</option>

	<option value="13">Hawaii</option>

	<option value="14">Idaho</option>

	<option value="15">Illinois</option>

	<option value="16">Indiana</option>

	<option value="17">Iowa</option>

	<option value="18">Kansas</option>

	<option value="19">Kentucky</option>

	<option value="20">Louisiana</option>

	<option value="21">Maine</option>

	<option value="22">Maryland</option>

	<option value="23">Massachusetts</option>

	<option value="24">Michigan</option>

	<option value="25">Minnesota</option>

	<option value="26">Mississippi</option>

	<option value="27">Missouri</option>

	<option value="28">Montana</option>

	<option value="29">Nebraska</option>

	<option value="30">Nevada</option>

	<option value="31">New Hampshire</option>

	<option value="32">New Jersey</option>

	<option value="33">New Mexico</option>

	<option value="34">New York</option>

	<option value="35">North Carolina</option>

	<option value="36">North Dakota</option>

	<option value="37">Ohio</option>

	<option value="38">Oklahoma</option>

	<option value="39">Oregon</option>

	<option value="40">Pennsylvania</option>

	<option value="41">Rhode Island</option>

	<option value="42">South Carolina</option>

	<option value="43">South Dakota</option>

	<option value="44">Tennessee</option>

	<option value="45">Texas</option>

	<option value="46">Utah</option>

	<option value="47">Vermont</option>

	<option value="48">Virginia</option>

	<option value="49">Washington</option>

	<option value="50">West Virginia</option>

	<option value="51">Wisconsin</option>

	<option value="52">Wyoming</option>

	</select> <?

      if ($error_state) echo " * $error_text_state";

      echo '<br><br>';

     

      

      echo '<input type="submit" name="SubmitForm" value="Submit" id="submitbutton">';

      echo '<form>';

      }

       

       

      if (!isset($_POST['SubmitForm'])) {

      showForm();

      } else {

      //Init error variables

      $error_first_name = false;

      $error_last_name = false;

      $error_user_name = false;

      $error_email = false;

      $error_password = false;

      $error_state = false;

       

      $first_name = isset($_POST['first_name']) ? trim($_POST['first_name']) : '';

      $last_name = isset($_POST['last_name']) ? trim($_POST['last_name']) : '';

      $user_name = isset($_POST['user_name']) ? trim($_POST['user_name']) : '';

      $email = isset($_POST['email']) ? trim($_POST['email']) : '';

      $password = isset($_POST['password']) ? trim($_POST['password']) : '';

      $state = isset($_POST['state']) ? trim($_POST['state']) : '';

       

      if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $error_email = true;

      if (strlen($first_name)<3) $error_first_name = true;

      if (strlen($first_name)>20) $error_first_name = true;

      if (strlen($last_name)<3) $error_last_name = true;

      if (strlen($last_name)>20) $error_last_name = true;

      if (strlen($user_name)<3) $error_user_name = true;

      if (strlen($user_name)>20) $error_user_name = true;

      if (strlen($password)<3) $error_password = true;

      if (strlen($password)>20) $error_password = true;

      if (strlen($email)<10) $error_email = true;

      if (($state)==0) $error_state = true;

       

      // Display the form again as there was an error

      if ($error_first_name || $error_last_name || $error_user_name || $error_email || $error_password || $error_state) {

      showForm($error_first_name,$error_last_name,$error_user_name,$error_email,$error_password,$error_state);

      } else {

      echo 'Submission was success!';

      }

       

      }

      ?> 
 

</div>

</div>
 

<?php include("end.php"); ?>

Open in new window

0
Comment
Question by:jbpeake
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Opalelement
ID: 24769910
     // Display first name field an error if needed
      echo '<label for="first_name">First Name:</label><input type="text" name="first_name" value="'.$first_name.'">';
      if ($error_first_name) echo " * $error_text_first_name";
      echo '<br>';

Is that part of your code filling in the first name correctly when there are errors?
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24769974
Hi,

You can validate client-side scripting before you post so when you do post, everything will work and you do not waste an expensive trip to the server.

See the tutorial below:

http://studge.com/javascript-validation-for-html-forms/

Regards
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24769979
0
 
LVL 2

Accepted Solution

by:
dinu_sorin earned 500 total points
ID: 24770905
Easy one. Those variables wasn't declared in function! You must just declare them as global:

After this row:
 // Function to display form
      function showForm($error_first_name=false,$error_last_name=false,$error_user_name=false,$error_email=false,$error_password=false,$error_state=false){

put the follow row:
global $first_name,$last_name,$user_name,$password,$email,$state;
0
 
LVL 2

Expert Comment

by:dinu_sorin
ID: 24770920
PS: You can't use client-side validation without a server-side validation. Users can easily disable javascript from browser and skip the validation.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now