lsargent Posted June 22, 2010 Share Posted June 22, 2010 I'm working on a form submit template for consumer feedback for a customer's website. The issue I'm having is that all the variable options are being carried through and posted in the email, and I only want the selected checkboxes or fields that contain input text to show up on the back end. HTML form code and PHP command code are included below: HTML Form Code: <form method="POST" action="customer-feedback.php"> <p>How did you hear about us? <p><input type="checkbox" name="Event"> Event (which one): <input type="text" name="Which_Event" size="30"> <p><input type="checkbox" name="Website"> Website (which one): <input type="text" name="Which_Website" size="30"> <p><input type="checkbox" name="Search_Engine"> Search Engine <p><input type="checkbox" name="Word_of_Mouth"> Word of Mouth <br> <p>What type of racing interests you? (check all that apply) <p><input type="checkbox" name="IndyCar"> IndyCar <p><input type="checkbox" name="Formula_One"> Formula One <p><input type="checkbox" name="Sprint_Midgets"> Sprint, Midgets <p><input type="checkbox" name="Nascar"> NASCAR <p><input type="checkbox" name="Other"> Other (which kind): <input type="text" name="Which_Kind" size="30"> <p><input type="checkbox" name="Like_All"> I like all auto racing <p><input type="checkbox" name="Like_None"> I am not an auto racing fan <p><input type="submit" name="submit" value="Submit"> </form> PHP Code from (customer-feedback.php) <?php // Website Contact Form Generator // http://www.tele-pro.co.uk/scripts/contact_form/ // Get posted data into local variables $EmailTo = "[email protected]"; $Communication = "Customer Feedback"; $Event = Trim(stripslashes($_POST['Event'])); $Which_Event = Trim(stripslashes($_POST['Which_Event'])); $Website = Trim(stripslashes($_POST['Website'])); $Which_Website = Trim(stripslashes($_POST['Which_Website'])); $Search_Engine = Trim(stripslashes($_POST['Search_Engine'])); $Word_of_Mouth = Trim(stripslashes($_POST['Word_of_Mouth'])); $IndyCar = Trim(stripslashes($_POST['IndyCar'])); $Formula_One = Trim(stripslashes($_POST['Formula_One'])); $Sprint_Midgets = Trim(stripslashes($_POST['Sprint_Midgets'])); $Nascar = Trim(stripslashes($_POST['Nascar'])); $Other = Trim(stripslashes($_POST['Other'])); $Which_Kind = Trim(stripslashes($_POST['Which_Kind'])); $Like_All = Trim(stripslashes($_POST['Like_All'])); $Like_None = Trim(stripslashes($_POST['Like_None'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } // prepare email body text $Body = ""; $Body .= "Discovered via: Event"; $Body .= $Event; $Body .= "\n"; $Body .= "Which Event: "; $Body .= $Which_Event; $Body .= "\n"; $Body .= "Discovered via: Website"; $Body .= $Website; $Body .= "\n"; $Body .= "Which Website: "; $Body .= $Which_Website; $Body .= "\n"; $Body .= "Discovered via: Search Engine"; $Body .= $Search_Engine; $Body .= "\n"; $Body .= "Discovered via: Word of Mouth"; $Body .= $Word_of_Mouth; $Body .= "\n"; $Body .= "Fan of: IndyCar"; $Body .= $IndyCar; $Body .= "\n"; $Body .= "Fan of: Formula One"; $Body .= $Formula_One; $Body .= "\n"; $Body .= "Fan of: Sprint, Midgets"; $Body .= $Sprint_Midgets; $Body .= "\n"; $Body .= "Fan of: NASCAR"; $Body .= $Nascar; $Body .= "\n"; $Body .= "Fan of: Other"; $Body .= $Other; $Body .= "\n"; $Body .= "Which Kind: "; $Body .= $Which_Kind; $Body .= "\n"; $Body .= "I like all auto racing"; $Body .= $Like_All; $Body .= "\n"; $Body .= "I am not an auto racing fan"; $Body .= $Like_None; $Body .= "\n"; // send email $success = mail($EmailTo, $Communication, $Body, "From: <[email protected]>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> Thanks Link to comment https://forums.phpfreaks.com/topic/205495-need-to-set-form-to-post-only-selected-variables/ Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 What do you expect, you use every variable in the $Body string without checking to see if any of them exist. Checkboxes will exist in the $_POST if checked, otherwise they will not exist. As almost all of the variables are checkboxes, there's no reason to be doing a trim(stripslashes) on them. For those all you would need is: $IndyCar = isset($_POST['IndyCar']); // Later on if ($IndyCar) { $Body .= "Fan of: IndyCar\n"; } Link to comment https://forums.phpfreaks.com/topic/205495-need-to-set-form-to-post-only-selected-variables/#findComment-1075329 Share on other sites More sharing options...
lsargent Posted June 22, 2010 Author Share Posted June 22, 2010 Thanks. I don't know much about php and used this template originally for a contact form in which all variables were required. I didn't know on my end what needed to be edited. Link to comment https://forums.phpfreaks.com/topic/205495-need-to-set-form-to-post-only-selected-variables/#findComment-1075338 Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 Sure, however, the way checkboxes work is an html thing, not a php thing. With that said, this is a pretty common question. Link to comment https://forums.phpfreaks.com/topic/205495-need-to-set-form-to-post-only-selected-variables/#findComment-1075339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.