【问题标题】:Email Form: $_POST is empty after form submit电子邮件表单:表单提交后 $_POST 为空
【发布时间】:2016-02-05 20:23:39
【问题描述】:

我一直在论坛上。这不起作用的一般原因是您需要一个 NAME 属性,而不是一个 ID。我有一个名称属性,但在提交时我仍然得到空的 $_POST 字段。我是一个初学者,已经花了 6 个小时试图弄清楚。这可能很简单。请帮忙。

我从 ShapeBootstrap 获得了这个免费模板:https://shapebootstrap.net/item/1524963-evento-free-music-event-template

我的html:

<form action="sendemail.php" method="post" name="contact-form" class="contact-form" id="main-contact-form">
    <div class="form-group">
        <input name="name" type="text" id="name" required="required" class="form-control" placeholder="Name">
    </div>
    <div class="form-group">
        <input type="email" name="email" id="email" class="form-control" required="required" placeholder="Email ID">
    </div>
    <div class="form-group">
        <textarea name="message" id="message" required class="form-control" rows="10" placeholder="Enter your message"></textarea>
    </div>                        
    <div class="form-group">
        <button type="submit" class="btn btn-primary pull-right">Send</button>
    </div>
    <div>&nbsp;</div>
</form>

我的 php:

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Thank you for contact us. As early as possible we will contact you'
    );

    $name = ($_POST["name"]); 
    $email = ($_POST['email']); 
    $subject = "Website Message";
    $message = ($_POST['message']); 

    $email_from = $email;
    $email_to = 'adam.wilson45@yahoo.com';//replace with your email

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

JavaScript:

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

	$( window ).resize(function() {
		menuToggle();
	});

	$('.main-nav ul').onePageNav({
		currentClass: 'active',
	    changeHash: false,
	    scrollSpeed: 900,
	    scrollOffset: 0,
	    scrollThreshold: 0.3,
	    filter: ':not(.no-scroll)'
	});

});

非常感谢您!

【问题讨论】:

  • print_r($_POST); 显示什么,什么都没有?
  • 您的代码容易受到标头注入攻击。我建议使用邮件库。喜欢swift
  • 不是布尔表达式,不影响值
  • 你说得对,米海,我查过这里。然后,我会像 chris85 建议的那样检查 print_r($_POST) 。如果它没有返回任何内容,则意味着您将表单提交到错误的页面
  • 提交表单的代码在哪里?如果您返回 JSON,您可能正在使用 AJAX。

标签: javascript php html forms post


【解决方案1】:

您没有在$.post 脚本中传递任何数据。 将$.post($(this).attr('action'), function(data) { 更改为$.post($(this).attr('action'), form.serialize(), function(data) {form.serialize() 位将表单数据的对象传递给 PHP 脚本。

请参阅http://api.jquery.com/jquery.post/ 了解更多信息。

【讨论】:

    【解决方案2】:

    谢谢@putvande!您的回答为我指明了正确的方向!添加 form.serialize 很接近。我们需要添加 $this。就在它之前。我希望我在 8 小时前和你谈过。 回答: 改变

    $.post($(this).attr('action'), function(data) {

    $.post($(this).attr('action'), $this.serialize(), function(data) {

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 2015-03-28
      • 2016-07-26
      • 1970-01-01
      相关资源
      最近更新 更多