如何在PHP中使用$POST自动从HTML5输入中收集数据并通过电子邮件发送


How can I use $POST in PHP to automatically collect data from HTML5 inputs and send through e-mail?

我有一份包含大量输入字段的调查问卷。到目前为止,我启动了一个PHP脚本,将其发送到我的电子邮件中,如下所示。

<?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 = 'Optiqvision@gmail.com';
$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>'; 
}
}
?>

这可以正常工作,但我想知道如何告诉PHP自动收集所有数据,而不必逐个处理每个元素(比上面所示的要多得多)。

我遇到了一个用于存储到SQL数据库的脚本,但没有用于电子邮件的脚本。我尝试将它用于SQL,但由于它创建了多少列而遇到问题。

除此之外,我还有另一项任务要处理,即允许用户添加因用户而异的字段,因此我需要$POST方法灵活,以便它可以发送结果。到目前为止,按照我的编码方式,当用户添加额外的字段时,没有办法发布它们。

我在网站上搜索了一下,到目前为止,这是我遇到的唯一一件与这个问题有关的事情,但我真的不明白在说什么。PHP:是否可以自动获取所有POSTed数据?

如果这真的是我想做的事情的答案,有人能再分解一下,告诉我它是如何工作的吗?

您可以这样尝试:

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

假设$key包含您需要的描述性信息,这些信息用下划线(_)分隔,就像您的问题一样。