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

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'];
    }
    
    ?>
    
    已采纳该答案
    打赏 评论
  • douchongbang6011 2016-07-06 08:11

    Maybe instead of col-xs-5 you could write col-xs-2. Or write input field width. Also check out http://bootsnipp.com/ there are dozens of working bootstrap snippets.

    打赏 评论
  • dqblm40280 2016-07-11 11:10

    My problem hasn't been solved unfortunately. Is it possible to do something like this:

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

    So i tell the form after which value to break, and not after every value?

    It should be easier to combine my old phpmailer form with a bootstrap and ajax form, but im to inexperienced to get that fixed.

    打赏 评论

相关推荐 更多相似问题