从 PHP 联系表单收到的电子邮件显示文本 "array",其中应输入文本
Received email from PHP contact form displays the text "array" where the input text should be
我在我的网站上设置了一个联系页面,但是当我收到来自联系表单的电子邮件时,用户输入的数据丢失并被单词 "array" 取代。即
Received Email content:
Contact Form:
Name: Array
Email: Array
Message: Array
URL of contact page:
http://www.jsladvancedmarketing.co.uk/contact.html
我是 PHP 的新手,所以我不确定我是否忽略了一些对你们中的一些专业人士来说可能很简单的事情。
如有任何帮助,我们将不胜感激。我在下面包含了我的代码。提前谢谢你。
下面是我在 send.php 文件中的代码:
<?php
$ajax = (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$ajax = true;
//we do not allow direct script access
if (!$ajax) {
//redirect to contact form
echo "Please enable Javascript";
exit;
}
require_once "config.php";
//we set up subject
$mail->Subject = isset($_REQUEST['email_subject']) ? $_REQUEST['email_subject'] : "Message from site";
//let's validate and return errors if required
$data = $mail->validateDynamic(array('required_error' => $requiredMessage, 'email_error' => $invalidEmail), $_REQUEST);
//let's make sure we have valid data
//if (!$data['errors'] && (!isset($_REQUEST['js']) || $_REQUEST['js'] != 1)) {
//$data['errors']['global'] = 'Javascript is required. Please try again';
//}
if ($data['errors']) {
echo json_encode(array('errors' => $data['errors']));
exit;
}
$html = '<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<h2>' . $mail->Subject . '</h2>
';
foreach ($data['fields'] as $label => $val) {
$html .= '<p>' . $label . ': ' . $val . '</p>';
}
$html .= '</div></body>';
$mail->setup($html, $_REQUEST, array());
$result = array('success' => 1);
if (!$mail->Send()) {
$result['success'] = 0;
}
echo json_encode($result);
exit;
来自我的 HTML 联系页面的代码:
<div class="container section">
<div class="row">
<div class="col-sm-6">
<ul class="icon-list list-unstyled">
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-map-marker fa-lg"></i>
</span>
<span class="table-cell">
<a class="magnific-popup" data-type="iframe" href="https://www.google.co.uk/maps/place/350+Argyle+St,+Glasgow,+Glasgow+City+G2+8NE/@55.8592209,-4.2632052,17z/data=!4m2!3m1!1s0x4888469cfd25ce6b:0x71e8a78954d3a278">350 Argyle Street, Glasgow, G2 8ND</a>
</span>
</div>
</li>
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-phone fa-lg"></i>
</span>
<span class="table-cell">
0141 1234567
</span>
</div>
</li>
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-globe fa-lg"></i>
</span>
<span class="table-cell">
<a href="#">www.jsladvancedmarketing.co.uk</a>
</span>
</div>
</li>
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-envelope fa-lg"></i>
</span>
<span class="table-cell">
<a href="mailto:enquiries@jsladvancedmarketing.co.uk">enquiries@jsladvancedmarketing.co.uk</a>
</span>
</div>
</li>
</ul>
</div>
<div class="col-sm-6">
<div class="successMessage alert alert-success" style="display: none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Thank You!
</div>
<div class="errorMessage alert alert-danger" style="display: none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Ups! An error occured. Please try again later.
</div>
<form role="form" action="assets/form/send.php" method="post" class="contactForm validateIt"
data-email-subject="Contact Form" data-show-errors="true">
<div class="row padding-xs-top">
<div class="col-md-6 col-sm-6">
<div class="form-group form-group-float-label">
<input id="contact_name" placeholder="Name" required type="text" name="field[]" class="form-control input-lg">
<label for="contact_name">Name *</label>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group form-group-float-label">
<input id="contact_email" placeholder="Email" required type="email" name="field[]" class="form-control input-lg">
<label for="contact_email">Email *</label>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group form-group-float-label">
<textarea id="contact_message" placeholder="Message" class="form-control input-lg" rows="4" name="field[]" required></textarea>
<label for="contact_message">Message *</label>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<button type="submit" class="btn btn-primary btn-lg pull-right">Send Now</button>
</div>
</div>
</form>
</div>
</div>
</div>
这里可能有几个问题。您正在使用:
foreach ($data['fields'] as $label => $val) {
$html .= '<p>' . $label . ': ' . $val . '</p>';
}
$data['fields'] 看起来应该是 $data['field'] 按照表格中的名称?测试以确保您获得所需的所有数据的一个好方法是在 foreach 循环上方 var_dump($data);
并检查该表单数据中的内容。如果您得到一个空数组,那么您使用的名称不正确。
我在我的网站上设置了一个联系页面,但是当我收到来自联系表单的电子邮件时,用户输入的数据丢失并被单词 "array" 取代。即
Received Email content:
Contact Form:
Name: Array
Email: Array
Message: Array
URL of contact page: http://www.jsladvancedmarketing.co.uk/contact.html
我是 PHP 的新手,所以我不确定我是否忽略了一些对你们中的一些专业人士来说可能很简单的事情。
如有任何帮助,我们将不胜感激。我在下面包含了我的代码。提前谢谢你。
下面是我在 send.php 文件中的代码:
<?php
$ajax = (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$ajax = true;
//we do not allow direct script access
if (!$ajax) {
//redirect to contact form
echo "Please enable Javascript";
exit;
}
require_once "config.php";
//we set up subject
$mail->Subject = isset($_REQUEST['email_subject']) ? $_REQUEST['email_subject'] : "Message from site";
//let's validate and return errors if required
$data = $mail->validateDynamic(array('required_error' => $requiredMessage, 'email_error' => $invalidEmail), $_REQUEST);
//let's make sure we have valid data
//if (!$data['errors'] && (!isset($_REQUEST['js']) || $_REQUEST['js'] != 1)) {
//$data['errors']['global'] = 'Javascript is required. Please try again';
//}
if ($data['errors']) {
echo json_encode(array('errors' => $data['errors']));
exit;
}
$html = '<body style="margin: 10px;">
<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
<h2>' . $mail->Subject . '</h2>
';
foreach ($data['fields'] as $label => $val) {
$html .= '<p>' . $label . ': ' . $val . '</p>';
}
$html .= '</div></body>';
$mail->setup($html, $_REQUEST, array());
$result = array('success' => 1);
if (!$mail->Send()) {
$result['success'] = 0;
}
echo json_encode($result);
exit;
来自我的 HTML 联系页面的代码:
<div class="container section">
<div class="row">
<div class="col-sm-6">
<ul class="icon-list list-unstyled">
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-map-marker fa-lg"></i>
</span>
<span class="table-cell">
<a class="magnific-popup" data-type="iframe" href="https://www.google.co.uk/maps/place/350+Argyle+St,+Glasgow,+Glasgow+City+G2+8NE/@55.8592209,-4.2632052,17z/data=!4m2!3m1!1s0x4888469cfd25ce6b:0x71e8a78954d3a278">350 Argyle Street, Glasgow, G2 8ND</a>
</span>
</div>
</li>
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-phone fa-lg"></i>
</span>
<span class="table-cell">
0141 1234567
</span>
</div>
</li>
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-globe fa-lg"></i>
</span>
<span class="table-cell">
<a href="#">www.jsladvancedmarketing.co.uk</a>
</span>
</div>
</li>
<li>
<div class="display-table">
<span class="table-cell">
<i class="fa fa-envelope fa-lg"></i>
</span>
<span class="table-cell">
<a href="mailto:enquiries@jsladvancedmarketing.co.uk">enquiries@jsladvancedmarketing.co.uk</a>
</span>
</div>
</li>
</ul>
</div>
<div class="col-sm-6">
<div class="successMessage alert alert-success" style="display: none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Thank You!
</div>
<div class="errorMessage alert alert-danger" style="display: none">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Ups! An error occured. Please try again later.
</div>
<form role="form" action="assets/form/send.php" method="post" class="contactForm validateIt"
data-email-subject="Contact Form" data-show-errors="true">
<div class="row padding-xs-top">
<div class="col-md-6 col-sm-6">
<div class="form-group form-group-float-label">
<input id="contact_name" placeholder="Name" required type="text" name="field[]" class="form-control input-lg">
<label for="contact_name">Name *</label>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group form-group-float-label">
<input id="contact_email" placeholder="Email" required type="email" name="field[]" class="form-control input-lg">
<label for="contact_email">Email *</label>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group form-group-float-label">
<textarea id="contact_message" placeholder="Message" class="form-control input-lg" rows="4" name="field[]" required></textarea>
<label for="contact_message">Message *</label>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<button type="submit" class="btn btn-primary btn-lg pull-right">Send Now</button>
</div>
</div>
</form>
</div>
</div>
</div>
这里可能有几个问题。您正在使用:
foreach ($data['fields'] as $label => $val) {
$html .= '<p>' . $label . ': ' . $val . '</p>';
}
$data['fields'] 看起来应该是 $data['field'] 按照表格中的名称?测试以确保您获得所需的所有数据的一个好方法是在 foreach 循环上方 var_dump($data);
并检查该表单数据中的内容。如果您得到一个空数组,那么您使用的名称不正确。