1

I have an html form that has lots of fields, and I have built a PHP form handler to email the results of the form to me.

The only part I am having trouble with is, I have a list of checkboxes with an associated text field for each.

In my email, I want a list of ONLY the items that are checked, with the contents of the text box. For Example, 360 - qty 10

Here are a few of the items and the code for them:

        <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>

My php is:

<?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 = '[email protected]';
$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
}

?>

2
  • Can you show your php form handler? That is where you will need to add some logic to accomplish this. Commented Sep 20, 2018 at 22:24
  • I edited including my complete form and added my php form handler. Thank you! Commented Sep 20, 2018 at 22:57

1 Answer 1

1

You don't have the php for the "grit," which I assume is the part you're concerned about.

However what I think you want to do, is name each grit checkbox like

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

And then in the PHP you will want to have an if statement for each

if (isset($_POST['grit3000']) {
     $body_message .= 'Grit3000 CHECKED \n';
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are wonderful - that is going to do it. I really really appreciate the help. I tried to upvote the answer, but due to my low rating it doesn't register. Lol, I can usually find my answers in previous posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.