Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证的AJAX改进_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 验证的AJAX改进

Javascript 验证的AJAX改进,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,所以我有一个AJAX,它可以正常工作,但是我想给它添加一些验证。我不希望它发送空信息。AJAX是为一个注释系统完成的。它从表单中获取值,然后使用php将其插入数据库。我遇到的问题是,它只是向数据库插入空注释 index.php <?php require_once("menu.php"); ?> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"

所以我有一个AJAX,它可以正常工作,但是我想给它添加一些验证。我不希望它发送空信息。AJAX是为一个注释系统完成的。它从表单中获取值,然后使用php将其插入数据库。我遇到的问题是,它只是向数据库插入空注释

index.php

<?php 
    require_once("menu.php");
?>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
        <script  src="comments.js" type="text/javascript" ></script>
        <script type="text/javascript">
            function validateForm()
            {
                var comment = document.getElementsByName('comment').value;

                if (comment == "" ) 
                {
                    alert("Please fill in all the fields");
                    return false;
                }
                else
                {
                    return true;
                }
            }
        </script>
<?php

    $connection = connectToMySQL();


    $selectPostQuery = "SELECT * FROM (SELECT * FROM `tblposts` ORDER BY id DESC LIMIT 3) t ORDER BY id DESC";

    $result = mysqli_query($connection,$selectPostQuery)
        or die("Error in the query: ". mysqli_error($connection));
    while ($row = mysqli_fetch_assoc($result)) 
    {
        $postid = $row['ID'];

?>
        <div class="wrapper">
        <div class="titlecontainer">
        <h1><?php echo $row['Title']?></h1>
        </div>
        <div class="textcontainer">
        <?php echo $row['Content']?>
        </div>
<?php
        if (!empty($row['ImagePath'])) #This will check if there is an path in the textfield
        {
?>
            <div class="imagecontainer">
            <img src="images/<?php echo "$row[ImagePath]"; ?>" alt="Article Image">
            </div>
<?php
        }
?>
        <div class="timestampcontainer">
        <b>Date posted :</b><?php echo $row['TimeStamp']?>
        <b>Author :</b> Admin
        </div>
<?php
        #Selecting comments corresponding to the post
        $selectCommentQuery = "SELECT * FROM `tblcomments` LEFT JOIN `tblusers` ON tblcomments.userID = tblusers.ID WHERE tblcomments.PostID ='$postid'";

        $commentResult = mysqli_query($connection,$selectCommentQuery)
            or die ("Error in the query: ". mysqli_error($connection));

        #renderinf the comments

        echo '<div class="comment-block_' . $postid .'">';

        while ($commentRow = mysqli_fetch_assoc($commentResult)) 
        {
?>
            <div class="commentcontainer">
            <div class="commentusername"><h1>Username :<?php echo $commentRow['Username']?></h1></div>
            <div class="commentcontent"><?php echo $commentRow['Content']?></div>
            <div class="commenttimestamp"><?php echo $commentRow['Timestamp']?></div>
            </div>
<?php
        }
?>
        </div>
<?php 

        if (!empty($_SESSION['userID']) ) 
        {
?>
            <form method="POST" class="post-frm" action="index.php" onsubmit="return validateForm();">
            <label>New Comment</label>
            <textarea name="comment" class="comment"> </textarea>
            <input type="hidden" name="postid" value="<?php echo $postid ?>">
            <input type="submit" name ="submit" class="submitComment"/>
            </form>
<?php
        }
        echo "</div>";
        echo "<br /> <br /><br />"; 
    }
 require_once("footer.php") ?>
我的php代码要插入到数据库中

 <?php
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])):

    session_start();
    include('connection.php');
    $connection = connectToMySQL();

        $userId = $_SESSION['userID'];
        $username = $_SESSION['username'];
        $postId = $_POST['postid'];
        $comment = $_POST['comment'];
        $date_format = " Y-m-d  g : i : s";
        $time = date ($date_format);


          $insertCommentQuery = "INSERT INTO `tblcomments` 
                                 (`Content`,`UserID`,`PostID`,`Timestamp`) 
                                VALUES (
                                   '$comment','$userId','$postId',
                                    CURRENT_TIMESTAMP)";
          $result = mysqli_query($connection,$insertCommentQuery);

$obj = array();

$obj['id'] = $postId;
$obj['html'] = '<div class="commentcontainer">
                    <div class="commentusername"><h1> Username :'.$username.'</h1></div>
                    <div class="commentcontent">'.$comment.'</div>
                    <div class="commenttimestamp">'.$time.'</div>
               </div>';
echo json_encode($obj);

    connectToMySQL(0);
   endif?>

您从未调用函数
validateForm()

这部分呢

if (!comment.val()){
                alert('You need to write a comment!');       
        }
如果有一个类为
comment
的输入字段,即使该字段为空,也将始终传递。你可以这样做:

//send ajax request
if(validateForm()===false){
    alert('You need to write a comment!');
    return false;
    }
    else{
     ....

您可以尝试将comment.val()更改为comment.text(),因为您使用的是textarea,看看这是否解决了您的问题。要在js代码中修改更改

您现在的问题是什么?插入数据库的空值?开发人员工具窗口中是否有任何错误A是的,很抱歉,它只是在控制台中插入空注释,没有任何错误。请尝试警告注释值,无论您是否获得空值,它给我的都是[object][object],然后逐步执行。首先在
validateForm()
的开头放置一个警报,查看它是否被触发。然后在
中放置两个警报,如果。。。否则…
construction以查看它返回的内容:true或FALSE如果尝试,它总是返回true,出于某种原因,我不知道为什么。原因是,当我在javascript中按send时,注释的值未定义。当它为空时,以及当它被文本填充时。但当我有文本时,它工作正常,这意味着它将文本正常地放入注释中。可能是因为我有多个同名表单(我正在查看每个帖子的表单)不知道为什么会影响它,然后在
if(typeof comment=='undefined'| | comment==“”)中更改它。
好的,我已经修复了它!GetelementsbyName由于某些原因不起作用,但getElementById的作用就像一个符咒。维尔德……你知道原因吗?我也为你做了准备;即使我在文本框中写入了文本,它也会给出一个空的弹出框
//send ajax request
if(validateForm()===false){
    alert('You need to write a comment!');
    return false;
    }
    else{
     ....