从具有关联文本框的多个复选框中捕获表单数据

时间:2018-09-20 22:12:34

标签: php html

我有一个包含许多字段的html表单,并且我构建了一个PHP表单处理程序,用于通过电子邮件将表单的结果发送给我。

我唯一遇到的麻烦是,我有一个复选框列表,每个复选框都有一个关联的文本字段。

在我的电子邮件中,我只想要一个列表,其中列出了选中的项目以及文本框的内容。例如360-数量10

以下是其中一些项目及其代码:

        <form action="form_handler2.php" method="POST">
		
          <div class="one_third first">
            <label for="first_name"><b>First Name</b> <span>*</span></label>
            <input type="text" name="first_name" id="first_name" value="" size="22" required>
          </div>
		  
          <div class="one_third">
            <label for="last_name"><b>Last Name</b> <span>*</span></label>
            <input type="text" name="last_name" id="last_name" value="" size="22" required>
          </div>

          <div class="one_third first">
            <label for="phone"><b>Phone #</b> <span>*</span></label>
            <input type="text" name="phone" id="phone" value="" size="22" required>
          </div>

          <div class="one_third">
            <label for="email"><b>Email</b> <span>*</span></label>
            <input type="email" name="email" id="email" value="" size="22" required>
          </div>		  
		  
          <div class="two_thirds first">
            <label for="business_name"><b>Business Name (type personal if not affiliated with a business)</b> <span>*</span></label>
            <input type="text" name="business_name" id="business_name" value="" size="59" required>
          </div>
		  
          <div class="two_thirds first">
            <label for="address_1"><b>Address 1</b></label>
            <input type="text" name="address_1" id="address_1" value="" size="59" >
          </div>

          <div class="two_thirds first">
            <label for="address_2"><b>Address 2</b></label>
            <input type="text" name="address_2" id="address_2" value="" size="59" >
          </div>
		  
          <div class="one_third first">
            <label for="city"><b>City</b></label>
            <input type="text" name="city" id="city" value="" size="22" >
          </div>		  
		  
          <div class="one_third">
            <label for="state"><b>State</b></label>
            <select name="state" id="state">
				<option value="">- select -</option>
				<option value="AK">AK</option>
				<option value="AL">AL</option>
				<option value="AR">AR</option>
				<option value="AZ">AZ</option>
				<option value="CA">CA</option>
				<option value="CO">CO</option>
				<option value="CT">CT</option>
				<option value="DE">DE</option>
				<option value="FL">FL</option>
				<option value="GA">GA</option>
				<option value="HI">HI</option>
				<option value="IA">IA</option>
				<option value="ID">ID</option>
				<option value="IL">IL</option>
				<option value="IN">IN</option>
				<option value="KS">KS</option>
				<option value="KY">KY</option>
				<option value="LA">LA</option>
				<option value="MA">MA</option>
				<option value="MD">MD</option>
				<option value="ME">ME</option>
				<option value="MI">MI</option>
				<option value="MN">MN</option>
				<option value="MO">MO</option>
				<option value="MS">MS</option>
				<option value="MT">MT</option>
				<option value="NC">NC</option>
				<option value="ND">ND</option>
				<option value="NE">NE</option>
				<option value="NH">NH</option>
				<option value="NJ">NJ</option>
				<option value="NM">NM</option>
				<option value="NV">NV</option>
				<option value="NY">NY</option>
				<option value="OH">OH</option>
				<option value="OK">OK</option>
				<option value="OR">OR</option>
				<option value="PA">PA</option>
				<option value="RI">RI</option>
				<option value="SC">SC</option>
				<option value="SD">SD</option>
				<option value="TN">TN</option>
				<option value="TX">TX</option>
				<option value="UT">UT</option>
				<option value="VA">VA</option>
				<option value="VT">VT</option>
				<option value="WA">WA</option>
				<option value="WI">WI</option>
				<option value="WV">WV</option>
				<option value="WY">WY</option>
			</select>
          </div>	
		  
          <div class="one_third first">
            <label for="zip"><b>Zip</b></label>
            <input type="text" name="zip" id="zip" value="" size="10" >
          </div>			  

          <div class="one_third first">
            <label for="country"><b>Country</b></label>
            <select name="country" id="country">
				<option value="">- select -</option>
				<option value="US">US</option>	
			</select>				
          </div>	

          <div class="two_thirds first">
            <label for="reason"><b>Reason for Contact</b> <span>*</span></label>
			Note: sample requests for businesses using Abralon pads in their current business <br>practice only. Limit one pad per grit type, max 3 pads per sample
            <select name="reason" id="reason"  required>
				<option value="">- select -</option>
				<option value="Order">Order</option>
				<option value="Reorder">Re-order</option>
				<option value="Sample">Sample Request</option>	
				<option value="Information">Information Request</option>	
				<option value="Feedback">Feedback/Comment</option>	
				<option value="Other">Other</option>	
			</select>				
          </div>

          <div class="two_thirds first">
            <b>Order/Sample Request - </b>Select grits and quantity.  Full boxes are 20 count. <br>Orders of 20 or more qualify for lower pricing.  Orders of 60 or more pads are <br>automatically upgraded to priority mail.
		  </div>
				<div class="inline-field">
					<input type="checkbox" name="grit[]" value="180">
					<label for="180"> 180 &nbsp; &nbsp; 
					<input id="180_qty" name="180_qty" type="text" placeholder="Qty" size="5" class="180_qty">
					</label><br>
				</div>
				<div class="inline-field">
					<input type="checkbox" name="grit[]" value="360">
					<label for="360"> 360 &nbsp; &nbsp; 
					<input id="360_qty" name="360_qty" type="text" placeholder="Qty" size="5" class="360_qty">
					</label><br>
				</div>
				<div class="inline-field">					
					<input type="checkbox" name="grit[]" value="500">
					<label for="500"> 500 &nbsp; &nbsp; 
					<input id="500_qty" name="500_qty" type="text" placeholder="Qty" size="5" class="500_qty">
					</label><br>	
				</div>
				<div class="inline-field">					
					<input type="checkbox" name="grit[]" value="1000">
					<label for="1000"> 1000 &nbsp; 
					<input id="1000_qty" name="1000_qty" type="text" placeholder="Qty" size="5" class="1000_qty">
					</label><br>
				</div>
				<div class="inline-field">					
					<input type="checkbox" name="grit[]" value="2000">
					<label for="2000"> 2000 &nbsp; 
					<input id="2000_qty" name="2000_qty" type="text" placeholder="Qty" size="5" class="2000_qty">
					</label>
					<br>	
				</div>
				<div class="inline-field">
					<input type="checkbox" name="grit[]" value="3000">
					<label for="3000"> 3000 &nbsp; 
					<input id="3000_qty" name="3000_qty" type="text" placeholder="Qty" size="5" class="3000_qty">
					</label>
					<br>
				</div>
				<div class="inline-field">					
					<input type="checkbox" name="grit[]" value="4000"> 
					<label for="4000"> 4000 &nbsp; 
					<input id="4000_qty" name="4000_qty" type="text" placeholder="Qty" size="5" class="4000_qty">
					</label><br>					
				</div>
 
		  
          <div class="two_thirds first">
            <label for="comment"><b>Comments/Special Instructions</b></label>
            <textarea name="comment" id="comment" cols="59" rows="5"></textarea>
          </div>
		  
		  <br>Order will be confirmed by email with pricing. Payment information must be<br> received via phone or fax prior to shipment. 
		  
		  <br><br>
		  
		  <b>NOTE:  Fields with "*" are required</b><br><br>
		  
				<div class="inline-field">					
					<input type="checkbox" name="abranet_info" value="abranet_info"> 
					<label for="abranet_info"> I would like information on Abranet pads &nbsp; 
					</label><br>					
				</div>
		  
				<div class="inline-field">					
					<input type="checkbox" name="newsletter" value="newsletter" checked> 
					<label for="newsletter"> Sign me up for your quarterly newsletter on Abralon and related products &nbsp; 
					</label><br>					
				</div>	<br>	  
		  
          <div>
            <input type="submit" name="submit" value="Submit Form">
            &nbsp;
            <input type="reset" name="reset" value="Reset Form">
          </div>
        </form>

