Page 1 of 1

PHP FORM HELP!!!

Posted: Sat Apr 03, 2004 9:50 pm
by coryp
I'm going to let you all know right off hand that I have practically no experience with php, and that I'm stuck working on this script because our programmer is off for a couple weeks. I tried adding the "required fields" snippets of code, but when I submit the form it lists all the errors.

Example:

Your form had the following errors : Sorry, you must enter a name.
Sorry, you must enter your email address.
Sorry, you must enter your experience.
Sorry, you must advise wether your a client of ours or not.
Sorry, you must advise wether you have been employed with us or not.
Sorry, you must advise how you wish to be paid.
Sorry, you must advise how you plan on selling.
Sorry, you must advise wether you have operated or been employed by another web hosting company or not.
Please go back and try again.

Here is the code

Code: Select all

<?php
  
  error_reporting(E_ALL); 
    
if ($_SERVER['REQUEST_METHOD'] == "POST") 
    { extract($_POST); } else { extract($_GET); } 

/* extract() takes the name=" " of your form fields and makes them into variables. So if you had <input type="text" name="fname"> -- you now have a variable called $fname that holds the value (if any) that the user entered into that text box. 

After this step, you can add your custom checking routines (what fields are required, etc.) Here I'm working with a simple form, that must have name city*/ 

$Full_Name = isset($Full_Name) ? $Full_Name : NULL; 
$Email_Address = isset($Email_Address) ? $Email_Address : NULL; 
$Experience = isset($Experience) ? $Experience : NULL;
$CPQIS_Client = isset($CPQIS_Client) ? $CPQIS_Client : NULL;
$Employed_by_CPQIS = isset($Employed_by_CPQIS) ? $Employed_by_CPQIS : NULL;
$How_do_you_wish_to_be_payed = isset($How_do_you_wish_to_be_payed) ? $How_do_you_wish_to_be_payed : NULL;
$How_do_you_plan_on_selling = isset($How_do_you_plan_on_selling) ? $How_do_you_plan_on_selling : NULL;
$Have_you_ever_operated_or_been_employed_by_another_web_hosting_company = isset($Have_you_ever_operated_or_been_employed_by_another_web_hosting_company) ? $Have_you_ever_operated_or_been_employed_by_another_web_hosting_company : NULL;

$errors = ""; //holds any errors 

if ($Full_Name == NULL) 
{ 
   $errors .= "Sorry, you must enter a name.<br />"; 
} 

if ($Email_Address == NULL) 
{ 
   $errors .= "Sorry, you must enter your email address.<br />"; 
} 

if ($Experience == NULL) 
{ 
   $errors .= "Sorry, you must enter your experience.<br />"; 
} 

if ($CPQIS_Client == NULL) 
{ 
   $errors .= "Sorry, you must advise wether your a client of ours or not.<br />"; 
} 

if ($Employed_by_CPQIS == NULL) 
{ 
   $errors .= "Sorry, you must advise wether you have been employed with us or not.<br />"; 
} 

if ($How_do_you_wish_to_be_payed == NULL) 
{ 
   $errors .= "Sorry, you must advise how you wish to be paid.<br />"; 
} 

if ($How_do_you_plan_on_selling == NULL) 
{ 
   $errors .= "Sorry, you must advise how you plan on selling.<br />"; 
} 

if ($Have_you_ever_operated_or_been_employed_by_another_web_hosting_company == NULL) 
{ 
   $errors .= "Sorry, you must advise wether you have operated or been employed by another web hosting company or not.<br />"; 
} 

if ($errors != "") 
{ 
   echo "Your form had the following errors : "; 
   echo $errors; 
   echo "Please go back and try again."; 
   die(); 
} 

