【问题标题】:AJAX/PHP form not sending informationAJAX/PHP 表单不发送信息
【发布时间】:2014-07-02 12:23:14
【问题描述】:

嘿,伙计们,我有一个表单,它可以在不重新加载页面的情况下发送消息。我在这里使用了本教程:http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/ 并对其进行了调整以满足我的需要。这是php:

<?php
session_start();
// Define some constants
define( "RECIPIENT_NAME", "John Smith" );
define( "RECIPIENT_EMAIL", "myemail@email.com" );
define( "EMAIL_SUBJECT", "Visitor Message" );

// Read the form values and define variables
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "",    $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
$honeypot = $_POST['url'];
$code = $_POST['code'];

//if the honeypot is filled out, dont send the form
if (!empty($honeypot)){
    $success = false; 
}
// If all values exist and the code matches, send the email 
else if ( $senderName && $senderEmail && $message && $code == $_SESSION['random_code'] ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}



// Return an appropriate response to the browser
if ( isset($_POST["ajax"]) ) {
 echo $success ? "success" : "error";
} else {
?>
<html>
 <head>
   <title>Thanks!</title>
 </head>
 <body>
 <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
 <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
 <p>Click your browser's Back button to return to the page.</p>
</body>
  </html>
<?php
}
?>

这里是javascript:

    function submitForm() {
 var contactForm = $(this);

//submit the form to the PHP script via Ajax
$('#sendingMessage').fadeIn();

$.ajax( {
  url: contactForm.attr( 'action' ) + "?ajax=true",
  type: contactForm.attr( 'method' ),
  data: contactForm.serialize(),
  success: submitFinished
} );


// Prevent the default form submission occurring
return false;
}

function submitFinished( response ) {
 response = $.trim( response );
 $('#sendingMessage').fadeOut();

 if ( response == "success" ) {

// Form submitted successfully:
// 1. Display the success message
// 2. Clear the form fields

$('#successMessage').fadeIn().delay(5000).fadeOut();
$('#name').val( "" );
$('#email').val( "" );
$('#message').val( "" );

 } else {

// Form submission failed: Display the failure message,
$('#failureMessage').fadeIn().delay(5000).fadeOut();
}
}

当用户单击发送按钮并检查所有字段都已填写后,该功能将运行。我认为它在它说 if(isset($_POST['ajax'])) 的 php 末尾跳了起来,因为我一直被重定向到 html 回退,就好像我没有启用 javascript(我通过道路)。你可以在这里看到一个工作示例:mattsandersdesign.com/test/contact2.html 提前致谢!

【问题讨论】:

    标签: php ajax jquery


    【解决方案1】:

    即使所有验证检查都通过了,您也必须return false。否则表单只会提交。
    另外,你甚至打电话给submitForm() 吗?

    【讨论】:

    • 我在 submitForm() 的底部得到了 return false 但也许我必须在该函数之外再做一次。是的,我有一个函数,当用户单击提交按钮时运行,它检查是否所有字段都已写入,然后在此之后运行 submitForm()
    【解决方案2】:

    好吧,我终于想通了。首先,我在错误的时间调用了 return false 。我需要在 submitForm 函数之外运行它。其次,我在错误的地方有几个变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2014-11-16
      • 2014-02-16
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 2018-10-11
      相关资源
      最近更新 更多