Post Data is not coming

夙愿已清 提交于 2019-12-11 07:37:26

问题


Update Two: After doing some testing, I found that it does not work when jQuery is loaded in the page. My page relies on jQuery, so what should I do?

Update: You can see the problematic page here here, and the working test page here.


I'm making a contact form, and it looks like this

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="../sendemail.php">
              <div class="row-fluid">
                <div class="span5">
                    <label>First Name</label>
                    <input name="first" type="text" class="input-block-level" required="required" placeholder="Your First Name">
                    <label>Last Name</label>
                    <input name="last" type="text" class="input-block-level" required="required" placeholder="Your Last Name">
                    <label>Email Address</label>
                    <input name="email" type="text" class="input-block-level" required="required" placeholder="Your email address">
                </div>
                <div class="span7">
                    <label>Message</label>
                    <textarea name="message" id="message" required class="input-block-level" rows="8"></textarea>
                </div>

            </div>
            <button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
            <p> </p>

        </form>

Nothing too fancy. sendemail.php looks like this

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

$name = @trim(stripslashes($_POST['first']." ".$_POST['last'])); 
$email = @trim(stripslashes($_POST['email'])); 
$subject = "Monarc - Message From ".$name;
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'xxx@xxx.xxx';

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

The email comes through, but the postdata isn't coming through in the email. It says (unknown sender) as the sender, and the body looks like this:

Name:

Email:

Subject: Monarc - Message From

Message:

What am I doing wrong?

Thanks!


回答1:


To post the form value you must use input type submit instead of button type

change:

<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>

to:

<input type="submit" class="btn btn-primary btn-large pull-right" value="Send Message">



回答2:


supplementary information, this form uses the following javascript file:

jQuery(function($) {

//Ajax contact 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; });

//Goto Top $('.gototop').click(function(event) {
event.preventDefault(); $('html, body').animate({ scrollTop: $("body").offset().top }, 500); }); //End goto top

});

Searching on another forum I found how to fix this. You only need add the following code in the php file:

$headers = "From: mail <$email_from>\r\n";
$headers .= "MIME-Version: 1.0" ."\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

and remove "@" of $success = @mail($email_to, $subject, $body,'From: <'.$email_from.'>');

In "main.js" file your code isn't supplying any data in the .post() method. you need to add $(this).serialize() to get all the form fields/values.

untested but should work -

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

Hope that helps resolve the issue



来源:https://stackoverflow.com/questions/23193156/post-data-is-not-coming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!