PHP表单提交而不填写分支等必填字段

时间:2017-07-12 20:57:38

标签: php forms

表单提交没有填写必填字段我是PHP的新手,所以这对我来说有点困难。

1 个答案:

答案 0 :(得分:0)

<?php 
include ("app_code/config.php");
session_start();

$errors=array();
if ($_POST){
	
$admin_url="http://".$_SERVER['HTTP_HOST']."/".ADMIN_DOWNLOAD_FILEPATH;
$message="Attention Sales Staff\n<br />One of your website visitors has sent you a request for quotation.Please see the details below:\n<br />";

//$message="Attention Sales Staff\nOne of your website visitors has sent you a request for quotation. Please see the details below:\n";
//Check each field and build errors array if problems found 
	if(isset($_POST['Iagree']) && !empty($_POST['Iagree'])){
		$message .="\n\n<br />I have read and agree to the Toyota Jamaica privacy policy.";
	}
	else {
		$errors['Iagree']='Check if you read and agree to privacy policy.'; 
	}
	if(isset($_POST['model']) && !empty($_POST['model'])){
		$message .="\n<br />Please send a price quotation on this model: {$_POST['model']}\n<br />";
	}
	else {
		$errors['model']='Please enter your preferred model.'; 
	}
	
	if(isset($_POST['companyname']) && !empty($_POST['companyname'])){
		$message .="\n<br />Company Name: {$_POST['companyname']}";
	}
	if(isset($_POST['fnamelname']) && !empty($_POST['fnamelname'])){
	 $message .="\n<br />First Name & Last Name: {$_POST['fnamelname']}";
	}
	else {
		$errors['fnamelname']='Please enter name.'; 
	}
	if(isset($_POST['email']) && !empty($_POST['email'])){
		$email= trim($_POST['email']);
		list($userName, $mailDomain) = split("@", $email); 
		if (checkdnsrr($mailDomain, "MX")) {//check email domain address validity
			$message .= "\n<br />Email: ". $email;
		}
		else{ 
			$errors['email']='Please enter your valid email address'; 
		}	
	}
	else {
		$errors['email']='Please enter your email. ';
	}	
	
	if(isset($_POST['phone']) && !empty($_POST['phone'])){
		 $message .="\n<br />Phone: {$_POST['phone']}";
	}
	else {
		$errors['phone']='Please enter your phone number.';
	}

	if(isset($_POST['besttime']) && !empty($_POST['besttime'])){
		$message .="\n<br />Best time to call: {$_POST['besttime']}";
	}
	if(isset($_POST['fax']) && !empty($_POST['fax'])){
		 $message .="\n<br />Fax: {$_POST['fax']}";
	}
	if(isset($_POST['address']) && !empty($_POST['address'])){
		 $message .="\n<br />Address: {$_POST['address']}";
	}
	if(isset($_POST['city']) && !empty($_POST['city'])){
		 $message .="\n<br />City: {$_POST['city']}";
	}
	if(isset($_POST['zcode']) && !empty($_POST['zcode'])){
		 $message .="\n<br />Code: {$_POST['zcode']}";
	}
	if(isset($_POST['pcontact']) && !empty($_POST['pcontact'])){
		 $message .="\n<br />Preferred contact: {$_POST['pcontact']}";
	}	
	if(isset($_POST['paytime']) && !empty($_POST['paytime'])){
		 $message .="\n<br />I plan to purchase within: {$_POST['paytime']}";
	}
		if(isset($_POST['paymethod']) && !empty($_POST['paymethod'])){
		 $message .="\n<br />Payment method: {$_POST['paymethod']}";
	}
		if(isset($_POST['adoptions']) && !empty($_POST['adoptions'])){
		//$message .="\nAdditional Request(s): {$_POST['adoptions']}";
		$message .="\n<br />Additional Request(s): ".strip_tags($_POST['adoptions']);
	}
	//$message .="\n";

	$message.="\r\n\n<br /><hr /><br /><small>You can visit the admin area to download quote requests. \r\n\n<br />To login to the admin area, please visit \r\n<br />". $admin_url ."</small>"; 


	//Create variables for the email
	$to = 'sales@toyotajamaica.com';
	
	$subject = 'Toyota Jamaica Quotation Request Form';
	//$bcc = 'loraine@zipltd.com';
	//$from = 'do-not-reply@toyotajamaica.com';
	//if no errors, send email and redirect to acknowledgment page
	if (!$errors){		
		/*
		mail( $to, $subject, $message,
                "From: $email\r\n" .
				//"Reply-To: $email\r\n".
				"Bcc: $bcc\r\n".
                "X-Mailer: PHP/" . phpversion());
		header("Location:".$_SERVER['PHP_SELF']."?name=$_POST[fnamelname]");
		exit();
		*/
		$qr='
		insert into quote (model, companyname, fnamelname, email,phone,besttime,fax,address,city,zcode,preferredcontact, paytime, paymethod, additionaloptions )
		values ("'.mysql_real_escape_string(htmlentities($_POST['model'])).'", 
"'.mysql_real_escape_string(htmlentities( $_POST['companyname'])).'", "'.mysql_real_escape_string(htmlentities( $_POST['fnamelname'])).'", "'.mysql_real_escape_string(htmlentities( trim($_POST['email']))).'", "'.mysql_real_escape_string(htmlentities( $_POST['phone'])).'", 
"'.mysql_real_escape_string(htmlentities( $_POST['besttime'])).'", "'.mysql_real_escape_string(htmlentities( $_POST['fax'])).'", "'.mysql_real_escape_string(htmlentities( $_POST['address'])).'", "'.mysql_real_escape_string(htmlentities( $_POST['city'])).'", 
"'.mysql_real_escape_string(htmlentities( $_POST['zcode'])).'", "'.mysql_real_escape_string(htmlentities( $_POST['pcontact'])).'", 
"'.mysql_real_escape_string(htmlentities( $_POST['paytime'])).'", 
"'.mysql_real_escape_string(htmlentities( $_POST['paymethod'])).'", "'.mysql_real_escape_string(htmlentities( $_POST['adoptions'])).'")';
		
		//Insert record to database
    	$insert = mysql_query($qr) /* or die(mysql_error())*/; 
		
		if ($insert)		{ 
		  		
		  /***** notification email to admin ***********/
		  $body = "<html>\n". "<body>\n".$message. "</body>\n"."</html>\n";
		  $headers  = "From: $email\r\n"."X-Mailer: PHP/" . phpversion(). 'MIME-Version: 1.0' . "\n". 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
		  mail( $to, $subject, $body, $headers ); 
		  /***** notification EOF ***********/	
		  
		  header("Location:".$_SERVER['PHP_SELF']."?name=$_POST[fnamelname]");
		  exit();
		}//end insert	
		
	}//end no errors
	
}//end post
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				<link rel="shortcut icon" href="images/favicon.png"/>
        <title>Welcome to Toyota Jamaica | Quote Request</title>
				
        <!-- STYLE SHEETS -->
        <link rel="stylesheet" type="text/css" href="css/reset.css" />
        <link rel="stylesheet" type="text/css" href="css/style.css" />
        <link rel="stylesheet" type="text/css" href="css/prettyPhoto.css" />
        <link rel="stylesheet" type="text/css" href="css/styletoggle.css" />
        
				<!-- JAVASCRIPT FILES -->
				<!-- JQuery librery -->
        <script type="text/javascript" src="js/lib/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/lib/jquery.easing.1.3.js"></script>
        <script type="text/javascript" src="js/lib/jquery-ui.min.js"></script>
				
        <!-- Cufon Text Replacment -->
        <script type="text/javascript" src="js/cufon/cufon-yui.js"></script>
        <script type="text/javascript" src="js/cufon/quicksand.font6aec.js"></script>
				
        <!-- JQuery Plugins -->
				<script type="text/javascript" src="js/superfish.js"></script>
        <script type="text/javascript" src="js/jquery.tooltip.js"></script>
				<script type="text/javascript" src="js/twitter.js"></script>
        <script type="text/javascript" src="js/jquery.asyncImgLoader.js"></script>
        <script type="text/javascript" src="js/jquery.prettyPhoto.js"></script>
				
        <!-- Custom javascript -->
        <script type="text/javascript" src="js/common.js"></script>
        <script type="text/javascript" src="js/costum.js"></script>
		<script type="text/javascript" src="js/nav_current.js"></script>

            <!--[if IE 6]>
            <script type="text/javascript" src="js/dd_belated_png.js"></script>
            <script type="text/javascript">
            DD_belatedPNG.fix('.pngfix');
            </script>
			<![endif]-->				
        <script type="text/javascript" src="js/analytics.js"></script>
    </head>    <body>
<div><a id="topArrow" class="tooltip pngfix" title="To Top" href="#top"></a></div>
<!-- start #top -->
        <?php include ('_includes/top.php') ; ?>
        <!-- end #top -->
		<!-- start #topnav -->
        <?php include ('_includes/menu_top.php') ; ?>
        <!-- end #topnav -->
        <div id="featured" class="wrapper pngfix">
            <div id="page_title" class="center">
                <h1>Request a Vehicle Quote</h1>
                <div class="title_slogan">Get a vehicle quotation from the comfort of your own home! </div>
            </div>
        </div><!-- end #featured -->
        <div id="main" class="wrapper">
            <div id="inner_main" class="center">
            		<? include ('_includes/breadcrumbs.php');?>
                <div id="sidebar" class="right_sidebar">
                    <span class="sidebar_top"></span>
                    
                    <?php include ('_includes/menu_shopping.php') ; ?>
                    
                    <?php include ('_includes/side_promos_about.php') ; ?>
                    <span class="sidebar_bottom"></span>
                </div>
                <!-- end sidebar -->
                <div id="main_left">
                
                    <p>Complete the form below to request a quotation on a new vehicle.</p>
                    
                    <div class="hr_divider"></div>

	  <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="quote">
	  	<?php if( count( $errors) > 0) {  ?> 
		<span style="font-family:Arial; font-size:8pt; font-weight:bold; color:red;">Please correct the errors and then resubmit. </span> 
		<?php }
				else if (isset($_GET['name'])) 
				{  //if no errors exist ?> <span style="font-family:Arial; font-size:10pt; font-weight:bold;color:red">	Thank You <?php echo $_GET['name']; ?> your request has been successfully sent. 	<?php 	}  ?></span> 
                
                <h4>Request a price on a new vehicle</h4>
                
                <p class="inputheader_12">Model:</p>
                <p class="requireField_12">(required)</p>
                <p id="errorName" class="ErrorData_12">
                <?php if (!empty($errors['model'])) {?> <font color="#FF0000"> <?php  echo '*'.$errors['model'];?></font><?php  }?>
                </p>
                <div class="inputTextBG_12">
                
      <select name="model" class="input-Field_12">
        <option <?= $_POST['model'] == "" ? "selected" : "" ?>  value="" selected>Select a model</option>
        <option <?= $_POST['model'] == "Yaris" ? "selected" : "" ?> value="Yaris">Yaris</option> 
        <option <?= $_POST['model'] == "Corolla" ? "selected" : "" ?> value="Corolla">Corolla</option>
        <option <?= $_POST['model'] == "FT86" ? "selected" : "" ?> value="FT86">FT86</option>
        <option <?= $_POST['model'] == "Prius Hybrid" ? "selected" : "" ?> value="Prius Hybrid">Prius Hybrid</option>            
        <option <?= $_POST['model'] == "Camry" ? "selected" : "" ?> value="Camry">Camry</option>
        <option <?= $_POST['model'] == "Camry Hybrid" ? "selected" : "" ?> value="Camry Hybrid">Camry Hybrid</option>
		<option <?= $_POST['model'] == "RAV4" ? "selected" : "" ?> value="RAV4">RAV4</option>
		<option <?= $_POST['model'] == "Fortuner" ? "selected" : "" ?> value="Fortuner">Fortuner</option>
		<option <?= $_POST['model'] == "Prado" ? "selected" : "" ?> value="Prado">Prado</option>
        <option <?= $_POST['model'] == "Land Cruiser" ? "selected" : "" ?> value="Land Cruiser">Land Cruiser</option>

		<option <?= $_POST['model'] == "Hilux" ? "selected" : "" ?> value="Hilux">Hilux</option>
		<option <?= $_POST['model'] == "Hiace Panel Van" ? "selected" : "" ?> value="Hiace Panel Van">Hiace Panel Van</option>
		<option <?= $_POST['model'] == "Hiace Bus" ? "selected" : "" ?> value="Hiace Bus">Hiace Bus</option>
		<option <?= $_POST['model'] == "Coaster" ? "selected" : "" ?> value="Coaster">Coaster</option>
		<!--<option <?= $_POST['model'] == "Daihatsu Terios" ? "selected" : "" ?> value="Daihatsu Terios">Daihatsu Terios</option>-->
		 </select>
         </div>
         
         <h4>Select Branch Location</h4>
                
                <p class="inputheader_12">Branches:</p>
                <p class="requireField_12">(required)</p>
                <p id="errorName" class="ErrorData_12">
                <?php if (!empty($errors['branches'])) {?> <font color="#FF0000"> <?php  echo '*'.$errors['branches'];?></font><?php  }?>
                </p>
                <div class="inputTextBG_12">
                
      <select name="branch" class="input-Field_12">
        <option <?= $_POST['branches'] == "" ? "selected" : "" ?>  value="" selected>Select a branch</option>
        <option <?= $_POST['branches'] == "Spanish Town Road Branch" ? "selected" : "" ?> value="Spanish Town Road Branch">Spanish Town Road Branch</option> 
        <option <?= $_POST['branches'] == "Old Hope Road Branch" ? "selected" : "" ?> value="Old Hope Road Branch">Old Hope Road Branch</option>
        <option <?= $_POST['branches'] == "Montego Bay Branch" ? "selected" : "" ?> value="Montego Bay Branch">Montego Bay Branch</option>
        
		 </select>
         </div>
         
         		<p class="inputheader_12">Additional options:</p>
                <p class="requireField_12"></p>
                <p id="errormesssageArea" class="ErrorData_12">
                </p>
                <div id="inputTextAreaBG">
                <textarea id="messageField" name="adoptions" value="" cols="60" rows="3" style="font-size:10px"><?php if (isset($_POST['adoptions'])) echo $_POST['adoptions'] ;?></textarea>
                </div>
                
                
                 <div class="hr_divider"></div>
                <h4>Please enter your contact information</h4>               
                
                <table>
                <tr>
                <td>
                <p class="inputheader_12">Company Name:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                	<input name="companyname" value="<?php if (isset($_POST['companyname'])) echo $_POST['companyname'] ;?>" type="text" size="30" class="input-Field_12" maxlength="50">
                    </div>
                    
                    
                    <p class="inputheader_12">Email:</p>
                <p class="requireField_12">(required)</p>
                <p id="errorName" class="ErrorData_12">
                <?php if (!empty($errors['email'])) {?> <font color="#FF0000"> <?php  echo '*'.$errors['email'];?></font><?php  }?>
                </p>
                <div class="inputTextBG_12">
                <input name="email" value="<?php if (isset($_POST['email'])) echo $_POST['email'] ;?>"  type="text" size="30" class="input-Field_12" maxlength="50">
                </div>
                    
                    
                    
                <p class="inputheader_12">Phone:</p>
                <p class="requireField_12">(required)</p>
                <p id="errorName" class="ErrorData_12">
                <?php if (!empty($errors['phone'])) {?> <font color="#FF0000"> <?php  echo '*'.$errors['phone'];?></font><?php  }?>
                </p>
                <div class="inputTextBG_12">
                <input name="phone" value="<?php if (isset($_POST['phone'])) echo $_POST['phone'] ;?>"  type="text" size="20" class="input-Field_12" maxlength="20">
                </div>
                
                
                <p class="inputheader_12">Address:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12"> 
                <input name="address" value="<?php if (isset($_POST['address'])) echo $_POST['address'] ;?>"  type="text" size="35" class="input-Field_12" maxlength="50">
                </div>
                
                <p class="inputheader_12">Code:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                <input name="zcode" value="<?php if (isset($_POST['zcode'])) echo $_POST['zcode'] ;?>"  type="text" size="20" class="input-Field_12" maxlength="30">
                </div>
                
                </td>
                <td width="50px">&nbsp;</td>
                <td>
                
                    
                <p class="inputheader_12">First Name & Last Name:</p>
                <p class="requireField_12">(required)</p>
                <p id="errorName" class="ErrorData_12">
                <?php if (!empty($errors['fnamelname'])) {?> <font color="#FF0000"> <?php  echo '*'.$errors['fnamelname'];?></font><?php  }?>
                </p>
                <div class="inputTextBG_12">
                <input name="fnamelname" value="<?php if (isset($_POST['fnamelname'])) echo $_POST['fnamelname'] ;?>"  type="text" size="30" class="input-Field_12" maxlength="50">
                </div>
                
                 <p class="inputheader_12">Fax:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                <input name="fax" value="<?php if (isset($_POST['fax'])) echo $_POST['fax'] ;?>"  type="text" size="20" class="input-Field_12"  maxlength="20">
                </div>
                
                
                
                <p class="inputheader_12">Best time to call:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                <select name="besttime" class="input-Field_12">
      <option <?= $_POST['besttime'] == "Morning" ? "selected" : "" ?>  value="Morning">Morning</option>
      <option <?= $_POST['besttime'] == "Noon" ? "selected" : "" ?>  value="Noon">Noon</option>
      <option <?= $_POST['besttime'] == "Afternoon" ? "selected" : "" ?>  value="Afternoon">Afternoon</option>
      <option <?= $_POST['besttime'] == "Evening" ? "selected" : "" ?>  value="Evening">Evening</option>
    </select>
    </div>
    
                <p class="inputheader_12">City:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                <input name="city" value="<?php if (isset($_POST['city'])) echo $_POST['city'] ;?>"  type="text" size="20" class="input-Field_12" maxlength="30">
                </div>
                
                
                
                <p class="inputheader_12">Preferred contact:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                <select name="pcontact" class="input-Field_12">
      <option <?= $_POST['pcontact'] == "Email" ? "selected" : "" ?>  value="Email" >Email</option>
      <option <?= $_POST['pcontact'] == "Phone" ? "selected" : "" ?>  value="Phone" >Phone</option>
      <option <?= $_POST['pcontact'] == "Fax" ? "selected" : "" ?>  value="Fax" >Fax</option>
      <option <?= $_POST['pcontact'] == "Any" ? "selected" : "" ?>  value="Any" >Any</option>
        </select>
        </div>
                </td>
                </tr>
                </table>
                
                    
        <div class="hr_divider"></div>
        
        
        <h4>Tell us about your purchase plans</h4>
        
        <table>
        <tr>
        <td>
        
        <p class="inputheader_12">Purchase time:</p>
                <p class="requireField_12"></p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                <select name="paytime" class="input-Field_12">
      <option <?= $_POST['paytime'] == "72hours" ? "selected" : "" ?>  value="72hours">I plan to purchase in the next 72 hours</option>
      <option <?= $_POST['paytime'] == "1month" ? "selected" : "" ?>  value="1month">I plan to purchase in the next month</option>
      <option <?= $_POST['paytime'] == "3months" ? "selected" : "" ?>  value="3months">I plan to purchase in the next 3 months</option>
      <option <?= $_POST['paytime'] == "undecided" ? "selected" : "" ?>  value="undecided">I am undecided</option>
        </select>
        </div>
        
        </td>
        <td width="50px">&nbsp;</td>
        <td>
        
         <p class="inputheader_12">Payment method:</p>
                <p class="requireField_12">(required)</p>
                <p id="errorName" class="ErrorData_12">
                </p>
                <div class="inputTextBG_12">
                <select name="paymethod" class="input-Field_12">
      <option <?= $_POST['paymethod'] == "Cash" ? "selected" : "" ?>  value="Cash">Cash</option>
      <option <?= $_POST['paymethod'] == "Bank Loan" ? "selected" : "" ?>  value="Bank Loan">Bank Loan</option>
      <option <?= $_POST['paymethod'] == "Toyota Finance" ? "selected" : "" ?>  value="Toyota Finance">Toyota Finance</option>
        </select>
        </div>
        
        </td>
        </tr>
        </table>
        		
        
        
       
        
        <blockquote class="blockindent">
                    	<p class="asterisk">In order to submit the form, you must agree to the privacy policy. To read the privacy policy, <a href="/privacy.php" target="_blank" >click here</a></p>
      </blockquote>
      
      <p class="inputheader_12"><input type="checkbox" name="Iagree" value="Iagree" <?= $_POST['Iagree'] == "Iagree" ? "checked" : "" ?>/> I have read and agree to the privacy policy.</p>
                <p class="requireField_12">(required)</p>
                <p id="errorName" class="ErrorData_12">
                <?php if (!empty($errors['Iagree'])) {?> <font color="#FF0000"> <?php  echo '*'.$errors['Iagree'];?></font> <?php }?>
                </p>
                
                <div class="hr_divider"></div>
      
      <p id="sendButton">
      	<a href="javascript: document.quote.submit();">Send</a>
      </p>
      
	  </form>
      
     
     
                
                        
                    
                </div>
                <!-- end #main_left -->
            </div>
            <!-- end {#main .center} -->
            <p class="clear">
            </p>
        </div>
        <!-- end main -->
         <?php include ('_includes/footer.php') ; ?>
        <!-- start footer_bottom -->
        <?php include ('_includes/footer_bottom.php') ; ?>
        <!-- end footer_bottom -->
    </body>
</html>