为什么我一直从textarea字段中获取空值?

时间:2016-08-19 15:41:28

标签: php html ajax forms

注意:尚未解决。

我有一个PHP循环内部的表单,我使用Ajax将表单值传递给另一个PHP页面,因此我可以将它们保存到我的数据库中。

当我第一次提交表单时,一切顺利,表单提交没有任何错误,但是当我尝试再次提交时,它确实提交数据库中的数据,textarea字段除外。它通过和空场而不是。我不知道为什么。

这是代码的一部分(并没有粘贴整个代码,因为它很长):

<?php 
    while($FR = $FetchReply->FETCH(PDO::FETCH_ASSOC))
    { 
        $user_id = $FR['replier_id'];
        $FetchReplyerInfo = $conn->prepare("SELECT * FROM users WHERE user_id = ?");
        $FetchReplyerInfo->execute([$user_id]);
        $FRI = $FetchReplyerInfo->fetch(PDO::FETCH_ASSOC);
        $username = $FRI['username'];
        ?>
            <form action="" method="POST" onsubmit="return SubmitComment('reply', <?php echo $FUI['user_id']; ?>, null, <?php echo $FC['id']; ?>);">
                <div class="form-group">
                    <textarea class="form-control" id="reply" rows="3" placeholder="* Your comment here" required></textarea>
                </div>
                <button type="submit" class="btn btn-theme btn-md">Reply</button>
            </form>
        <?php 
    } ?>

这是完整的代码:

    <?php
    include_once("$_SERVER[DOCUMENT_ROOT]/jellygif/lib/dbconnect.php");
    session_start();
    if(isset($_SESSION['Member']))
        {
        include_once("including_php.php");  

        }
    else 
        {

        }
    if(isset($_GET['imgname']))
        {
        include_once("including_php2.php"); 

        }
    else 
        {
            echo "error";
        }


?>
<!DOCTYPE html>
<html lang="en">
<head>


<!-- Theme skin -->
<link id="t-colors" href="/jellygif/skins/default.css" rel="stylesheet" />
<!-- boxed bg -->
<link id="bodybg" href="/jellygif/bodybg/bg1.css" rel="stylesheet" type="text/css" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
<script>
    function SubmitComment(type, userid, imgid, commentid)
    {
        if(type == "reply") {
            var comment = document.getElementById('reply').innerHTML
            $.ajax({
                type: 'POST',
                url: 'reply_comment.php',
                data: {
                    userid: userid,
                    comment_id: commentid,
                    comment: comment
                },
                success: function(response) {
                    document.getElementById("submitedreply").innerHTML = response+document.getElementById("submitedreply").innerHTML;
                    document.getElementById("reply").value = "";
                }, 
                error: function(response) {
                    console.log(response);
                }
            });
        }
        else if(type == "new") {
            var com = document.getElementById('comment').value;
            $.ajax({
                type: 'POST',
                url: 'post_comment.php',
                data: {
                    userid: userid,
                    img_id: imgid,
                    comment: com
                },
                success: function(response) {
                    document.getElementById("submitedcomment").innerHTML = response+document.getElementById("submitedcomment").innerHTML;
                    document.getElementById("comment").value = "";
                }, 
                error: function(response) {
                    console.log(response);
                }
            });
        }

        return false;
    }
</script>
</head>
<body>


