Jump to content

HTML form > PHP > Email (multiple selectors)


JeffSydor

Recommended Posts

So I have a form that I've been developing. I originally got the code from htmlDrive.net and have been working on applying it. Basically, I need this HTML form to post to my PHP page and generate/send the posted content into an email. I have been able to get this to work great for single selections but not for multiple selection boxes.

 

Here is my form:

                    <form id="formElem" name="formElem" action="mediaForm.php" method="post">
                        <fieldset class="step">
                            <legend>Media Request</legend>
                            <p>
                                <label for="segment">Product Segment: </label>
                                <select name="segment[]" size="4" multiple id="segment">
                                    <option selected> </option>
                                    <option>Pedestrian</option>
                                    <option>Industrial</option>
                                    <option>Security</option>
                                    <option>Sensorio</option>
                                </select>
                            </p>
                            <p>
                                <label for="media">Select Media: </label>
                                <select name="media[]" size="4" multiple id="media">
                                    <option selected> </option>
                                    <option>Catalog</option>
                                    <option>Media Kit</option>
                                    <option>Technical Binder</option>
                                    <option>General Information CD</option>
                                </select>
                            </p>
                            <p>
                                <label for="print">Printed Publication </label>
                                <input name="print" type="checkbox" id="print">
                            </p>
                            <p>
                                <label for="digital">Digital Publication </label>
                                <input name="digital" id="digital" type="checkbox">
                            </p>
                        </fieldset>
                        <fieldset class="step">
                            <legend>Personal Info</legend>
                            <p>
                              <label for="company">Company: </label>
                                <input id="company" name="company" placeholder="ABC Automatics" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                                <label for="title">Job Title: </label>
                                <input id="title" name="title" placeholder="President" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                                <label for="firstName">First Name: </label>
                                <input id="firstName" name="firstName" placeholder="John" AUTOCOMPLETE = "on" />
                            </p>
                          <p>
                                <label for="lastName">Last Name: </label>
                                <input id="lastName" name="lastName" placeholder="Smith" AUTOCOMPLETE = "on" />
                            </p>
                          <p>
                                <label for="customerType">Customer Type: </label>
                                <select name="customerType[]" size="4" multiple id="customerType">
                                    <option selected> </option>
                                    <option>Wholesale</option>
                                    <option>Distributor</option>
                                    <option>Architect</option>
                                    <option>Technician</option>
                                    <option>Sales</option>
                                    <option>End User</option>
                                    <option>Other</option>
                                </select>
                                <br> 
</p>
                          <p> If you do not know your type, please choose "Other."
                          </p>
                        </fieldset>
                      <fieldset class="step">
                            <legend>Contact Info</legend>
                            <p>
                                <label for="email">Email: </label>
                                <input id="email" name="email" placeholder="jsmith@ABC_Auto.com" type="email" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                                <label for="phone">Phone Number: </label>
                                <input id="phone" name="phone" placeholder="xxx-xxx-xxxx" type="tel" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                                <label for="address">Address: </label>
                                <input id="address" name="address" placeholder="123 Enterprise Ln" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                              <label for="city">City: </label>
                              <input id="city" name="city" placeholder="City Name" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                                <label for="state">State: </label>
                                <input id="state" name="state" placeholder="ST" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                                <label for="zipcode">Zipcode: </label>
                                <input id="zipcode" name="zipcode" placeholder="12345" AUTOCOMPLETE = "on" />
                            </p>
                            <p>
                                <label for="country">Country: </label>
                                <select id="country" name="country">
                                    <option selected> </option>
                                    <option>United States</option>
                                    <option>Mexico / Latin America</option>
                                    <option>Canada</option>
                                    <option>Other</option>
                                </select>
                            </p>
                      </fieldset>
                        <fieldset class="step">
                            <legend>Comments</legend>
                            <p>
                                <label for="comments"></label>
                                <textarea name="comments" id="comments" placeholder="Insert additional comments here"></textarea>
                            </p>
                        </fieldset>
						<fieldset class="step">
                            <legend>Confirm</legend>
							<p>
								Everything in the form was correctly filled 
								if all the steps have a green checkmark icon.
								A red checkmark icon indicates that some field 
								is missing or filled out with invalid data. In this
								last step the user can confirm the submission of
								the form.
							</p>
                            <p class="submit">
                                <button id="registerButton" type="submit" value="submit">Submit</button>
                            </p>
                        </fieldset>
                    </form>

