Help with PHP form needed

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
PHP Wannabe
Forum Newbie
Posts: 2
Joined: Mon Jun 30, 2003 4:07 pm

Help with PHP form needed

Post by PHP Wannabe »

I have this PHP form. The validation itself works but I need the form to repoplulate when a user missed a required field (radio buttons and checkboxes) Also a summary of the inputs should show up without the form being visible again after they sumbit, and the users favourite color as the background. I'm kind of new at this so the code might not be as clean as many pro's here write. Be easy on me. If possible, please fill in any missing code that I should use to make this work. I ran out of ideas.

Code: Select all

<html> 
<head> 
<title>PHP Form</title> 
</head> 

<body bgcolor="#000000" alink="#000000" vlink="#0000ff"> 
<table border="1" align="center" bgcolor="#ccccff" cellpadding="5"> 
<tr> 
<td> 

<h4><font color="#000000">PHP Form</font></h4> 
<hr width="600"> 

<?php 

//session_start(); 
if ($REQUEST_METHOD == "POST") { 

$errors = validate_data($f_name, $l_name, $age, $phone, $mail_list); 
if ($errors) { 
for ($i=0; $i < count($errors); $i++) 
print $errors[$i]; 
}else{ 
print thank_user(); 
} 

} 

?> 

<form action="<?= $PHP_SELF ?>" method="post" name="output"> 

<?php 

function validate_data($f_name, $l_name, $age, $phone, $mail_list) { 
if ($f_name == "") { 
$errors[]="<font color="#ff0000">You did not enter your first name.</font><br>"; 
} 

if ($l_name == "") { 
$errors[]="<font color="#ff0000">You did not enter your last name.</font><br>"; 
} 

if ($age == "") { 
$errors[]="<font color="#ff0000">You did not enter your age.</font><br>"; 
} 
elseif (!is_numeric($age)) { 
$errors[]="<font color="#ff0000">Age must be numeric.</font><br>"; 
} 
elseif ($age < 18 || $age > 110) { 
$errors[]="<font color="#ff0000">Age entered must be between 18 and 110.</font><br>"; 
} 

$len = strlen($phone); 
if ($len < 11) { 
$errors[]="<font color="#ff0000">You must enter a phone number with at least 11 characters.</font><br>"; 
} 

if ($mail_list == "Yes" || $mail_list == "No") { 
}else{ 
$errors[]="<font color="#ff0000">You must specify if you would like to be contaced by e-mail.</font><br>"; 
} 

return $errors; 

} //Close validate_data block. 

function thank_user() { 
print ("<h3><center>Thank you for visiting our site $title $l_name.</center></h3>"); 
print ("<h4>Here is a summary of the information you provided:</h4>"); 
print ("Your name: $f_name $l_name<br>"); 
print ("Age: $age<br>"); 
print ("Gender: $gender<br>"); 
print ("Address: $address<br>"); 
print ("Province: $province<br>"); 
print ("Phone Number: $phone<br>"); 
print ("E-mail: $email<br>"); 
print ("Time we can contact you: $contact<br>"); 
print ("Are you on our mailing list? $mail_list<br>"); 
print ("You rated our site as: $rate<br>"); 
print ("Your comments were: $comments"); 

} //Close thank_user block. 

include ("header.php"); //Header file. 


//function display_form() { //Open display_form function. 

print ("<table border="0" cellpadding="3">"); 

