douou6696 2015-03-12 19:40
浏览 56

在子目录中使用时,所有表单值都为空

I have a contact us page in a website made for a client, which I am hosting from a sub-directory of my main website. When someone sends a message from "contact us" page, its sent to the website owner's email, but the values are empty. But when I tested the same two pages from the root directory they worked fine.

Following is the code.

HTML PAGE

<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="fname" type="text" class="form-control" required="required" placeholder="First Name">
        </div>
        <div class="form-group">
            <input name="lname" type="text" class="form-control" required="required" placeholder="Last 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" id="message" required="required" class="form-control" rows="8" placeholder="Message"></textarea>
    </div>
</div>
</form>

PHP FILE

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

$fname= @trim(stripslashes($_POST['fname'])); 
$lname = @trim(stripslashes($_POST['lname'])); 
$name = $fname.' '.$lname;
$email = @trim(stripslashes($_POST['email'])); 
$message = @trim(stripslashes($_POST['message'])); 

$email_from = $email;
$email_to = 'my.email@gmail.com';

$body = 'Name: ' . $name . "

" . 'Email: ' . $email . "

" . 'Message: ' . $message;

$success = @mail($email_to, 'An email from website\'s form', $body, 'From: <'.$email_from.'>');

echo json_encode($status);
die;

When I put both of these files in the root (public_html) through CPanel's file manager, they work fine, but if I put them in the sub-folder than the values are empty.

Following are the examples.

Example 1, when sent from the sub-folder
enter image description here



Example 2, when sent form the main directory
enter image description here

  • 写回答

1条回答 默认 最新

  • dongqiao9394 2015-05-05 03:09
    关注

    check your file "main.js" and replace:

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

    for

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

    the complete "main.js" is:

    jQuery(function($) {
    
        //Ajax contact
        var form = $('.contact-form');
            form.submit(function () {
                $this = $(this);
                $.post($(this).attr('action'), $('.contact-form').serialize(), 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
    
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器