dsz1966 2013-10-26 01:30
浏览 19
已采纳

表单保持投掷“不允许直接访问此页面。”无法找到错误

I have been writing a form in JavaScript, PHP and HTML. I seem to be having a problem with it, and i feel its a small one. I've been working hard for hours and the problem still exists.

The form I am trying to get working is located here: http://www.cyberbytesdesign.com/s/ama/contact.php

Here's the JavaScript being used:

$(function() {
    // Setup any needed variables.
    var $form   = $('#contact-form'),
        $loader = '<img src="img/loader.gif" height="20" width="20" alt="Loading..." />';

    $form.append('<div id="response" class="hidden">');
    var $response = $('#response');

    // Do what we need to when form is submitted.
    $form.on('click', 'input[type=submit]', function(e){

        // Hide any previous response text and show loader
        $response.hide().html( $loader ).show();

        // Make AJAX request 
        $.post('resources/script/contact-send.php', $form.serialize(), function( data ) {

            // Show response message
            $response.html( data );

            // Scroll to bottom of the form to show respond message
            var bottomPosition = $form.offset().top + $form.outerHeight() - $(window).height();

            if( $(document).scrollTop() < bottomPosition )
                $('html, body').animate({ scrollTop : bottomPosition });

            // If form has been sent succesfully, clear it
            if( data.indexOf('success') !== -1 )
                $form.find('input:not(input[type="submit"], input[type="reset"]), textarea, select').val('').attr( 'checked', false );

        });

        // Cancel default action
        e.preventDefault();
    });
});/*END FUNCTION*/

Here's the PHP being used when the form is submitted (Which is throwing the error with the "else { die('Direct access to this page is not allowed.');"

    // Get email address
    $email_address = 'test@gmail.com';

    // Ensures no one loads page and does simple spam check
    if( isset($_POST['name']) && empty($_POST['spam-check']) ) {

        // Declare our $errors variable we will be using later to store any errors
        $error = '';

        // Setup our basic variables
        $input_name = strip_tags($_POST['name']);
        $input_email = strip_tags($_POST['email']);
        $input_subject = strip_tags($_POST['subject']);
        $input_message = strip_tags($_POST['message']);

        // We'll check and see if any of the required fields are empty
        if( strlen($input_name) < 2 ) $error['name'] = '<label class="inline" for="contact-name">Please enter your <b>Name</b></label>.';
        if( strlen($input_message) < 5 ) $error['message'] = '<label class="inline" for="contact-message">Please leave a <b>Message</b></label>.';

        // Make sure the email is valid
        if( !filter_var($input_email, FILTER_VALIDATE_EMAIL) ) $error['email'] = '<label class="inline" for="contact-email">Please enter a valid <b>Email Address</b></label>.';

        // Set a subject & check if custom subject exist
        $subject = "Customer: Contact Us";
        if( $input_subject ) $message = "Subject: $input_subject
";
        $message .= "
Message: $input_message
";
        $message .= "
---
This email was sent by $input_name from $input_email";

        // Now check to see if there are any errors 
        if( !$error ) {

            // No errors, send mail using conditional to ensure it was sent
            if( mail($email_address, $subject, $message, "From: $input_email") ) {
                echo '<p class="success"><b>EMAIL SENT SUCCESSFULLY.</b><br />' . "Dear $input_name, " . 'Thank You for contacting RAC-Engineering! Please allow us <b>24-48</b> hours to review your request and get back to you. If you need a response sooner, please contact us via telephone at (716) 695-2269.<br /><br /><b>Please verify that this is your correct Email Address:</b><br />' . "Email Address: <i>$input_email</i>" . '<br /><br /><span class="red"><b>PLEASE NOTE:</b></span><br /> If we do not respond to your request within a reasonable amount of time, please give us a call as there may have been an error on our end with your request.</p>';
            } else {
                echo '<p class="error">There was a problem sending your email! Please give us a call at (716) 695-2269 as there seems to be an error on our end with the form.</p>';
            }

        } else {

            // Errors were found, output all errors to the user
            $response = (isset($error['name'])) ? $error['name'] . "<br /> 
" : null;
            $response .= (isset($error['email'])) ? $error['email'] . "<br /> 
" : null;
            $response .= (isset($error['message'])) ? $error['message'] . "<br /> 
" : null;

            echo "<p class='error'>$response</p>";

        }

    } else {

        die('Direct access to this page is not allowed.');

    }

lastly, here is the HTML for the form:

        <div id="contact-form" class="row m-top-10">
          <form action="resource/script/question-send.php" method="post">
            <div class="column-4 required">
              <label class="no-margin" for="request-name"><strong>Name</strong></label>
              <input type="text" name="name" value="" id="request-name" placeholder="First / Last Name" required>
            </div>
            <div class="column-4 required">
              <label for="request-email"><strong>Email Address</strong></label>
              <input class="required" type="email" name="email" value="" id="request-email" placeholder="email@example.com" required>
            </div>
            <div class="column-4">
              <label for="request-subject"><strong>Subject</strong></label>
              <input type="text" name="subject" value="" id="request-subject" placeholder="e.g. Question">
            </div>
            <div class="row column-8 first-column m-top-30-desktop required">
              <label for="request-message"><strong>Your Message</strong></label>
              <textarea class="required" name="message" id="request-message" placeholder="Please include any additional information pertaining to your inquiry." required></textarea>
            </div>
            <div class="hidden">
              <label for="request-spam-check">Do not fill out this field:</label>
              <input name="spam-check" type="text" value="" id="request-spam-check" />
            </div>
            <div class="column-4 m-top-60 m-top-20-mobile">
              <input class="large strong btn btn-blue" type="submit" value="Submit">
            </div>
            <div class="column-4 textcenter last">
              <input class="large strong blue" type="reset" value="Reset">
            </div>
          </form>
          <span class="row"></span>
        </div>

Thank you SO MUCH, I have been driving myself crazy over this, it's got to be something very minor that a new pair of eyes will probably be able to catch.

  • 写回答

1条回答 默认 最新

  • dongxibeng5324 2013-10-26 01:35
    关注

    Instead of writing:

    if( isset($_POST['name']) && empty($_POST['spam-check']) ) {
    

    Try writing:

    if (isset($_POST['name']) && trim($_POST['spam-check']) === "") {
    

    I think that might help.

    UPDATE: I know why.

    I took a look at your website's code:

    <div id="contact-form" class="row m-top-10">
      <form action="resource/script/question-send.php" method="post">
        <div class="column-4 required">
          <label class="no-margin" for="request-name"><strong>Name</strong></label>
          <input type="text" name="name" value="" id="request-name" placeholder="First / Last Name" required>
        </div>
        <div class="column-4 required">
          <label for="request-email"><strong>Email Address</strong></label>
          <input class="required" type="email" name="email" value="" id="request-email" placeholder="email@example.com" required>
        </div>
        <div class="column-4">
          <label for="request-subject"><strong>Subject</strong></label>
          <input type="text" name="subject" value="" id="request-subject" placeholder="e.g. Question">
        </div>
        <div class="row column-8 first-column m-top-30-desktop required">
          <label for="request-message"><strong>Your Message</strong></label>
          <textarea class="required" name="message" id="request-message" placeholder="Please include any additional information pertaining to your inquiry." required></textarea>
        </div>
        <div class="hidden">
          <label for="request-spam-check">Do not fill out this field:</label>
          <input name="spam-check" type="text" value="" id="request-spam-check" />
        </div>
        <div class="column-4 m-top-60 m-top-20-mobile">
          <input class="large strong btn btn-blue" type="submit" value="Submit">
        </div>
        <div class="column-4 textcenter last">
          <input class="large strong blue" type="reset" value="Reset">
        </div>
      </form>
      <span class="row"></span>
    </div>
    

    And in the JavaScript, you wrote this:

    var $form   = $('#contact-form')
    

    But you realize that #contact-form is a div, not the form inside it.

    Change it to this:

    var $form = $('#contact-form form:first')
    

    That should fix everything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法