print ("<tr><td colspan="2">Fields with a <font color="#ff0000">*</font> are required.</td></tr>"); 

print ("<tr>"); 
print ("<td align="right">"); 
print ("Title:"); 
print ("</td>"); 
print ("<td> "); 
print ("<input type="radio" name="title" value="Mr.">Mr."); 
print ("<input type="radio" name="title" value="Mrs.">Mrs."); 
print ("<input type="radio" name="title" value="Miss">Miss"); 
print ("<input type="radio" name="title" value="Ms">Ms"); 
print ("<input type="radio" name="title" value="Dr.">Dr."); 
print ("</td>"); 
print ("</tr>"); 
  
print ("<tr>"); 
print ("<td align="right">"); 
print ("<font color="#ff0000">* </font>First Name:"); 
print ("<td colspan="2">"); 
print ("<input type="text" name="f_name" maxlength="19" size="20" value='$f_name'>"); 
print ("</td>"); 
print ("</td>"); 
print ("</tr>"); 
  
print ("<tr>"); 
print ("<td align="right">"); 
print ("<font color="#ff0000">* </font>Last Name:"); 
print ("<td colspan="2">"); 
print ("<input type="text" name="l_name" maxlength="19" size="20" value='$l_name'>"); 
print ("</td>"); 
print ("</td>"); 
print ("</tr>"); 
  
print ("<tr>"); 
print ("<td align="right">"); 
print ("<font color="#ff0000">* </font>Age:"); 
print ("<td colspan="2">"); 
print ("<input type="text" name="age" maxlength="3" size="5" value='$age'>"); 
print ("</td>"); 
print ("</td>"); 
print ("</tr>"); 
  
print ("<tr>"); 
print ("<td align="right">"); 
print ("Gender:"); 
print ("<td colspan="2">"); 
print ("<input type="radio" name="gender" value="Male">Male"); 
print ("<input type="radio" name="gender" value="Female">Female"); 
print ("</td>"); 
print ("</td>"); 
print ("</tr>"); 

print ("<tr> 
    <td align="right"> 
Address: 
<td colspan="2"> 
<input type="text" name="address" maxlength="19" size="20" value='$address'> 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td align="right"> 
Province: 
<td colspan="2"> 
<input type="text" name="province" maxlength="19" size="20" value='$province'> 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td align="right"> 
<font color="#ff0000">* </font>Telephone Number: 
<td colspan="2"> 
<input type="text" name="phone" maxlength="19" size="20" value='$phone'> 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td align="right"> 
E-mail Address: 
<td colspan="2"> 
<input type="text" name="email" size="20" value='$email'> 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td align="right"> 
When can we contact you? 
<td> 
<input type="checkbox" name="contact" value="Morning">Morning 
<input type="checkbox" name="contact" value="Afternoon">Afternoon 
<input type="checkbox" name="contact" value="Evening">Evening 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td align="right"> 
<font color="#ff0000">* </font>Would you like to be on our mailing list? 
<td colspan="2"> 
<input type="radio" name="mail_list" value="Yes">Yes 
<input type="radio" name="mail_list" value="No">No 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td align="right"> 
What is your favourite color? 
<td colspan="2"> 
<select name="color" size="1"> 
<option>Pick a color...</option> 
<option>--------------------</option> 
<option>Red</option> 
<option>Green</option> 
<option>Blue</option> 
<option>Orange</option> 
<option>Pink</option> 
<option>Yellow</option> 
</select> 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td align="right"> 
How would you rate our site? 
<td colspan="2"> 
<input type="radio" name="rate" value="Excellent">Excellent 
<input type="radio" name="rate" value="Good">Good 
<input type="radio" name="rate" value="Needs Work">Needs Work 
</td> 
</td> 
  </tr>"); 
  
print ("<tr> 
    <td colspan="2"> 
  
</td> 
  </tr>"); 
  
print ("<tr> 
  <td colspan="2" align="left"> 
  Please enter any comments here:<br> 
  <textarea rows="5" cols="50" name="comments"></textarea> 
  </td> 
  </tr>"); 
  
print ("<tr> 
    <td colspan="2" align="center"> 
      <input type="submit" value="Submit Form"> <input type="reset" value="Clear Form"> 
    </td> 
  </tr>"); 

print ("</table>"); 

//} //Closing display_form function block. 

//display_form(); //This calls on the display_form function. 


include ("footer.php"); //Footer file. 

?> 

</form> 

</td> 
</tr> 
</table> 
</body> 

</html>
mod_edit:

Code: Select all

replaced by

Code: Select all

[/size]
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Re: Help with PHP form needed

Post by m3rajk »

first off: it's good to get in the habit of having all the php that you can have come before html, come before html. this way you avoid "headers already sent"

another thing is that when i first posted on here, i was told of a better way to write large chhunks of html. i'll show you that.

i modified your code dramatically. i purposely left out a bunch of it because i don't want to give give you code and rob you of a learning experience. i just set it up a way i think will help you get on track and learn something i learned a when i got here (i came here looking for form advice too... now i'm looking for why php isn't getting one thing from html... no one seems to be answering that thread...)

you'll note i didn't even include most of what you had, just created an archetype.. a template or shell for you to fill in, and gave a few variables and such as an example

template:

Code: Select all

&lt;?php

# set the variables

# variables for errors
$haserrors=FALSE; $errors=array(); $errormessages();

# check for errors
if($REQUEST_METHOD=="POST"){
 /* do checks */
 /* if it fails, set $haserrors to TRUE and add error message to $errormessages and bad variables to $errors (note, to add to an array, use $array&#1111;]=$new_value note2: this is always a push) */
}

if($REQUEST_METHOD=="POST"){
  if($haserrors){
    pagehead(); showerrors($errors, $errormessages); pagefooter();
  }else{
    pagehead(); thankuser(); pagefooter();
  }
}else{
  pagehead(); displayform(); pagefooter();
}

function pagehead(){
  ?&gt;
&lt;!-- place opening of page here --&gt;
  &lt;?php
}

function pagefooter(){
  ?&gt;
&lt;!-- place ending of page here --&gt;
  &lt;?php
}

function thankuser(){
  ?&gt;
&lt;!-- place thanking page here --&gt;
  &lt;?php
}

function displayform(){
  ?&gt;
&lt;!-- place form's first display here here --&gt;
  &lt;?php
}

function showerrors($errors, $errormessages){
  ?&gt;
&lt;!-- place display with errors here --&gt;
  &lt;?php
}

?&gt;
example:

Code: Select all

<?php

# set the variables
$name=$_POST['name']; $eye_color=$_POST['eye_color'];

# variables for errors
$haserrors=FALSE; $errors=array(); $errormessages();

# check for errors
if($REQUEST_METHOD=="POST"){
 /* do checks */
 /* if it fails, set $haserrors to TRUE and add error message to $errormessages and bad variables to $errors (note, to add to an array, use $array[]=$new_value note2: this is always a push) */

  if(!(isset($eye_color))){
    $haserrors=TRUE; $errors[]=$eye_color; $errormessages[]='you forgot to enter your eye color!';
  }

  if(!(isset($name))){
    $haserrors=TRUE; $errors[]=$name; $errormessages[]='you forgot to enter your name!';
  }
}

if($REQUEST_METHOD=="POST"){
  if($haserrors){
    pagehead(); showerrors($errors, $errormessages); pagefooter();
  }else{
    pagehead(); thankuser(); pagefooter();
  }
}else{
  pagehead(); displayform(); pagefooter();
}

function pagehead(){
  ?>
<!-- place opening of page here -->
  <?php
}

function pagefooter(){
  ?>
<!-- place ending of page here -->
  <?php
}

function thankuser(){
  ?>
<!-- place thanking page here -->
  <?php
}

function displayform(){
  ?>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST">
<table>
  <tr>
    <td>your name:</td>
    <td><input type="text" name="name" size="20"></td>
  </tr>
  <tr>
    <td>your eye color:</td>
    <td><input type="checkbox" name="eye_color" value="blue">blue<input type="checkbox" name="eye_color" value="brown">brown</td>
  </tr>
  <tr>
    <td><input type="submit"></td>
    <td><input type="reset"></td>
  </tr>
</table>
</form>
  <?php
}

function showerrors($errors, $errormessages){
  ?>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST">
<table>
  <tr>
    <td>your name:</td>
    <td><input type="text" name="name" size="20" <?php if(isset($name)){ echo 'value="'.$name.'"'; ?>></td>
  </tr>
  <tr>
    <td>your eye color:</td>
    <td><input type="checkbox" name="eye_color" value="blue" <?php if(isset($eye_color)&&($eye_color=='blue')){ echo 'checked'; ?>>blue<input type="checkbox" name="eye_color" value="brown" <?php if(isset($eye_color)&&$eye_color=='brown'){ echo 'checked'; ?>>brown</td>
  </tr>
  <tr>
    <td><input type="submit"></td>
    <td><input type="reset"></td>
  </tr>
</table>
</form>
  <?php
}

?>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i forgot to have it proint out error messages... just add

Code: Select all

foreach ($errormessages as $errormessage){ echo "<h1>$errormessage</h1>";} to the front of the error display and it'll do that
PHP Wannabe
Forum Newbie
Posts: 2
Joined: Mon Jun 30, 2003 4:07 pm

Post by PHP Wannabe »

Thank you for taking the time a showing me the right way. It's much easier to learn if you actually see the code. :D

Thanks again.
Post Reply