<div id="wrapper">
    <!-- start header -->
    <header>

        <div class="navbar navbar-default navbar-static-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="index.html">JELLYGIF.COM</a>
                </div>
                <div class="navbar-collapse collapse ">
                    <ul class="nav navbar-nav">
                        <li class="dropdown">
                            <a href="/jellygif/index.php">Home</a>
                        </li>
                        <li><a href="portfolio.html">CATALOG</a></li>
                        <li class="active"><a href="">PROFILE</a></li>
                        <li><a href="/jellygif/lib/logout.php">SIGN OUT</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </header>
    <!-- end header -->

    <section id="content">
    <div class="container">
        <div class="row">
            <div class="col-lg-8">
                <article>
                        <div class="post-image">    
                            <img src="/jellygif/uploads/<?php echo $FMI['Filename']; ?>" alt="" class="img-responsive" />
                        </div>
                                <div class="row">
                                    <div class="col-lg-9">
                                        <input type="text" id="copyTarget" value="http://localhost/jellygif/uploads/<?php echo $FMI['Filename']; ?>" class="form-control input-lg" readonly="TRUE">
                                    </div>
                                    <div class="col-lg-3">
                                        <input type="submit" id="copyButton" value="Copy link" class="btn btn-theme btn-block btn-lg">
                                    </div>
                                </div>

                        <script>var sharebutton_is_horizontal = false; document.write('<script src="//cdn.front.to/libs/share7.js"></scr' + 'ipt>');</script>
                        <blockquote>
                        <b>Source</b>  : <a href="<?php echo $FMI['source']; ?>"><?php echo $FMI['source']; ?></a>  
                        </blockquote>

                        <div class="bottom-article">
                            <ul class="meta-post">
                                <li><i class="fa fa-calendar"></i><a href="#"> <?php echo $FMI['post_time']; ?></a></li>
                                <li><i class="fa fa-user"></i><a href="#"> <?php echo $FUI['username']; ?></a></li>
                                <li><i class="fa fa-comments"></i><a href="#"><?php echo $NUMBER; ?> <?php if($NUMBER == 1) { echo "Comment";} else echo "comments"; ?></a></li>
                                <li><i class="fa fa-tags"></i><a href="#"><?php echo $FMI['catalog']; ?></a></li>
                            </ul>
                        </div>
                </article>
                    <div class="comment-area">
                        <h4><?php echo $NUMBER; ?> <?php if($NUMBER == 1) { echo "Comment";} else echo "Comments"; ?></h4>
                        <?php 
                        while($FC = $FetchComments->FETCH(PDO::FETCH_ASSOC))
                        {?>
                        <div id="media" class="media">
                            <a href="#" class="pull-left"><img src="/jellygif/img/avatar.png" alt="" class="img-circle" /></a>
                            <div class="media-body">
                                <div class="media-content">
                                    <h6><span><?php echo $FC['comment_time']."    "; ?></span>&nbsp <?php echo $FC['username']; ?></h6>
                                    <p>
                                        <?php echo $FC['comment']; ?>
                                    </p>
                                    <br><br>
                                    <?php 
                                        $C_ID = $FC['id'];
                                        $FetchReply = $conn->prepare("SELECT * FROM replies WHERE comment_id = ?");
                                        $FetchReply->execute([$C_ID]);
                                        while($FR = $FetchReply->FETCH(PDO::FETCH_ASSOC))
                                        { 
                                        $user_id = $FR['replier_id'];
                                        $FetchReplyerInfo = $conn->prepare("SELECT * FROM users WHERE user_id = ?");
                                        $FetchReplyerInfo->execute([$user_id]);
                                        $FRI = $FetchReplyerInfo->fetch(PDO::FETCH_ASSOC);
                                        $username = $FRI['username'];
                                        ?>
                                        <div class="media">
                                            <a href="#" class="pull-left"><img src="/jellygif/img/avatar.png" alt="" class="img-circle" /></a>
                                            <div class="media-body">
                                                <div class="media-content">
                                                    <h6><span><?php echo $FR['reply_time']."    "; ?></span>&nbsp <?php echo $username ?></h6>
                                                    <p>
                                                        <?php echo $FR['reply_comment']; ?>
                                                    </p>
                                                    <br><br>        
                                                </div>
                                            </div>
                                        </div>
                                        <?php } ?>
                                    <div id="submitedreply">
                                    </div>
                                    <br>
                                    <button class="align-right btn btn-theme btn-md click">Reply</button>
                                    <!-- Inner Reply -->
                                    <div class="hiddenstuff" style="display:none">
                                        <form action="" method="POST" onsubmit="return SubmitComment('reply', <?php echo $FUI['user_id']; ?>, null, <?php echo $FC['id']; ?>);">
                                          <div class="form-group">
                                            <textarea class="form-control" id="reply" rows="3" placeholder="* Your comment here" required></textarea>
                                          </div>
                                          <button type="submit" class="btn btn-theme btn-md">Reply</button>
                                        </form>
                                    </div>
                                    <!-- END Inner Reply -->
                                </div>
                            </div>
                        </div>
                        <?php } ?>
                        <div id="submitedcomment">
                        </div>

                        <div class="marginbot30"></div>
                        <h4>Comment on this PIC</h4>
                        <!-- Standard Reply -->
                        <form  action="" method="POST" onsubmit="return SubmitComment('new', <?php echo $FUI['user_id']; ?>, <?php echo $FMI['id']; ?>)">
                          <div class="form-group">
                            <textarea class="form-control" id="comment" rows="8" placeholder="* Your comment here" required></textarea>
                          </div>
                          <button type="submit" class="btn btn-theme btn-md">Submit</button>
                        </form>
                        <!-- END Standard Reply -->
                    </div>

                <div class="clear"></div>
            </div>

<a href="#" class="scrollup"><i class="fa fa-angle-up active"></i></a>
<!-- javascript
    ================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/jellygif/js/jquery.min.js"></script>
<script src="/jellygif/js/modernizr.custom.js"></script>
<script src="/jellygif/js/jquery.easing.1.3.js"></script>
<script src="/jellygif/js/bootstrap.min.js"></script>
<script src="/jellygif/js/jquery.appear.js"></script>
<script src="/jellygif/js/stellar.js"></script>
<script src="/jellygif/js/classie.js"></script>
<script src="/jellygif/js/uisearch.js"></script>
<script src="/jellygif/js/jquery.cubeportfolio.min.js"></script>
<script src="/jellygif/js/google-code-prettify/prettify.js"></script>
<script src="/jellygif/js/animate.js"></script>
<script src="/jellygif/js/custom.js"></script>
<script>

    $(".click").click(function(){
    $(this).next().fadeIn("slow");

});



</script>
<script>
document.getElementById("copyButton").addEventListener("click", function() {
    copyToClipboard(document.getElementById("copyTarget"));
});

function copyToClipboard(elem) {
      // create hidden text element, if it doesn't already exist
    var targetId = "_hiddenCopyText_";
    var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
    var origSelectionStart, origSelectionEnd;
    if (isInput) {
        // can just use the original source element for the selection and copy
        target = elem;
        origSelectionStart = elem.selectionStart;
        origSelectionEnd = elem.selectionEnd;
    } else {
        // must use a temporary form element for the selection and copy
        target = document.getElementById(targetId);
        if (!target) {
            var target = document.createElement("textarea");
            target.style.position = "absolute";
            target.style.left = "-9999px";
            target.style.top = "0";
            target.id = targetId;
            document.body.appendChild(target);
        }
        target.textContent = elem.textContent;
    }
    // select the content
    var currentFocus = document.activeElement;
    target.focus();
    target.setSelectionRange(0, target.value.length);

    // copy the selection
    var succeed;
    try {
          succeed = document.execCommand("copy");
    } catch(e) {
        succeed = false;
    }
    // restore original focus
    if (currentFocus && typeof currentFocus.focus === "function") {
        currentFocus.focus();
    }

    if (isInput) {
        // restore prior selection
        elem.setSelectionRange(origSelectionStart, origSelectionEnd);
    } else {
        // clear temporary content
        target.textContent = "";
    }
    return succeed;
}
</script>
</body>
</html>

0 个答案:

没有答案