donglian6625 2016-07-06 06:53
浏览 81
已采纳

Bootstrap 3表单和php - 电子邮件中同一行的多个输入

I've searched for hours but couldn't really find an answer about this.

What i would like to do is a large field and smaller field next to each other, eg. for an separated address or phone number, like Blackstreet 27 or 123 - 456789. It needs to be printed as one in the message i receive in my mailbox. Everything is going about two or multiple fields on a webpage, but not about the final result. Same name or id doesn't work.

<div class="row">
   <div class="col-xs-6">
     <div class="form-group">
        <label for="form_area">Area code *</label>
        <input id="form_area" maxlength="4" type="tel" name="area" class="form-control" placeholder="Area code required *" required="required" data-error="Your area code is required.">
        <div class="help-block with-errors"></div>
     </div>
   </div>
   <div class="col-xs-6">
     <div class="form-group">
        <label for="form_phone">Phone number *</label>
        <input id="form_phone" type="tel" pattern="^[_0-9]{1,}$" name="phone" class="form-control" placeholder="Phone number required *" required="required" placeholder="Your phone number is required.">
        <div class="help-block with-errors"></div>
      </div>
   </div>
</div>

EDIT: I'm sorry. I think it has something to do with the php file. The brings it all to break. When i remove it, it comes all to one line, and only some field have to come in one line.

code:

<?php

// configure
$from = 'emailadres'; 
$sendTo = 'emailadres';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'businessname' => 'Businessname', 'phone' => 'Phonenumber', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email
$okMessage = 'I'll contact you asap!';
$errorMessage = 'Something went wrong. try again later';

// let's do the sending

try
{
$emailText = "You have new message from contact form
=============================
";

foreach ($_POST as $key => $value) {

    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value
";
    }
}

mail($sendTo, $subject, $emailText, "From: " . $from);

$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&  strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
else {
echo $responseArray['message'];
}

?>
  • 写回答

3条回答 默认 最新

  • doujinge9648 2016-07-11 23:41
    关注

    I've got the answer finally! (Thanks to Ondrej the author of the script).

    This part had to be replaced:

    foreach ($_POST as $key => $value) {
    
    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value
    ";
    }
    }
    

    For this:

    $emailText .= "Name: " . $_POST['name']. "
    ";
    $emailText .= "Businessname: " . $_POST['businessname']. "
    ";
    $emailText .= "Address: " . $_POST['street']." ". $_POST['number']. "
    ";
    $emailText .= "Phonenumber: " . $_POST['phone']. "
    ";
    $emailText .= "E-mail: " . $_POST['email']. "
    ";
    $emailText .= "Message: " . $_POST['message']. "
    ";
    

    And this line can be removed in the configure part:

    $fields = array('name' => 'Name', 'businessname' => 'Businessname', 'phone' => 'Phonenumber', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email
    

    So the complete php file now looks like this:

    <?php
    
    // configure
    $from = 'emailadres'; 
    $sendTo = 'emailadres';
    $subject = 'New message from contact form';
    $okMessage = 'I'll contact you asap!';
    $errorMessage = 'Something went wrong. try again later';
    
    // let's do the sending
    
    try
    {
    $emailText = "You have new message from contact form
    =============================
    ";
    
    $emailText .= "Name: " . $_POST['name']. "
    ";
    $emailText .= "Businessname: " . $_POST['businessname']. "
    ";
    $emailText .= "Address: " . $_POST['street']." ". $_POST['number']. "
    ";
    $emailText .= "Phonenumber: " . $_POST['phone']. "
    ";
    $emailText .= "E-mail: " . $_POST['email']. "
    ";
    $emailText .= "Message: " . $_POST['message']. "
    ";
    
    mail($sendTo, $subject, $emailText, "From: " . $from);
    
    $responseArray = array('type' => 'success', 'message' => $okMessage);
    }
    catch (\Exception $e)
    {
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
    }
    
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&   strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);
    
    header('Content-Type: application/json');
    
    echo $encoded;
    }
    else {
    echo $responseArray['message'];
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题