Jump to content

[SOLVED] Form validation using same page - no variables passed!


Recommended Posts

Hello,

 

i'm trying to put together a form to insert records into a database. I have had the form working fine but i want to add some validation after several users missed out important information using the page.

 

I have a page set up with an if statement choosing one of two pages depending on whether a hidden field (on the validated page) has been set. The problem I have is when the form passes all validation and redirects to the insert script (to insert the input values from the validated form) page no data gets inserted. As I have the two html pages on the one php page it's almost like it's inserting the first unfilled page rather than the validated one.

 

Is this what's going on?

 

Can anyone shed some light on this?

 

Here is my code

 

Regards

 

James

 

<?php
// Start the session

session_start();

// Check to see if the session has been set already - of not redirect to login page

if(!isset($_SESSION['Login']))

header ("location:login.php");

// Include neccessary set up files

include ('../config.php');
include ('../opendb.php');

// LIST OF ERROR VARIABLES.................

$error_companyname = '<span class="form_error">*ERROR* - No company name</span>';
$error_companyname_taken = '<span class="form_error">*ERROR* - Company name already exists!</span>';
$error_client_type = '<span class="form_error">*ERROR* - No client type</span>';
$error_sales_contact = '<span class="form_error">*ERROR* - No sales contact</span>';
$error_companyname = '<span class="form_error">*ERROR* - No company name</span>';
$error_booking_type = '<span class="form_error">*ERROR* - No booking type</span>';
$error_pre_payment = '<span class="form_error">*ERROR* - No pre-payment</span>';

// Define the company name variable
$company_name = $_POST['companyname'];

// Make the query to find all results with the company name entered
$company_name_query = "SELECT companyname FROM contacts WHERE companyname = '$company_name'";

$company_name_result = mysql_query($company_name_query);

if (!$company_name_result) { echo 'Query Error: ' . mysql_error(); }


// If all neccessary fields are filled in....

if (isset($_POST['form_set']) && (mysql_num_rows($company_name_result) < 1) && (!empty($_POST['clienttype'])) && (!empty($_POST['companyname'])) && (!empty($_POST['sales_contact'])) && (!empty($_POST['client_lead'])) && (!empty($_POST['pre_payment']))) {

header ("location: check.php");

}

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

?> 

<!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=ISO-8859-1" />
<title>Customer Management - Enter a new record</title>
<link rel="stylesheet" type="text/css" href="styles/database.css"/>
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<script language="javascript" type="text/javascript" src="date_picker/datetimepicker.js"></script>
</head>
<body>

<div id="wrapper">

<div id="header"></div>

<div id="main_nav">

<?PHP include ("nav/main_nav.php"); ?>

</div>

<div id="main_content">

<h1>Please enter the customer details</h1>

<form action="" method="post" onsubmit="return validate_form(this)">
<input name="form_set" type="hidden" value="true" />

<table width="700" border="0" cellpadding="0" cellspacing="0" id="data_container">
	<tr>
	<td width="600" valign="top"><fieldset><legend>Company Details</legend>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" class="single_line_table">
	<tr>
		<td width="50%" align="right" valign="middle">
			<p class="form_details"><strong>Company Name:</strong>
			<input name="companyname" type="text" id="companyname" tabindex="1" />
			</p>
		</td>
		<td width="50%" align="right" valign="middle">
			<p class="form_details"><strong>Booking Type:</strong>
				<select name="client_lead" id="client_lead" tabindex="5" >
					<option value="" selected="selected">Please choose...</option>
					<option value="">-------------------</option>
					<option value="C">Client</option>
					<option value="L">Lead</option>
				</select>
			</p>			</td>
	</tr>
	<tr>
		<td width="50%" align="right" valign="middle">
			<p class="form_details"><strong>Client Type:</strong>
				<select name="clienttype" id="clienttype" value="">
					<option value="" selected="selected">Please choose...</option>
					<option value="">-------------------</option>
						<?  
						// Populate the Advertiser Type drop down menu
						$sql = mysql_query("SELECT client_type_name FROM client_types");
						$num = mysql_num_rows($sql);
						$i=0;
						while ($i < $num) {
							$item = mysql_result($sql,$i,"client_type_name");
							$i++;
							echo  '<option value="' . $item . '">' . $item . '</option>';    }
						?>
				</select>
			</p>			</td>
		<td width="50%" align="right" valign="middle">
			<p class="form_details"><strong>Pre Payment:</strong>
				<select name="pre_payment" id="pre_payment">
					<option value="" selected="selected">Please choose...</option>
					<option value="">-------------------</option>
					<option value="Y">Yes</option>
					<option value="N">No</option>
				</select>
			</p>			</td>
	</tr>
	<tr>
		<td width="50%" align="right" valign="middle">
			<p class="form_details"><strong>Sales Contact:</strong>
					<select name="sales_contact" id="sales_contact">
							<option value="" selected="selected">Please choose...</option>
							<option value="">------------------</option>
							<?  