$subject = "Free Ded. Server Contest Signup " . $_POST['Email Address']; 
$message = "Information Posted:\n\n Full Name: " . $_POST['Full_Name'] . "\nEmail Address: " . $_POST['Email_Address'] . "\nExperience: " . $_POST['Experience'] . "\nHTML: " . $_POST['HTML'] . "\nPHP: " . $_POST['PHP'] . "\nPERL: " . $_POST['PERL'] . "\nNone: " . $_POST['None'] . "\nCPQIS Client?: " . $_POST['CPQIS_Client'] . "\nClient ID: " . $_POST['Client_ID'] . "\nEmployed by CPQIS?: " . $_POST['Employed_by_CPQIS'] . "\nPosition: " . $_POST['Position'] . "\nWebsite: " . $_POST['Website'] . "\nHow do you wish to be payed?: " . $_POST['How_do_you_wish_to_be_payed'] . "\nMailing Addy: " . $_POST['Mailing_Addy'] . "\nPayPal Addy: " . $_POST['PayPal_Addy'] . "\nHow do you plan on selling?: " . $_POST['How_do_you_plan_on_selling'] . "\nHave you ever operated or been employed by another web hosting company?: " . $_POST['Have_you_ever_operated_or_been_employed_by_another_web_hosting_company'] . "\nCompany URL: " . $_POST['Company_URL'] . "\nComments: " . $_POST['Comments']; 

mail ("coryp@cpqservices.com", $subject, $message); 

?>
  <span class="style11">Thank you for your registration. We will get back to you as soon as we review your application. </span></span>
                
              </p>
<form method="POST" action="<?php echo $PHP_SELF; ?>"> 
  <p align="center" class="style7 style14">Your Information</p>
  
  <div align="center" class="style15">
    <table width="44%" border="2" cellspacing="0" cellpadding="1">
      <tr>
        <td width="259"><div align="left"><span class="style13"><span class="style9">*</span><span class="style3">Full Name</span></span></div></td>
        <td width="259"><input name="Full Name" type="text" id="Full Name"></td>
      </tr>
      <tr>
        <td><div align="left"><span class="style9">*</span><span class="style13">Email Address </span></div></td>
        <td><input name="Email Address" type="text" id="Email Address"></td>
      </tr>
      <tr>
        <td><div align="left"><span class="style9">*</span><span class="style13">Experience</span></div></td>
        <td>          <textarea name="Experience" id="Experience">I have experience in...</textarea></td>
      </tr>
      <tr>
        <td><div align="left"><span class="style9">*</span><span class="style13">I am familiar with </span></div></td>
        <td><input name="HTML" type="checkbox" id="HTML" value="checkbox">
          HTML
            <input name="PHP" type="checkbox" id="PHP" value="checkbox"> 
          PHP
          <input name="PERL" type="checkbox" id="PERL" value="checkbox"> 
          PERL
          <input name="None" type="checkbox" id="None" value="checkbox">
          None</td>
      </tr>
      <tr>
        <td><div align="left"><span class="style3"><span class="style13"><span class="style9">*</span>Are you a CPQIS client? </span></span></div></td>
        <td>          <select name="CPQIS Client" id="CPQIS Client">
          <option>Yes</option>
          <option>No</option>
        </select>
If yes         <input name="Client ID" type="text" id="Client ID" value="Client ID" size="15"> 
<span class="style17"><a href="javascript:popUp()">-What's this?</a></span> </td>
      </tr>
      <tr>
        <td><div align="left"><span class="style9">*</span><span class="style13">Are you or have you been employed by CPQIS? </span></div></td>
        <td><select name="Employed by CPQIS" id="Employed by CPQIS">
          <option>Yes</option>
          <option>No</option>
        </select> 
          If yes 
          <input name="Position" type="text" id="Position" value="Position"></td>
      </tr>
      <tr>
        <td><div align="left" class="style3"><strong>Website</strong></div></td>
        <td><input name="Website" type="text" id="Website" value="http://"></td>
      </tr>
    </table>
    <p class="style3"><strong>Payment Options</strong></p>
    <table width="44%" border="2" cellpadding="1" cellspacing="0">
      <tr>
        <td width="259"><div align="left"><span class="style13"><span class="style9">*</span><span class="style3">How do you wish to be payed for orders over the required? </span></span></div></td>
        <td width="259"><p>
            <select name="How do you wish to be payed" id="How do you wish to be payed">
              <option>PayPal</option>
              <option>Mail</option>
            </select> 
            <span class="style3">NOTE: You will be payed quarterly commissions for each order over the required. </span></p>
          </td>
      </tr>
      <tr>
        <td><div align="left"><span class="style13">Mailing Address </span></div></td>
        <td><textarea name="Mailing Addy" id="Mailing Addy"></textarea></td>
      </tr>
      <tr>
        <td><div align="left"><span class="style13">PayPal Address </span></div></td>
        <td>          <input name="PayPal Addy" type="text" id="PayPal Addy"></td>
      </tr>
    </table>
    <p class="style3"><strong>Other</strong></p>
    <table width="44%" border="2" cellpadding="1" cellspacing="0">
      <tr>
        <td width="259"><div align="left"><span class="style13"><span class="style9">*</span><span class="style3">How do you plan on selling? </span></span></div></td>
        <td width="259"><p>
          <textarea name="How do you plan on selling" id="How do you plan on selling"></textarea>
