简体   繁体   中英

Received email from PHP contact form displays the text “array” where the input text should be

I have setup a contact page on my site but when I receive emails from the contact form the user input data is missing and replaced by the word "array". ie

Received Email content:

Contact Form:

Name: Array

Email: Array

Message: Array

URL of contact page: http://www.jsladvancedmarketing.co.uk/contact.html

I am a novice when it comes to PHP so I am unsure if I have overlooked something that may be simple to some of you pro's.

Any help with this would be greatly appreciated. I have included my code below. Thank you in advance.

Below is my code from send.php file:

<?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;

The code from my HTML contact page:

<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">&times;</button>
                Thank You!
            </div>
            <div class="errorMessage alert alert-danger" style="display: none">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</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>

There might be a couple problems here. You are using:

foreach ($data['fields'] as $label => $val) {
    $html .= '<p>' . $label . ': ' . $val . '</p>';
}

$data['fields'] looks like it should be $data['field'] as per the name in the form? A good way to test to make sure you are getting all the data you want is to var_dump($data); just above the foreach loop and check to see whats in that form data. If you get an empty array then you are using an incorrect name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM