...

View Full Version : Trouble setting form to post only selected variables, i.e. checkbox, input field, etc



lsargent
06-22-2010, 02:20 AM
I'm working with 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 and/or fields that contain input text to show up on the back end upon submission. HTML form code and PHP 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">&nbsp;&nbsp;Event (which one):&nbsp;<input type="text" name="Which_Event" size="30">
<p><input type="checkbox" name="Website">&nbsp;&nbsp;Website (which one):&nbsp;<input type="text" name="Which_Website" size="30">
<p><input type="checkbox" name="Search_Engine">&nbsp;&nbsp;Search Engine
<p><input type="checkbox" name="Word_of_Mouth">&nbsp;&nbsp;Word of Mouth
<br>
<p>What type of racing interests you?&nbsp;&nbsp;(check all that apply)
<p><input type="checkbox" name="IndyCar">&nbsp;&nbsp;IndyCar
<p><input type="checkbox" name="Formula_One">&nbsp;&nbsp;Formula One
<p><input type="checkbox" name="Sprint_Midgets">&nbsp;&nbsp;Sprint, Midgets
<p><input type="checkbox" name="Nascar">&nbsp;&nbsp;NASCAR
<p><input type="checkbox" name="Other">&nbsp;&nbsp;Other (which kind):&nbsp;<input type="text" name="Which_Kind" size="30">
<p><input type="checkbox" name="Like_All">&nbsp;&nbsp;I like all auto racing
<p><input type="checkbox" name="Like_None">&nbsp;&nbsp;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@yourdomain.com";
$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@yourdomain.com>");
// 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\">";
}
?>

DJCMBear
06-22-2010, 10:11 AM
The way to do check/radio inputs is you dont set the name as what the item is you set the name as an array and then put a value as what the item is for example.



<form method="post" action="">
<input type="checkbox" name="items[]" value="item 1" /> item 1
<input type="checkbox" name="items[]" value="item 2" /> item 2
<input type="checkbox" name="items[]" value="item 3" /> item 3
<input type="checkbox" name="items[]" value="item 4" /> item 4
</form>


And for the php post you do this.


<?php
foreach($_POST['items'] As $item)
{
print "{$item} was checked.<br />";
}
?>


If items 1 and 3 are checked the output would be this:
item 1 was checked.
item 3 was checked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum