0

I have a questionnaire with tons of input fields. So far I started a PHP script to send it to my email which goes as follows.

<?php 

$First_Name= $_POST['First_Name'];
$Last_Name= $_POST['Last_Name'];
$E_Mail= $_POST['E_Mail'];
$FB_Name= $_POST['FB_Name'];
$Twitter_Name= $_POST['Twitter_Name'];
$Country= $_POST['Country'];
$State_Province= $_POST['State_Province'];
$City= $_POST['City'];
$Phone_01= $_POST['Phone_01'];
$Phone_02= $_POST['Phone_02'];
$Phone_03= $_POST['Phone_03'];
$How_did_you_hear_about_my_Company= $_POST['How_did_you_hear_about_my_Company'];
$Operating_your_Business= $_POST['Operating_your_Business'];
$Name_for_your_Business= $_POST['Name_for_your_Business'];
$Whats_the_name_of_your_Business= $_POST['Whats_the_name_of_your_Business'];
$What_type_of_Business= $_POST['What_type_of_Business'];
$Type_of_Business_you_run= $_POST['Type_of_Business_you_run'];
$How_long_you_been_building= $_POST['How_long_you_been_building'];
$How_long_you_been_in_business= $_POST['How_long_you_been_in_business'];
$What_do_you_plan_to_offer= $_POST['What_do_you_plan_to_offer'];
$What_do_you_offer= $_POST['What_do_you_offer'];
$List_all_products_you_offer['List_all_products_you_offer'];

$from = 'From: Your_New_Client';
$to = '[email protected]';
$subject = 'A New Questionnaire';

$body = "Name: $First_Name $Last_Name
\n E-Mail: $E_Mail
\n FB Name: $FB_Name
\n Twitter Name: $Twitter_Name
\n Country: $Country
\n State/Province: $State_Province
\n City: $City
\n Phone Number: $Phone_01 $Phone_02 $Phone_03
\n How did you hear about my Company?:\n\n $How_did_you_hear_about_my_Company
\n Where are you in terms of opperating your Business?: $Operating_your_Business
\n Have you come up with a name for your Business?: $Name_for_your_Business
\n What's the name of your Business?: $Whats_the_name_of_your_Business
\n What type of Business will you be running?: $What_type_of_Business
\n What type of Business do you run?: $Type_of_Business_you_run
\n How long have you been Building your Business?: $How_long_you_been_building
\n How long have you been in Business?: $How_long_you_been_in_business
\n What do you plan to offer?: $What_do_you_plan_to_offer
\n What do you offer?: $What_do_you_offer
\n List of products: $List_all_products_you_offer 
";

?>

<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

This works properly, but I want to know how I can tell the PHP to automatically collect all the data without me having to go one by one with each element (there's a lot more than what's represented above).

I came across a script that does this for storing to an SQL database, but not one for email. I tried using the one for SQL but was running into issues because of how many columns it creates.

Aside from this I have another task to tackle with allowing users to add fields which will vary from user to user, so I need the $POST method to be flexible so it can send the results. With the way I have it coded so far, there's no way for extra fields to be posted when the user adds them.

I searched through the site and so far this is the only thing I've come across that relates to this issue, but don't really understand what's being said. PHP: Possible to automatically get all POSTed data?

If that is indeed the answer to what I'm trying to do could someone please break it down a little more and tell me how it works?

1 Answer 1

3

You can try like this:

$body="";
foreach ($_POST as $key => $value) {
    $body.= str_replace("_"," ",$key).": ". $value."\n";
}

provided that $key holds the descriptive information which you need with words separated by underscore (_) as in your question is.

Sign up to request clarification or add additional context in comments.

2 Comments

SPLENDID IT WORKS!!!!! The only thing is the <br/> shows up in the email rather than starting a new line, so it's all one long paragraph.
replace <br/> with \n. If you want to use <br/>, you need to specify content-type header

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.