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条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?