Jump to content

[SOLVED] Mandatory Fields in a Form


bimibear

Recommended Posts

Hi,

I am having difficulty in getting my code to not upset the html layout surrounding it.

I have managed to get the form to work aesthetically when the fields are completed however, when I do not fill in the fields (to test what happens when a client ignore required fields) it throws my table and aesthetics out - ignoring my CSS.

 

I am assuming that the issue is to do with my php code as on its own the html has been working prior to putting the php code into it.

 

Any suggestions on what might be causing this would be greatly appreciated.

 

I currently have the code working here for testing purposes:

http://www.emmapagejewellery.com/find_IBO_test.php

 

My code is:

<?php
/* ***************************************************************

E-mail sender with custom mandatory fields and e-mail address validation W3C Compliant

PHP programming: Javier Valderrama

Developed for Jaxolotl Design - www.jaxolotl-design.com

Fully customizable web based e-mail system with mandatory fields checking without loosing fields content
Secure e-mail sending by destroying tags an sending e-mail in HTML format
Custom error messages
Custom mandatory fields
E-mail address validation 
W3C Compliant HTML code

*************************************************************** */
############################################### CREATE AN STANDARD TABLE TO CONTAIN ERROR MESSAGES
$alert_open ="<table width=\"200\" height=\"20\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border:0px solid #CC0000;\"><tr><td>";
$alert_close ="</td></tr></table>";

##########################################  VALIDATE MANDATARY FIELDS
function validate_required(){
global $alert_open,$alert_close ;
$allow = true;

if(	// add or change this fields to make them MANDATORY
(trim($_POST['name']))
&& (trim($_POST['email']))
&& (trim($_POST['fax']))
&& (trim($_POST['address']))
&& (trim($_POST['comment']))

){
	$allow = true;
}
else{
	// Error message when mandatory fields are empty

	echo $alert_open . "Please complete the required fields.".$alert_close;
	$allow = false;
}

if($allow==true){
	return true;
}
else{
	return false;
}
}