</p></td>
      </tr>
      <tr>
        <td><div align="left"><span class="style9">*</span><span class="style13">Have you ever operated or been employed by another web hosting company? </span></div></td>
        <td><select name="Have you ever operated or been employed by another web hosting company" id="Have you ever operated or been employed by another web hosting company">
          <option>Yes</option>
          <option>No</option>
        </select> 
          If yes 
          <input name="Company URL" type="text" id="Company URL" value="Company URL" size="15"></td>
      </tr>
      <tr>
        <td><div align="left" class="style3"><strong>Comments</strong></div></td>
        <td>          <textarea name="Comments" id="Comments"></textarea></td>
      </tr>
    </table>
    </div>
  <p align="center">
    <span class="style12">
    <input type="submit" value="SIGNUP!" />
  </span>  </p>
</form> 
<? 
if (!mail($Full_Name,$Email_Address,$Experience,$CPQIS_Client,$Employed_by_CPQIS,$How_do_you_wish_to_be_payed,$How_do_you_plan_on_selling,$Have_you_ever_operated_or_been_employed_by_another_web_hosting_company)) 
{ 
    die("There was an error sending your email"); 
} 
 

?>
Now I know my "variables" are a little long, but I need it that way for specific reasons. .....What am I doing wrong?

A bit imroved version

Posted: Wed Apr 07, 2004 11:57 am
by wwwapu
Hello
Here is a bit better version of your code. I got it working, but there is still something to improve or consider:
1. If there is some information missing, the form is reseted and all fields are empty. (One might define variables for each field and set them empty at first and write it to form)
2. For a user it is disturbing to be "required" some information. If user really wants to signup he/she will fill the form completely.

Some notes about your website:
1. Why Cory P.? Why not the whole name? Somebody might think there is something to hide. Without plainly identifying oneself one loses some credibility in the eyes of users.
2. Victor S. should consider defining margin-right, margin-left and margin.bottom in CSS for body element. The pages would be easier to read and it would not seem so "stuffed".

And now the code:

Code: Select all

<?php
<?php