// Populate the Section drop down menu

$sql = mysql_query("SELECT * FROM users WHERE sales = 'Y'");

$num = mysql_num_rows($sql);

$i=0;

while ($i < $num) {

$firstname = mysql_result($sql,$i,"firstname");
$lastname = mysql_result($sql,$i,"lastname");

    $i++;

echo  '<option value="' . $firstname . ' ' . $lastname . '">' . $firstname . ' ' . $lastname . '</option>';    }

?>
					</select>
			</p></td>
<td width="50%" align="right" valign="middle"><p class="form_details"><strong>Business Type:</strong>
<input name="businesstype" type="text" id="businesstype" tabindex="2" />
</p></td>
</tr>
<tr>
<td width="50%" align="right" valign="middle">
<p class="form_details"><strong>Advertising In:</strong>
		<select name="advertisingin" id="advertisingin">
				<option value="Wedding Ideas" selected="selected">Wedding Ideas</option>
		</select>
</p></td>
<td width="50%" align="right" valign="middle"><p class="form_details"> </p></td>
</tr>
</table>
      </fieldset>
    <tr>
      <td valign="top"><fieldset><legend>Contact Details</legend>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="single_line_table">
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>(1)  First Name:</strong>
              <input name="contactonefirstname" type="text" id="contactonefirstname"  value="<?=$onefirstname;?>" tabindex="6" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>(2) First Name:</strong>
                <input name="contacttwofirstname" type="text" id="contacttwofirstname" tabindex="9" /></p></td>
            </tr>
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>(1) Last Name:</strong> 
                			<input name="contactonelastname" type="text" id="contactonelastname"  value="<?=$onelastname;?>" tabindex="7" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>(2) Last Name:</strong> 
              				<input name="contacttwolastname" type="text" id="contacttwolastname"  tabindex="10" /></p></td>
            </tr>
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>(1) E-mail:</strong>
                			<input type="text" name="contactoneemail" id="contactoneemail" value="<?=$oneemail;?>" tabindex="8" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>(2) E-mail:</strong>
              				<input type="text" name="contacttwoemail" id="contacttwoemail" tabindex="11" /></p></td>
            </tr>
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Website:</strong>
              <input name="website" type="text" id="website"  /></p></td>
              <td width="50%" align="right" valign="top"> </td>
            </tr>
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Phone:</strong>
              <input name="phone" type="text" id="phone" value="<?=$phone;?>" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>Fax:</strong>
              <input type="text" name="fax" id="fax" /></p></td>
            </tr>
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Mobile:</strong>
              <input type="text" name="mobile" id="mobile" /></p></td>
              <td width="50%" align="right" valign="top"> </td>
            </tr>

            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Address:</strong>
              <input name="address" type="text" id="address" value="<?=$address;?>" /></p></td>
              <td width="50%" align="right" valign="top"> </td>
            </tr>

            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Town/City:</strong>
                <input name="towncity" type="text" id="towncity" value="<?=$towncity;?>" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>County:</strong>
                	<input name="countystate" type="text" id="countystate" value="<?=$countystate;?>" /></p></td>
            </tr>

            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Postcode:</strong>
                	<input name="postcodezip" type="text" id="postcodezip" value="<?=$postcodezip;?>" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>Country:</strong>
                <select name="country" value="<?=$scountry;?>" >
                  <option value="United Kingdom" selected="selected">United Kingdom</option>
                  <option value=""> - - - - - - - - - - </option>
                  <option value="Other">Other</option>
                </select></p></td>
            </tr>
        </table>
          </fieldset></td>
    </tr>

    <tr>
      <td valign="top">
  	<p><fieldset><legend>Notes</legend> 
  	<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td colspan="2" align="center"><textarea name="notes" id="notes" class="notes"></textarea></td>
          </tr>
          <tr>
            <td width="50%" align="right" valign="top"><p class="form_details"><strong>Follow Up Date:</strong>
              <input name="followup" type="text" id="followup" size="25" />
              <a href="javascript:NewCal('followup','yyyyMMdd')"><img src="date_picker/cal.gif" width="16" height="16" border="0" alt="Pick a date" /></a></p></td>
            <td width="50%" align="right" valign="top"> </td>
          </tr>
        </table>
  	</fieldset></p>
        
  </table>