My PHP:

<?php
if(isset($_POST['email'])) {
     
    $email_to = "email@yoursite.com";
    $email_subject = "Media Request Form";
          
    $segment = $_POST['segment'];	
    $media = $_POST['media'];
    $print = $_POST['print'];
    $digital = $_POST['digital'];

    $company = $_POST['company'];
    $title = $_POST['title'];
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $customerType = $_POST['customerType'];

    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zipcode = $_POST['zipcode'];
    $country = $_POST['country'];

    $comments = $_POST['comments'];
     
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Company: ".clean_string($company)."\n";
    $email_message .= "Title: ".clean_string($title)."\n";
    $email_message .= "Name: ".clean_string($firstName)." ".clean_string($lastName)."\n";
    $email_message .= "Type: ".clean_string($customerType)."\n\n";

    $email_message .= "Email: ".clean_string($email)."\n";
	$email_message .= "Phone: ".clean_string($phone)."\n";
	$email_message .= "Address: ".clean_string($address)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
	$email_message .= "State: ".clean_string($state)."\n";
	$email_message .= "Zipcode: ".clean_string($zipcode)."\n";
	$email_message .= "Country: ".clean_string($country)."\n\n";
	
	$email_message .= "Segment: ".clean_string($segment[])."\n";
	$email_message .= "Media Type: ".clean_string($media)."\n";
	$email_message .= "Printed Copy: ".clean_string($print)."\n";
	$email_message .= "Digital Copy: ".clean_string($digital)."\n\n";
	
	$email_message .= "Comments: ".clean_string($comments)."\n\n";
     
     
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
 
<!-- include your own success html here -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
}
?>

My multiple selectors are not showing up correctly. They are only outputting "array" for the content.

 

Any help would be much appreciated!

 

Thanks!

Link to comment
Share on other sites

You define a variable from an array in the post data

 

$segment = $_POST['segment'];

 

But, you then use that variable here:

 

$email_message .= "Segment: ".clean_string($segment[])."\n";

 

You are passing $segment[] to the function. Using the brackets at the end of the variable is used to add a new value to the array. Plus, that function is simply returning the results of str_replace(). If you pass it an array, you will get an array as the result. You can't append an array to a string. The simplest solution (based upon my understanding of your code) would be to properly pass the variable to the function (i.e. without the []) and to modify the function to return a string.

 

 

function clean_string($string)
{
    $bad = array("content-type","bcc:","to:","cc:","href");
    $result = str_replace($bad,"",$string);
    if(is_array($result))
    {
        $result = implode(', ', $result);
    }
    return $result;
}

 

That will return the cleaned string OR if the value was an array it will return the array values concatenated with a comma and space as a string.

Link to comment
Share on other sites

You define a variable from an array in the post data

$segment = $_POST['segment'];

But, you then use that variable here:

$email_message .= "Segment: ".clean_string($segment[])."\n";

You are passing $segment[] to the function. Using the brackets at the end of the variable is used to add a new value to the array. Plus, that function is simply returning the results of str_replace(). If you pass it an array, you will get an array as the result. You can't append an array to a string. The simplest solution (based upon my understanding of your code) would be to properly pass the variable to the function (i.e. without the []) and to modify the function to return a string.

function clean_string($string)
{
    $bad = array("content-type","bcc:","to:","cc:","href");
    $result = str_replace($bad,"",$string);
    if(is_array($result))
    {
        $result = implode(', ', $result);
    }
    return $result;
}

That will return the cleaned string OR if the value was an array it will return the array values concatenated with a comma and space as a string.

Okay so I did that, and it worked for one test. Now, each time I test it, it only returns the last option in the list. Nothing before it.

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.