/* First I've set $thank_you as empty String. this will be changed and printed to browser if the form is successfully mailed.*/
$thank_you="";
/* If the form has not been sent (which means the page has been loaded the first time) no variables will be set .
But when the form is sent $_POST is extracted */
if(!empty($_POST)){
	extract($_POST);

	
/* extract() takes the name=" " of your form fields and makes them into variables. So if you had <input type="text" name="fname"> -- you now have a variable called $fname that holds the value (if any) that the user entered into that text box. 
*/


$Full_Name = isset($Full_Name) ? $Full_Name : ""; 
$Email_Address = isset($Email_Address) ? $Email_Address : ""; 
$Experience = isset($Experience) ? $Experience : ""; 
$CPQIS_Client = isset($CPQIS_Client) ? $CPQIS_Client : ""; 
$Employed_by_CPQIS = isset($Employed_by_CPQIS) ? $Employed_by_CPQIS : ""; 
$How_do_you_wish_to_be_payed = isset($How_do_you_wish_to_be_payed) ? $How_do_you_wish_to_be_payed : ""; 
$How_do_you_plan_on_selling = isset($How_do_you_plan_on_selling) ? $How_do_you_plan_on_selling : ""; 
$Have_you_ever_operated_or_been_employed_by_another_web_hosting_company = isset($Have_you_ever_operated_or_been_employed_by_another_web_hosting_company) ? $Have_you_ever_operated_or_been_employed_by_another_web_hosting_company : ""; 

$errors = ""; //holds any errors 

if ($Full_Name == ""){ 
   $errors .= "Sorry, you must enter a name.<br />"; 
} 

if ($Email_Address == ""){ 
   $errors .= "Sorry, you must enter your email address.<br />"; 
} 

if ($Experience == ""){ 
   $errors .= "Sorry, you must enter your experience.<br />"; 
} 

if ($CPQIS_Client == ""){ 
   $errors .= "Sorry, you must advise wether your a client of ours or not.<br />"; 
} 

if ($Employed_by_CPQIS == ""){ 
   $errors .= "Sorry, you must advise wether you have been employed with us or not.<br />"; 
} 

if ($How_do_you_wish_to_be_payed == ""){ 
   $errors .= "Sorry, you must advise how you wish to be paid.<br />"; 
} 

if ($How_do_you_plan_on_selling == ""){ 
   $errors .= "Sorry, you must advise how you plan on selling.<br />"; 
} 

if ($Have_you_ever_operated_or_been_employed_by_another_web_hosting_company == ""){ 
   $errors .= "Sorry, you must advise wether you have operated or been employed by another web hosting company or not.<br />"; 
} 

if ($errors != ""){ 
	echo "Your form had the following errors : "; 
	echo $errors;
	echo "Please go back and try again.";
}else{

/* This is changed. Now the experience in languages. is brougth to one variable and then added to $message */
	$html =!empty($html) ? $html : "no";
	$php = !empty($php) ? $php : "no";
	$perl = !empty($perl) ? $perl: "no";
	$languages= "\nHTML: $html \nPHP: $php \nPerl: $perl";
	$none= !empty($none) ? $languages="No experience whatsoever" : "";
	$subject = "Free Ded. Server Contest Signup   $Email_Address";
	$message = "Information Posted:\n\nFull Name:  $Full_Name \nEmail Address:  $Email_Address \nExperience:  $Experience \nLanguages: $languages \nCPQIS Client?:  $CPQIS_Client \nClient ID:  $Client_ID \nEmployed by CPQIS?: $Employed_by_CPQIS . \nPosition:  $Position \nWebsite: $Website \nHow do you wish to be payed?:  $How_do_you_wish_to_be_payed \nMailing Addy: $Mailing_Addy \nPayPal Addy:  $PayPal_Addy \nHow do you plan on selling?:  $How_do_you_plan_on_selling \nHave you ever operated or been employed by another web hosting company?: $Have_you_ever_operated_or_been_employed_by_another_web_hosting_company \nCompany URL:  $Company_URL \nComments: $Comments"; 

	mail ("coryp@cpqservices.com", $subject, $message)or die("There was an error sending your email");
	/* Below is $thank_you that was defined already in the beginning of the code. Here it is changed to thank you message */
	$thank_you = "Thank you for your registration. We will get back to you as soon as we review your application.";
}
} //if(!empty($_POST)) ends here

?>
<!-- $thank_you is displayed here. At the first time it is empty, but after successfull form sending it is not. -->
  <span class="style11"><?php print ("$thank_you")?></span></span> 
                
              </p>
<!-- The form is comletely revised. I changed the select elements into radio buttons because select had value
yes when form was sent. So the checking and error messaging of it was useless. Radio has value only if it is selected.
There is also some other changes. Check this CAREFULLY before using.-->
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">

<p align="center" class="style7 style14">Your Information</p> 
  