###############################################  RETURNS DATA FOR TEXTAREA
function return_textarea_text($string,$allow_html){
switch($allow_html):

case'html':
$patterns = array(
'#<\s{0,}textarea#is', 
'#</\s{0,}textarea\s{0,}>#is'
);
$substitutions = array('[textarea','[/textarea]');
return preg_replace($patterns,$substitutions,double_escape(trim(stripslashes($string))));
break;

case'plain':
$patterns = array ('"' , ">" , "<");
$substitutions = array(""" , ">" , "<");

return double_escape(str_replace($patterns,$substitutions,trim(stripslashes($string))));
break;

endswitch;
}
###############################################  RETURNS DATA FOR INPUT TEXT FIELD
function return_input_text($string){
$patterns = array ('"' , ">" , "<");
$substitutions = array(""" , ">" , "<");
return double_escape(str_replace($patterns,$substitutions,strip_new_line(trim(stripslashes($string)))));
}
###############################################  PROCES PLAIN TEXT TO KILL HTML
function process_plain_text($string){//process_input_text
$patterns = array ("'", '"' , ">" , "<","“","”","’");
$substitutions = array("\'", """ , ">" , "<",""",""","\'");
$output_string = str_replace($patterns,$substitutions,trim(stripslashes($string)));

return $output_string;
}
###############################################
function strip_new_line($string){
$string = ereg_replace("[\n\r\t]", " ", $string);
return $string;
}
############################################### DOUBLE ESCAPES
function double_escape($string){
$string = str_replace("&","&",$string);
return $string;
}
###############################################
function translate_breaks($string){
if(preg_match('#\<.*?\>#s',$string)){
	return $string;
}
else{
	return nl2br(trim($string));
}
}
############################################### RETURNS AVAILABLE
function available_posted_rowed($posted,$rowed){
if($posted){
	return $posted;
}
else{
	return $rowed;
}
}
###############################################
function validate_email($email_string) {
// check if e-mail is a valid string
global $alert_open,$alert_close ;
if($email_string){
	if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email_string)){
		return true;
	}
	else{
		// error message
		echo $alert_open . "<strong class=\"red\">\"".$email_string."\"</strong><br>This e-mail address is not a valid e-amil address.<br> Please amend and retry.".$alert_close."<br>";
		return false;
	}
}
else{
	return true;
}
}

?>
<!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"><!-- InstanceBegin template="/Templates/copy.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<meta name="verify-v1" content="" />
            <!-- verification code required for Google -->
<!-- InstanceBeginEditable name="doctitle" -->
		<title>Emma Page Jewellery</title>
<!-- InstanceEndEditable -->
		<meta name="generator" content="Adobe Dreamweaver" />
		<meta name="description" content="Emma Page Jewellery" />
            <!-- add description of who Emma Page are -->
<!-- InstanceBeginEditable name="keywords" -->
		<meta name="Keywords" content="Emma Page Jewellery, Jewellery, " />
            <style type="text/css">
<!--
.style1 {color: #FF0000}
-->
            </style>
<!-- InstanceEndEditable -->
            <!-- add any words associated with Emma Page Jewellery -->
		<meta name="title" content="Emma Page Jewellery - Home Page" />
		<meta name="author" content="thompsoncooke" />
		<meta name="robots" content="all, index, follow" />
            <meta name="googlebot" content="all, index, follow"></meta>
            <meta name="pagerank™" content="10"></meta>
		<meta name="revisit" content="5 days"></meta>
            <meta name="revisit-after" content="5 days" />
		<meta name="classification" content="Emma Page Jewellery" />
            <!-- insert comma separated list of keyword phrases here. -->
            <meta name="subject" content="Emma Page Jewellery"></meta>
            <!-- cut and paste Description META Tag here. -->
		<meta name="distribution" content="Global" />
		<meta name="language" content="English" />
		<meta name="company name" content="Emma Page Jewellery" />
		<meta name="country" content="Australia, New Zealand" />
		<meta name="copyright" content="Copyright © 2009 Emma Page Jewellery" />
		<link rel="stylesheet"  type="text/css" href="css/emmapage.css" />
		<link rel="icon" type="image/x-icon" href="favicon.ico" />
		<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
            <script type="text/javascript" src="GeneratedItems/CSScriptLib.js"></script>
		<script type="text/javascript" src="GeneratedItems/dropmenu.js"></script>
            <csscriptdict import="import">
		<script type="text/javascript" src="GeneratedItems/CSScriptLib.js"></script>
		</csscriptdict>
</head>

<body>
<!-- Header -->
<table class="fixed" height="30" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
        <tr>
			<td class="nav" valign="top" align="left" height="30" width="750"></td>
     		</tr>
</table>
<!-- Navigation Panel -->
<table class="fixed" height="135" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
<tr>
			<td class="nav" width="655" height="125" align="left" valign="top">
                <a class="nav" href="home.html">HOME</a>
                <img src="000.gif" alt="" height="10" width="20" align="bottom" border="0" />
                <a class="nav" href="home.html" onclick="return clickreturnvalue()" onmouseover="dropdownmenu(this, event, menu1, '150px');return true;" onmouseout="delayhidemenu()">ABOUT</a>
                <img src="000.gif" alt="" height="10" width="20" align="bottom" border="0" />
                <a class="nav" href="home.html" onclick="return clickreturnvalue()" onmouseover="dropdownmenu(this, event, menu2, '150px')" onmouseout="delayhidemenu()">OPPORTUNITY</a>
                <img src="000.gif" alt="" height="10" width="20" align="bottom" border="0" />
                <a class="nav" href="home.html" onclick="return clickreturnvalue()" onmouseover="dropdownmenu(this, event, menu3, '150px')" onmouseout="delayhidemenu()">JEWELLERY</a>
                <img src="000.gif" alt="" height="10" width="20" align="bottom" border="0" />
                <a class="nav" href="home.html" onclick="return clickreturnvalue()" onmouseover="dropdownmenu(this, event, menu4, '150px')" onmouseout="delayhidemenu()">LATEST NEWS</a>
                <img src="000.gif" alt="" height="9" width="159" align="bottom" border="0" />	  </td>
    <td class="nav" width="95"height="125" valign="top" align="left">
    <a class="nav" href="home.html" onclick="return clickreturnvalue()" onmouseover="dropdownmenu(this, event, menu5, '150px')" onmouseout="delayhidemenu()">CONTACT</a></td>
  </tr>
</table>
<!-- Scroll Arrows Up-->
<table class="fixed" height="10" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
<!-- InstanceBeginEditable name="up" -->            
		<tr><td valign="top" align="right" width="750" height="10"><img src="000.gif" width="12" height="11" /></td>
  </tr>
<!-- InstanceEndEditable -->
</table>
<!-- Grey Rule -->
<table class="fixed" height="1" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
		<tr><td valign="top" align="centre" width="750" height="1"><img src="blue.gif" alt="" width="750" height="1"/></td></tr>
</table>
<!-- Body -->
<table class="fixed" height="235" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >

		<tr>
		<td class="square" width="11" height="222"></td>
            <!-- InstanceBeginEditable name="menucopy" -->
            <td class="ibo" width="176" height="222" valign="top" ><br />
            <span class="subheader">For more information on <br />
            purchasing
            Emma Page Jewellery<br /> 
            or becoming
            an Emma Page<br /> 
            Consultant, please
            complete your <br />
            details and submit.</span><br />
            <br />
            <span class="style1">*</span> please note the required fields<br />
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
            <br /><img src="000.gif" alt="" width="163" height="22"  valign="top"  /></td>
            <!-- InstanceEndEditable -->
		<td class="square" width="8" height="222"></td>
            <!-- InstanceBeginEditable name="copy" -->
            <td class="ibo" width="544" height="222">
            <table class="ibo" width="389" height="192">
		  <?

				if(
				(array_key_exists("send_email",$_POST))
				&&(validate_required())
				&&(validate_email(trim($_POST['email'])))
				)
				{
					$personal_information = "
					<br><strong>Name:</strong> ".process_plain_text($_POST['name'])."
					<br><strong>Email:</strong>  ".process_plain_text($_POST['email'])."
					<br><strong>Fax:</strong>  ".process_plain_text($_POST['fax'])."
					<br><strong>Address:</strong>  ".process_plain_text($_POST['address'])."
					<br><strong>Comment:</strong><br>
					".translate_breaks(process_plain_text($_POST['comment']))."	";

					/// Your e-mail address here
					$email_to = "[email protected]";

					/// The e-mail subject here
					$email_subject = "Subject";

					/// sender's e-mail
					$headers = "From: ".trim($_POST['email']);


					$semi_rand = md5(time());
					$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

					$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
					$email_message = "This is a multi-part message in MIME format.\n\n"
					. "--{$mime_boundary}\n"
					. "Content-Type:text/html; charset=\"iso-8859-1\"\n"
					. "Content-Transfer-Encoding: 8bit\n\n"

					/// E-mail message content
					."\n".$personal_information
					."<br><br>" . "\n\n";


					/// Send e-mail
					$sending_ok = @mail($email_to, $email_subject, $email_message, $headers);


					if($sending_ok){
						// If e-mail is sended successfully

						echo "Your e-mail has been send successfully with the following information:<br>"
						. $personal_information
						. "<br><br>
						Thank you for contacting us.
						";
					}
					else{
						// e-mail sending error message 
						echo $alert_open
						."We apologize, your e-mail has not been sent.<br>Please try again later.<br>
						<a href=\"".$_SERVER['PHP_SELF']."\"><strong>Back</strong></a>"
						.$alert_close;
					}
				}
				else{
					?>
                        <form method="post" action="" name="contact_us" >
                        <input type="hidden" name="send_email" value="send_email" />
            	<tr>
				<td width="64">name<span class="style1">*</span></td>
				<td width="249"><input type="text"  name="name" value="<?php echo return_input_text(available_posted_rowed($_POST['name'],"")); ?>" size="40" maxlength="30" class="ibo" ></td>
                    <td width="60"></td>
			</tr>
			<tr>
				<td>email<span class="style1">*</span></td>
				<td><input type="text"  name="email" value="<?php echo return_input_text(available_posted_rowed($_POST['email'],"")); ?>" size="40" maxlength="10" class="ibo"></td>
                    <td width="60"></td>
			</tr>
			<tr>
				<td>fax<span class="style1">*</span></td>
				<td><input type="text"  name="fax" value="<?php echo return_input_text(available_posted_rowed($_POST['fax'],"")); ?>" size="40" maxlength="10" class="ibo"></td>
                    <td width="60"></td>
			</tr>
			<tr>
				<td>address<span class="style1">*</span></td>
				<td><input type="text"  name="address" value="<?php echo return_input_text(available_posted_rowed($_POST['address'],"")); ?>" size="40" maxlength="50" class="ibo"></td>
                    <td width="60"></td>
			</tr>
			<tr>
				<td height="36">comment<span class="style1">*</span></td>
			  <td><textarea name="comment" value="<?php echo return_textarea_text(available_posted_rowed($_POST['comment'],""),"html"); ?>" rows="2" cols="35" class="ibo"></textarea></td>
                    <td width="60">
                    <input type="submit" value="Submit" style="width : 50px; margin: 5px" class="ibo" align="right"/></td>
			</tr>
       		  </form>
              <?php
		  }
		  ?>
           </table>
            <br /></td>
		<!-- InstanceEndEditable -->
            <td class="square" width="11" height="222"></td>
  </tr>
</table>

<!-- Grey Rule -->
<table class="fixed" height="1" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
		<tr><td valign="top" align="left" width="750" height="1"><img src="blue.gif" alt="" width="750" height="1" /></td></tr> 
</table>
<!-- Scroll Arrows Down-->
<table class="fixed" height="10" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
<!-- InstanceBeginEditable name="down" -->            
		<tr><td valign="top" align="right" width="750" height="10"><img src="000.gif" width="12" height="11" /></td>
  </tr>
<!-- InstanceEndEditable -->
</table>
<!-- Footer -->
<table class="fixed" height="71" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
		<tr>
		  	<td class="nav" width="655"height="71" valign="bottom"><img src="images/logo/tag.gif" width="187" height="59" /></td>
		  <td class="ibo" width="95"height="71" valign="bottom" align="left">
           	  	<a href="login.html">• IBO Login</a></td>
  </tr>
</table>
<!-- Image Loading -->
<table class="hidden" height="10" width="750" align="center" border="0" cellspacing="0" cellpadding="0" >
			<tr>
		  		<!-- InstanceBeginEditable name="load" -->
                    <td class="nav" width="750"height="10" valign="bottom">
                    </td>
                    <!-- InstanceEndEditable -->
  				</tr>
</table>        
</body>
<!-- InstanceEnd --></html>

 

Link to comment
https://forums.phpfreaks.com/topic/157301-solved-mandatory-fields-in-a-form/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.