Marty Walker Posted August 22, 2012 Share Posted August 22, 2012 Hi, I loathe forms. Wherever possible I try to avoid implementing them. In this case I have no choice and I'm left with an issue I can't figure out - this is based on Ryan's basic form example. I'm stumped on how to send the check box values to the recipient email. It's probably glaringly obvious to most except me. Regards Marty <?php /** * basic-form.php - Example of a simple contact form in ProcessWire * */ // set this to the email address you want to send to (or pull from a PW field) $emailTo = 'email@email.com'; // or if not set, we'll just email the default superuser if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email; // set and sanitize our form field values $form = array( 'contact_name' => $sanitizer->text($input->post->contact_name), 'email' => $sanitizer->email($input->post->email), 'comments' => $sanitizer->textarea($input->post->comments), ); // initialize runtime vars $sent = false; $error = ''; // check if the form was submitted if($input->post->submit) { // determine if any fields were ommitted or didn't validate //foreach($form as $key => $value) { // if(empty($value)) $error = "<p class='form-error'>Please completed all fields.</p>"; //} //if(empty($form->$company_name)) $error = "<p class='form-error'>Please completed CF field.</p>"; // if no errors, email the form results if(!$error) { $subject = "A message from the contact form"; $message = ''; foreach($form as $key => $value) $message .= "$key: $value\n"; mail($emailTo, $subject, $message, "From: $form[email]"); $sent = true; } } if($sent) { echo "<p>Thank you, your message has been sent.</p>"; // or pull from a PW field } else { // encode values for placement in markup foreach($form as $key => $value) { $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8"); } // output the form echo <<< _OUT $error <form action="./" method="post" id="contact-form"> <p> <label for="company_name" class="contact_form">Company name:</label> <input type="text" id="company_name" class="contact_form " name="company_name" value="$form[company_name]" size="40" /> </p> <p> <label for="address_1" class="contact_form">Address 1:</label> <input type="text" id="address_1" class="contact_form " name="address_1" value="$form[address_1]" size="40" /> </p> <p> <label for="address_2" class="contact_form">Address 2:</label> <input type="text" id="address_2" class="contact_form " name="address_2" value="$form[address_2]" size="40" /> </p> <p> <label for="suburb" class="contact_form">Suburb</label> <input type="text" id="suburb" class="contact_form " name="suburb" value="$form[suburb]" size="40" /> </p> <p> <label for="state" class="contact_form">State:</label> <input type="text" id="suburb" class="contact_form " name="state" value="$form[state]" size="20" /> </p> <p> <label for="postcode" class="contact_form">Postcode:</label> <input type="text" id="postcode" class="contact_form " name="postcode" value="$form[postcode]" size="10" /> </p> <p> <label for="country" class="contact_form">Country:</label> <input type="text" id="postcode" class="contact_form " name="country" value="$form[country]" size="40" /> </p> <p> <label class="contact_form" for="contact_name">Contact person:*</label> <input class="contact_form required" type="text" id="contact_name" name="contact_name" size="40" value="$form[contact_name]" /> </p> <p> <label for="position" class="contact_form">Position:</label> <input type="text" id="position" class="contact_form " name="position" value="$form[position]" size="40" /> </p> <p> <label for="phone" class="contact_form">Phone</label> <input type="text" id="phone" class="contact_form " name="phone" value="$form[phone]" size="40" /> </p> <p> <label class="contact_form" for="email">Your email:*</label> <input class="contact_form required" type="email" name="email" id="email" size="40" value="$form[email]" /> </p> <fieldset class="products"> <p>Products of interest:</p> <fieldset class="prod_col"> <input type="checkbox" id="Hats" name="Hats" value="$form[hats]" /><label for="Hats" class="Hats">Hats</label><br/> <input type="checkbox" id="Gloves" name="Gloves" value="$form[gloves]" /><label for="Gloves" class="Gloves">Gloves</label><br/> <input type="checkbox" id="Mens" name="Mens" value="$form[mens]" /><label for="Mens" class="Mens">Mens</label><br/> </fieldset> <fieldset class="prod_col"> <input type="checkbox" id="Bags" name="Bags" value="$form[bags]" /><label for="Bags" class="Bags">Bags</label><br/> <input type="checkbox" id="Scarves" name="Scarves" value="$form[scarves]" /><label for="Scarves" class="Scarves">Scarves</label><br/> <input type="checkbox" id="Clothing" name="Clothing" value="$form[clothing]" /><label for="Clothing" class="Clothing">Clothing</label><br/> </fieldset> </fieldset> <p> <label class="contact_form comments" for="comments">If you have any further comments or questions, please complete this area:</label> <textarea class="contact_form" id="comments" name="comments" rows="5" cols="62">$form[comments]</textarea> </p> <input class="contact_submit" type="submit" name="submit" value="Submit" /> <p>* required fields</p> </form> _OUT; } ?> Link to comment Share on other sites More sharing options...
ryan Posted August 22, 2012 Share Posted August 22, 2012 If you stick with the way you are doing it now, giving each checkbox field it's own 'name' attribute, then you would just need to update your $form array to include them, like the additional 'Hats' entry below: $form = array( 'contact_name' => $sanitizer->text($input->post->contact_name), 'email' => $sanitizer->email($input->post->email), 'comments' => $sanitizer->textarea($input->post->comments), 'Hats' => ($input->post->Hats ? 'Yes' : 'No') ); Your <input> tag should also read like this: $checked = array(); $checked['Hats'] = ($form['hats'] === 'Yes' ? 'checked' : ''); … <input type='checkbox' id='Hats' name='Hats' value='1' $checked[Hats]> However, I think a better way to approach all of the above is to use just one $products variable to account for all of them. $products = array('Hats', 'Gloves', 'Mens', 'Bags', 'Scarves', 'Clothing'); $form = array( 'contact_name' => $sanitizer->text($input->post->contact_name), 'email' => $sanitizer->email($input->post->email), 'comments' => $sanitizer->textarea($input->post->comments), 'products' => array() // make it an empty array ); // sanitize like this if($input->post->products) foreach($input->post->products as $product) { // here we are just ensuring submitted products are in fact valid if(in_array($product, $products)) $form['products'][] = $product; // add product } // output the checkboxes like this: foreach($products as $product) { $checked = in_array($product, $form['products']) ? "checked" : ''; echo "<label><input type='checkbox' name='products[]' value='$product' $checked> $product</label><br>"; } The above may look longer, but keep in mind it's replacing the entire checkboxes input and output, while giving you more flexibility. Adding a new product is just a matter of adding it to the $products array at the top. When you generate your email, you'll want to detect the array so that the email has the product names rather than the text "array()": foreach($form as $key => $value) { if($key == 'products') $value = implode(', ', $value); // make it a CSV string $message .= "$key: $value\n"; } 1 Link to comment Share on other sites More sharing options...
Marty Walker Posted August 23, 2012 Author Share Posted August 23, 2012 Ryan, Thanks very much for taken the time to reply at length. (Actually, I'm starting to think there are two of you. ) How do I weave this into that large form output? <?php // output the checkboxes like this: foreach($products as $product) { $checked = in_array($product, $form['products']) ? "checked" : ''; echo "<label><input type='checkbox' name='products[]' value='$product' $checked> $product</label><br>"; } ?> Regards Marty Link to comment Share on other sites More sharing options...
slkwrm Posted August 23, 2012 Share Posted August 23, 2012 If you mean wrapping your inputs in fieldsets then you could do it this way: $fNum = 3; //number of fields in a fieldset $n =1; //counter foreach ($products as $product) { $checked = in_array($product, $form['products']) ? "checked" : ''; $out = "<label><input type='checkbox' name='products[]' value='$product' $checked> $product</label>"; if (($n%$fNum == 1) || $product === $products->first() ) { $out = '<fieldset class="prod_col">'.$out; } if (!($n%$fNum ) || $product === $products->last() ) { $out = $out.'</fieldset>'; } echo $out; $n++; } Edit: corrected wrong variable names Link to comment Share on other sites More sharing options...
Marty Walker Posted August 23, 2012 Author Share Posted August 23, 2012 Thanks silkworm. PHP noob here. I don't understand what the "echo <<< _OUT" before all the form html does as well as the "_OUT;" after it. I want to get that foreach in there somehow. Regards Marty Link to comment Share on other sites More sharing options...
SiNNuT Posted August 23, 2012 Share Posted August 23, 2012 Thanks silkworm. PHP noob here. I don't understand what the "echo <<< _OUT" before all the form html does as well as the "_OUT;" after it. I want to get that foreach in there somehow. Regards Marty That is php heredoc syntax and can be quite useful. Link to comment Share on other sites More sharing options...
slkwrm Posted August 23, 2012 Share Posted August 23, 2012 You can't use this code inside heredoc string as it's basically a big string from <<< _OUT to _OUT; Heredoc syntax is easy to understand so try to look up the link SiNNuT posted. You can have as many heredoc identifiers as you need. echo <<<MYSTRING my string's content and $myVar MYSTRING; So it basically serves to let you write a big string right in your code. If you don't want to fiddle with heredoc, you can still use ordinary echo to output your form line by line. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now