<div align="center" class="style15"> 
	<table width="44%" border="2" cellspacing="0" cellpadding="1"> 
		<tr> 
        	<td width="259"><div align="left"><span class="style13"><span class="style9">*</span><span class="style3">Full Name</span></span></div> 
        	<td width="259"><input name="Full_Name" type="text" id="Full Name"> 
      	</tr> 
      	<tr> 
        	<td><div align="left"><span class="style9">*</span><span class="style13">Email Address </span></div> 
        	<td><input name="Email Address" type="text" id="Email Address"> 
      	</tr> 
      	<tr> 
        	<td><div align="left"><span class="style9">*</span><span class="style13">Experience</span></div> 
        	<td><textarea name="Experience" id="Experience"></textarea> 
      	</tr> 
      	<tr> 
        	<td><div align="left"><span class="style9">*</span><span class="style13">I am familiar with</span></div> 
        	<td><input name="html" type="checkbox" id="HTML" value="yes"> 
          	HTML 
            <input name="php" type="checkbox" id="PHP" value="yes"> 
          	PHP 
          	<input name="perl" type="checkbox" id="PERL" value="yes"> 
          	PERL 
          	<input name="none" type="checkbox" id="None" value="none"> 
          	None 
		</tr> 
		<tr> 
        	<td><div align="left"><span class="style3"><span class="style13"><span class="style9">*</span>Are you a CPQIS client? </span></span></div> 
        	<td>
          	<input type="radio" name="CPQIS_Client" value="Yes">Yes
          	<input type="radio" name="CPQIS_Client" value="No">No
          	<br>If yes
          	<input name="Client_ID" type="text" id="Client ID" value="Client ID" size="15">
          	<br>
          	<span class="style17"><a href="javascript:popUp()">-What's this?</a></span>  
		</tr> 
      	<tr> 
        	<td><div align="left"><span class="style9">*</span><span class="style13">Are you or have you been employed by CPQIS? </span></div> 
        	<td>
        		<input type="radio" name="Employed_by_CPQIS" value="Yes">Yes
        		<input type="radio" name="Employed_by_CPQIS" value="No">No
        		<br>If yes 
          		<input name="Position" type="text" id="Position" value="Position"> 
      	</tr> 
      	<tr> 
        	<td><div align="left" class="style3"><strong>Website</strong></div> 
        	<td><input name="Website" type="text" id="Website" value="http://"> 
      	</tr> 
	</table> 
    <p class="style3"><strong>Payment Options</strong></p> 
	<table width="44%" border="2" cellpadding="1" cellspacing="0"> 
    	<tr>
    		<td width="259"><div align="left"><span class="style13"><span class="style9">*</span><span class="style3">How do you wish to be payed for orders over the required? </span></span></div>
    		<td width="259"><p>
    			<input name="How_do_you_wish_to_be_payed" type="radio" value="PayPal">PayPal
    			<input name="How_do_you_wish_to_be_payed" type="radio" value="Mail">Mail
    			<span class="style3">NOTE: You will be payed quarterly commissions for each order over the required. </span></p> 
		</tr> 
		<tr> 
			<td><div align="left"><span class="style13">Mailing Address </span></div> 
			<td><textarea name="Mailing_Addy" id="Mailing Addy"></textarea> 
		</tr> 
		<tr> 
			<td><div align="left"><span class="style13">PayPal Address </span></div> 
			<td><input name="PayPal_Addy" type="text" id="PayPal Addy"> 
		</tr> 
	</table> 
	<p class="style3"><strong>Other</strong></p> 
	<table width="44%" border="2" cellpadding="1" cellspacing="0"> 
		<tr> 
			<td width="259"><div align="left"><span class="style13"><span class="style9">*</span><span class="style3">How do you plan on selling? </span></span></div> 
			<td width="259"><p> 
			<textarea name="How_do_you_plan_on_selling" id="How do you plan on selling"></textarea> 
			</p> 
		</tr> 
		<tr> 
		<td><div align="left"><span class="style9">*</span><span class="style13">Have you ever operated or been employed by another web hosting company? </span></div> 
		<td><input type="radio" name="Have_you_ever_operated_or_been_employed_by_another_web_hosting_company" id="Have you ever operated or been employed by another web hosting company" value="Yes">Yes
		<input type="radio" name="Have_you_ever_operated_or_been_employed_by_another_web_hosting_company" id="Have you ever operated or been employed by another web hosting company" value="No">No
		<br>If yes 
		<input name="Company URL" type="text" id="Company URL" value="Company URL" size="15"> 
	</tr> 
	<tr> 
		<td><div align="left" class="style3"><strong>Comments</strong></div> 
		<td><textarea name="Comments" id="Comments"></textarea> 
	</tr>
</table> 
</div> 
<p align="center"> 
<span class="style12"> 
<input type="submit" value="SIGNUP!" /> 
</span>  </p> 
</form> 


?>