<input name="send" type="submit" id="confirm_button" value="Submit" onsubmit="return validate_form(this)">
</form>

  </div>
  
  <div id="footer"><?php echo SITE_VERSION; ?></div>
   
   </div>
   
   </body>
   
   </html>

<? } else { 


?>

<!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=ISO-8859-1" />
<title>Customer Management - Enter a new record</title>
<link rel="stylesheet" type="text/css" href="styles/database.css"/>
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<script language="javascript" type="text/javascript" src="date_picker/datetimepicker.js"></script>
</head>
<body>

<div id="wrapper">

<div id="header"></div>

<div id="main_nav">

<?PHP include ("nav/main_nav.php"); ?>

</div>

<div id="main_content">

<h1>Please enter the customer details</h1>

<form action="" method="post" onsubmit="return validate_form(this)">
<input name="form_set" type="hidden" value="true" />

<table width="700" border="0" cellpadding="0" cellspacing="0" id="data_container">
	<tr>
	<td width="600" valign="top"><fieldset><legend>Company Details</legend>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" class="single_line_table">
	<tr>
		<td width="50%" align="right" valign="middle">

			<? if (empty($_POST['companyname'])) { echo $error_companyname; } ?>

			<? if (mysql_num_rows($company_name_result) > 0) { echo $error_companyname_taken; } ?>

			<p class="form_details"><strong>Company Name:</strong>
			<input name="companyname" type="text" id="companyname" tabindex="1" value="<? echo stripslashes($_POST['companyname']); ?>" />
			<input type="hidden" name="companyname2" value="<? stripslashes($_POST['companyname']); ?>" />
			</p>			</td>
		<td width="50%" align="right" valign="middle">
			<? if (empty($_POST['client_lead'])) { echo $error_booking_type; } ?><p class="form_details"><strong>Booking Type:</strong>
			<select name="client_lead" id="client_lead">
				<option value="<? echo $_POST['client_lead']; ?>" selected="selected"><? if (empty($_POST['client_lead'])) { echo 'Please choose...'; } else if ($_POST['client_lead'] == 'C') { echo 'Client'; } else if ($_POST['client_lead'] == 'L') { echo 'Lead'; } ?></option>
				<option value="">-------------------</option>
			<? if (empty($_POST['client_type'])) { echo '<option value="C">Client</option><option value="L">Lead</option>'; } else if ($_POST['client_lead'] == 'C') { echo '<option value="L">Lead</option>'; } else if ($_POST['client_lead'] == 'L') { echo '<option value="C">Client</option>'; } ?>
			</select></p>
		</td>
	</tr>
	<tr>
		<td width="50%" align="right" valign="middle">
			<? if (empty($_POST['clienttype'])) { echo $error_client_type; } ?><p class="form_details"><strong>Client Type:</strong>
				<select name="clienttype" id="clienttype" value="">
					<option value="<? if (empty($_POST['clienttype'])) { echo ''; } else { echo $_POST['clienttype']; } ?>" selected="selected"><? if (empty($_POST['clienttype'])) { echo 'Please choose...'; } else { echo $_POST['clienttype']; } ?></option>
					<option value="">-------------------</option>
						<?  
						// Populate the Advertiser Type drop down menu
						$sql = mysql_query("SELECT client_type_name FROM client_types");
						$num = mysql_num_rows($sql);
						$i=0;
						while ($i < $num) {
							$item = mysql_result($sql,$i,"client_type_name");
							$i++;
							echo  '<option value="' . $item . '">' . $item . '</option>';    }
						?>
				</select>
			</p>			</td>
		<td width="50%" align="right" valign="middle">
			<? if (empty($_POST['pre_payment'])) { echo $error_pre_payment; } ?><p class="form_details"><strong>Pre Payment:</strong>
				<select name="pre_payment" id="pre_payment" value="<?=$pre_payment;?>">
					<option value="<? if (empty($_POST['pre_payment'])) { echo ''; } else { echo $_POST['pre_payment']; } ?>" selected="selected"><? if (empty($_POST['pre_payment'])) { echo 'Please choose...'; } else if ($_POST['pre_payment'] == 'Y') { echo 'Yes'; } else if ($_POST['pre_payment'] == 'N') { echo 'No'; } ?></option>
					<option value="">-------------------</option>
					<option value="Y">Yes</option>
					<option value="N">No</option>
				</select>
			</p>			</td>
	</tr>
	<tr>
		<td width="50%" align="right" valign="middle">
			<? if (empty($_POST['sales_contact'])) { echo $error_sales_contact; } ?><p class="form_details"><strong>Sales Contact:</strong>
			<select name="sales_contact" id="sales_contact">
				<option value="<? if (empty($_POST['sales_contact'])) { echo ''; } else { echo $_POST['sales_contact']; } ?>" selected="selected"><? if (empty($_POST['sales_contact'])) { echo 'Please choose...'; } else { echo $_POST['sales_contact']; } ?></option>
				<option value="">------------------</option>
				<?  

// Populate the Section drop down menu

$sql = mysql_query("SELECT * FROM users WHERE sales = 'Y'");

$num = mysql_num_rows($sql);

$i=0;

while ($i < $num) {

$firstname = mysql_result($sql,$i,"firstname");
$lastname = mysql_result($sql,$i,"lastname");

    $i++;

echo  '<option value="' . $firstname . ' ' . $lastname . '">' . $firstname . ' ' . $lastname . '</option>';    }

?>
					</select>
			</p></td>
<td width="50%" align="right" valign="middle"><p class="form_details"><strong>Business Type:</strong>
<input name="businesstype" type="text" id="businesstype" value="<? echo $_POST['businesstype']; ?>" tabindex="2" />
</p></td>
</tr>
<tr>
<td width="50%" align="right" valign="middle">
<p class="form_details"><strong>Advertising In:</strong>
		<select name="advertisingin" id="advertisingin" value="<? echo $_POST['advertisingin']; ?>">
				<option value="Wedding Ideas" selected="selected">Wedding Ideas</option>
		</select>
</p></td>
<td width="50%" align="right" valign="middle"><p class="form_details"> </p></td>
</tr>
</table>
      </fieldset>
    <tr>
      <td valign="top"><fieldset><legend>Contact Details</legend>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="single_line_table">
            <tr>
			<td width="50%" align="right">
		  		<? if (empty($_POST['contactonefirstname'])) { echo $error_contact_one_first_name; } ?><p class="form_details"><strong>(1)  First Name:</strong>
              		<input name="contactonefirstname" type="text" id="contactonefirstname"  value="<? echo $_POST['contactonefirstname']; ?>" tabindex="3" /></p>
			</td>
              	<td width="50%" align="right" valign="top"><p class="form_details"><strong>(2) First Name:</strong>
                	<input name="contacttwofirstname" type="text" id="contacttwofirstname" value="<? echo $_POST['contacttwofirstname']; ?>"tabindex="6" /></p>
			</td>
            </tr>
            <tr>
              	<td width="50%" align="right">
				<? if (empty($_POST['contactonelastname'])) { echo $error_contact_one_last_name; } ?><p class="form_details"><strong>(1) Last Name:</strong> 
                	<input name="contactonelastname" type="text" id="contactonelastname"  value="<? echo $_POST['contactonelastname']; ?>" tabindex="4" /></p>
			</td>
              	<td width="50%" align="right" valign="top">
				<p class="form_details"><strong>(2) Last Name:</strong> 
              		<input name="contacttwolastname" type="text" id="contacttwolastname" value="<? echo $_POST['contacttwolastname']; ?>" tabindex="7" /></p>
			</td>
            </tr>
            <tr>
              	<td width="50%" align="right">
				<? if (empty($_POST['contactoneemail'])) { echo $error_contact_one_email; } ?><p class="form_details"><strong>(1) E-mail:</strong>
                	<input type="text" name="contactoneemail" id="contactoneemail" value="<? echo $_POST['contactoneemail']; ?>" tabindex="5" /></p>
			</td>
              	<td width="50%" align="right" valign="top"><p class="form_details"><strong>(2) E-mail:</strong>
              		<input type="text" name="contacttwoemail" id="contacttwoemail" value="<? echo $_POST['contacttwoemail']; ?>" tabindex="8" /></p></td>
            </tr>
            <tr>
              	<td width="50%" align="right">
		  		<p class="form_details"><strong>Website:</strong>
              		<input name="website" type="text" id="website" value="<? echo $_POST['website']; ?>" tabindex="9" /></p>
			</td>
              	<td width="50%" align="right" valign="top"> </td>
            </tr>
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Phone:</strong>
              <input name="phone" type="text" id="phone" value="<? echo $_POST['phone']; ?>" tabindex="10" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>Fax:</strong>
              <input type="text" name="fax" id="fax" value="<? echo $_POST['fax']; ?>" tabindex="11"/></p></td>
            </tr>
            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Mobile:</strong>
              <input type="text" name="mobile" id="mobile" value="<? echo $_POST['mobile']; ?>" tabindex="11" /></p></td>
              <td width="50%" align="right" valign="top"> </td>
            </tr>

            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Address:</strong>
              <input name="address" type="text" id="address" value="<? echo $_POST['address']; ?>" tabindex="12" /></p></td>
              <td width="50%" align="right" valign="top"> </td>
            </tr>

            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Town/City:</strong>
                <input name="towncity" type="text" id="towncity" value="<? echo $_POST['towncity']; ?>" tabindex="13" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>County:</strong>
                	<input name="countystate" type="text" id="countystate" value="<? echo $_POST['countystate']; ?>" tabindex="14" /></p></td>
            </tr>

            <tr>
              <td width="50%" align="right"><p class="form_details"><strong>Postcode:</strong>
                	<input name="postcodezip" type="text" id="postcodezip" value="<? echo $_POST['postcodezip']; ?>" tabindex="15" /></p></td>
              <td width="50%" align="right" valign="top"><p class="form_details"><strong>Country:</strong>
                <select name="country">
                  <option value="United Kingdom" selected="selected">United Kingdom</option>
                  <option value=""> - - - - - - - - - - </option>
                  <option value="Other">Other</option>
                </select></p></td>
            </tr>
        </table>
          </fieldset></td>
    </tr>

    <tr>
      <td valign="top">
  	<p><fieldset><legend>Notes</legend> 
  	<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td colspan="2" align="center"><textarea name="notes" id="notes" class="notes" tabindex="16" ><? echo $_POST['notes']; ?></textarea></td>
          </tr>
          <tr>
            <td width="50%" align="right" valign="top"><p class="form_details"><strong>Follow Up Date:</strong>
              <input name="followup" type="text" id="followup" size="25" tabindex="17" value="<? echo $_POST['followup']; ?>" />
              <a href="javascript:NewCal('followup','yyyyMMdd')"><img src="date_picker/cal.gif" width="16" height="16" border="0" alt="Pick a date" /></a></p></td>
            <td width="50%" align="right" valign="top"> </td>
          </tr>
        </table>
  	</fieldset></p>
        
  </table>

<input name="send" type="submit" id="confirm_button" value="Submit" onsubmit="return validate_form(this)">
</form>

  </div>
  
  <div id="footer"><?php echo SITE_VERSION; ?></div>
   
   </div>
   
   </body>
   
   </html>
<? } ?>

I think you forgot to resend your form variables to the check.php page. You are just checking form_set then redirecting if it is true. So no $_POST variables are even being sent to check.php.

 

I could be way off. I didn't read all of your code. 

You're right Justin, thanks for pointing that out.

 

I've made an if statement and put it into the second form action like so...

 

if (isset($_POST['form_set']) && (mysql_num_rows($company_name_result) < 1) && (!empty($_POST['clienttype'])) && (!empty($_POST['companyname'])) && (!empty($_POST['sales_contact'])) && (!empty($_POST['client_lead'])) && (!empty($_POST['pre_payment']))) {

$submit = 'includes/insert_record_process.php';

} else {

$submit = '';

}

 

So that if all conditions are met the script executes the form action and if not there is no action and the page keeps submitting to itself until it is correctly filled in.

 

James

[/code]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.