Jorus Jorus - 4 months ago 5
PHP Question

Bootstrap 3 form and php - mutliple inputs on same line in e-mail message

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 \n 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\n=============================\n";

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

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

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'];
}

?>

Answer

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\n";
}
}

For this:

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

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\n=============================\n";

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

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'];
}

?>
Comments