来自HTML表单提交的php的错误消息

时间:2018-05-02 19:21:03

标签: javascript php html ajax forms

我在HTML页面中使用此表单,它可以在2个站点上运行。我复制了以下代码来创建contact.html和contact.php

我已经尝试了2天,让它在新网站上运行并不断收到错误消息:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in D:\Hosting\7131065\html\contact.php on line 31

随着PHP的响应: Contact form successfully submitted.

然而'''并没有在其他网站上抛出错误。删除'\'会导致不同的错误。

响应应该在contact.html中显示(通过AJAX),但响应显示页面contact.php

HTML表单(包括链接)

CSS <!-- minified CSS -->
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <!-- TSA Styles -->
    <link href="css/tsa-styles.css" rel="stylesheet">
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Crimson+Text:400,600|Open+Sans:400,400i,600" rel="stylesheet">

HTML
<!--Form-->
                    <form id="contact-form" method="post" action="contact.php">
                    <div class="message"></div>
                        <div class="controls">
                            <div class="form-group">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                    <input id="form_name" type="text" name="yourname" class="form-control" placeholder="Your Name - Required*" required="required" data-error="Your name is required.">
                                </div>
                                <div class="help-block with-errors"></div>
                            </div>
                            <div class="form-group email">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
                                    <input id="form_email" type="email" name="email" class="form-control" placeholder="Your Email - Required*" required="required" data-error="Valid email is required.">
                                </div>
                                <div class="help-block with-errors"></div>
                            </div>
                    <div class="form-group">
                    <label class="how">How did you hear about us?</label>                                                                                       
                    <select class="form-control" name="hear">
                          <option>Please click to make selection</option>
                          <option>Web Search</option>
                          <option>Facebook</option>
                          <option>Friend</option>
                          <option>Support Group</option>
                          <option>Print Ad</option>
                          <option>Bar</option>
                          <option>Other</option>
                        </select>
                    </div>
                    <div class="form-goup">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span><textarea id="form_message" name="message" class="form-control" rows="8" placeholder="Your Message - Required*" required data-error="Message is required."></textarea>
                        </div>
                        <div class="help-block with-errors"></div>
                    </div>
                        <input type="submit" class="btn btn-default tsa" value="Send Message">
                    </div>                      
                </form>
JS

    <!-- jQuery for Bootstrap's JavaScript  -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

contact.php

<?php
// Set up
$from = 'new-contact@tsagl.org/';
$sendTo = 'contact-manager@tsagl.org';
$subject = 'New message from Conact page';
$fields = array('yourname' => 'Name', 'email' => 'Email', 'hear' => 'How did you hear about us?', 'message' => 'Message'); 
$okMessage = 'Contact form successfully submitted.';
$errorMessage = 'There was an error while submitting the form. Please try again later';

// Actions

if (empty($_POST['message'])) {
    exit;
}

try
{
    $emailText = "New message from Contact Page\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'];
}

任何回复都非常感谢!

0 个答案:

没有答案