由于jQuery无法接收PHP表单发布数据 [英] PHP form post data not being received due to jQuery

查看:118
本文介绍了由于jQuery无法接收PHP表单发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送电子邮件的基本表格.它正在发送电子邮件,但未传递表单中的数据.我缺少让数据发布的东西了吗?

I have a basic form to send an email. It is sending the email, but not passing the data from the form. Is there something I am missing to allow the data to POST?

用于发送电子邮件的HTML表单:

HTML form to send email:

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
    <div class="row">
        <div class="col-sm-5">
            <div class="form-group">
                <input name="name" type="text" class="form-control" required="required" placeholder="Name">
            </div>
            <div class="form-group">
                 <input name="email" type="text" class="form-control" required="required" placeholder="Email address">
            </div>
            <div class="form-group">
                 <button type="submit" class="btn btn-primary btn-lg">Send Message</button>
            </div>
        </div>
        <div class="col-sm-7">
            <textarea name="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
        </div>
    </div>
</form>

sendemail.php中的PHP代码

PHP code in sendemail.php

<?php
header('Content-type: application/json');
$status = array(
    'type'=>'success',
    'message'=>'Email sent!'
);

$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email'])); 
$subject = "Example Contact";
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'admin@example.com';

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: ' . $email_from);

echo json_encode($status);
die;


更新: 当包含class ="contact-form"时,似乎在js/jquery中存在问题.如果我将其保留,则将发送变量.


UPDATE: It looks like there is a problem in js/jquery when the class="contact-form" is included. If I leave it out, the variables are sent.

这是表单的jquery代码:

Here is the jquery code for the form:

//contact form
var form = $('.contact-form');
form.submit(function () {
    $this = $(this);
    $.post($(this).attr('action'), function(data) {
        $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
    },'json');
    return false;
});

以下是一些相关帖子:
PHP空白电子邮件
联系表单不会提交数据
js删除提交的表单数据
PHP脚本发送邮件但不显示表单数据
发布数据没有到来

推荐答案

您正在呼叫 jquery.post() ,但您只说发布到哪里以及成功后该怎么做.您要发布什么?第二个参数必须是您要发布的数据:

You're calling jquery.post() but you only say where to post to, and what to do when it succeeds. What are you posting? The second parameter needs to be the data you're posting:

$.post($(this).attr('action'), $(this).serialize(), function(data) {
    $this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
},'json');

这篇关于由于jQuery无法接收PHP表单发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