Jump to content

Need to set form to post only selected variables


lsargent

Recommended Posts

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@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\">";
}
?> 

 

Thanks

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.