我的php是:

<?php
$field_first_name = $_POST['first_name'];
$field_last_name = $_POST['last_name'];
$field_phone = $_POST['phone'];
$field_email = $_POST['email'];
$field_business_name = $_POST['business_name'];
$field_address_1 = $_POST['address_1'];
$field_address_2 = $_POST['address_2'];
$field_city = $_POST['city'];
$field_state = $_POST['state'];
$field_zip = $_POST['zip'];
$field_country = $_POST['country'];
$field_reason = $_POST['reason'];

$field_comment = $_POST['comment'];


$mail_to = 'xxx@xxx.com';
$subject = 'XXX - '.$field_reason;

$body_message = 'Name: '.$field_first_name.' '.$field_last_name."\n";
$body_message .= 'Business Name: '.$field_business_name."\n";
$body_message .= 'E-mail: '.$field_email."\n\n";
$body_message .= 'Phone: '.$field_phone."\n\n";
$body_message .= 'Address:  '.$field_address_1."\n";
$body_message .= '               '.$field_address_2."\n";
$body_message .= '               '.$field_city.', '.$field_state.' '.$field_zip.' '.$field_country."\n\n";

$body_message .= 'Comment: '.$field_comment."\n";

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        window.location = 'success.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        window.location = 'failure.html';
    </script>
<?php
}

?>

1 个答案:

答案 0 :(得分:0)

您没有用于“砂砾”的php,我认为这是您关注的部分。

不过,我认为您想做的是将每个粗粒复选框命名为

<input type="checkbox" name="grit3000" value="3000">

然后在PHP中,您将需要为每个对象添加一个if语句

if (isset($_POST['grit3000']) {
     $body_message .= 'Grit3000 CHECKED \n';
}