Ajax调用正在将数据插入页面的其他位置


Ajax call is inserting data on page in some other place

我正在使用ajax调用在页面的开头插入数据。这更像是一个墙后脚本。每当用户发布内容时,最新的帖子都会显示在帖子页面的顶部。

我使用的是JS、PHP和HTML页面。

这是我的js代码:

    $(function() {
    $("#tweet_submit").click(function() 
    {
    var tweet = $("#tweet").val();
    var x = $('.preview').attr('id');
    if(x)
    var z= x;
    else
    var z=0;
    var dataString = 'tweet='+ tweet+ '&z=' +z;
    if(tweet=='')
    {
    alert('Please type your tweet here');
    }
    else
    {
    alert('into post');
    $.ajax({
    type: "POST",
    url: "WallPost/post_tweet1.php",
    data: dataString,
    cache: false,
    success: function(html){
    $("#tweet").val('');
    $("#preview").hide();
    $("#content").prepend(html);
    }
    });
    }return false;
    }); 
    });

这是我的PHP页面:post_tweet.php

    <?php
    include("includes/db.php");
    include("session.php");
    include_once 'includes/time_stamp.php';
    //echo "request sent to post";
    if($_POST)
    {
    $tweet=$_POST['tweet'];
    $upload_id=$_POST['z'];
    $time=time();
    $query = "INSERT INTO tweets(tweet,time,upload_id,uid_fk) VALUES ('$tweet','$time','$upload_id','$uid')";
    if (!mysqli_query($con,$query))
                                {
                                die('Error: ' . mysqli_error($con));
                                }
                                else
                                {
                                $msg ="<br> 1 record added";
                                }
    echo "<br>".$msg;
    $sql=mysqli_query($con,"select T.tid, T.tweet, T.time, T.upload_id, U.username, U.fullname, U.email FROM tweets T, users U WHERE T.uid_fk=U.uid and T.uid_fk='$uid' order by T.tid DESC LIMIT 1");
    //$show_result=mysqli_query($sql);
    while($row=mysqli_fetch_row($sql))
    {
    echo "data start here";
    $tid= "$row[0]";
    $tweet= "$row[1]";
    $time = "$row[2]";
    $img_id= "$row[3]";
    $username="$row[4]";
    $fullname = "$row[5]";
    $email = "$row[6]";
    $lowercase = strtolower($email);
    $imagecode = md5( $lowercase );
    $image_sql=mysqli_query($con,"select image_name from uploads where upload_id='$img_id'");
    while($data=mysqli_fetch_row($image_sql))
    {
    $image_name="$data[0]";
    $chars = 7; 
            $text = $image_name." "; 
            $text = substr($text,0,$chars);  
            $imgtext = "pic.twitter/".$text."...";
    }
    //echo "<div class='center'>";
    echo "<div class='tweet_box' id='$tid'>";
    echo "<div class='tweet_user'><img class='user_img' src='http://www.gravatar.com/avatar/$imagecode?s=50'></div>";
    echo "<div class='tweet_body'>";
    ?>
    <div class='tweet_time'><?php time_stamp($time);?></div>
    <?php
    echo "<div><b><a href='http://twitter.com/$username'>$fullname</a></b> <span class='uname'>@$username</span></div>";
    echo "<div class='tweet_text'>$tweet &nbsp;";
    if($img_id!=0)
    {
    echo "<a href=''/>$imgtext</a></div>";
    }
    else
    {
    echo "</div>";
    }
    echo "<div class='tweet_options'><div class='first_option'><a href='#'>Expand</a> </div><div class='sub_options'>&nbsp;&nbsp;<a href='#'>Reply</a> &nbsp;&nbsp;<a href='#'>Retweet</a> &nbsp;&nbsp;<a href='#'>Favourite</a></div></div>";
    if($img_id!=0)
    {
    ?>
    <div class="tweet_image" id="tweetimage<?php echo $tid; ?>">
    <center><img src="uploads/<?php echo $image_name; ?>" class="image_show"/></center></div>
    <?php
    }
    echo "</div></div>";
    ?>
    <div class="tweet_reply_box" id="replybox<?php echo $tid;?>">
    <div><textarea rel="<?php echo $tid;?>" id="replytext<?php echo $tid;?>" class="reply" name="reply" 
    maxlength="140">@<?php echo $username;?> </textarea></div>
    <div class="reply_button_bar" id="replybutton<?php echo $tid;?>">
    <div><input type="submit"  value=" Reply " class="reply_btn" id="<?php echo $tid;?>"/></div>
    </div>
    </div>
    <div id="reply_load<?php echo $tid;?>" class="reply_load">
    </div>

    <?php
    }
    }
    else { }
    ?>

这是我的HTML页面:

Wallpost.php

    <!doctype html>
    <html>
    <head>
        <title>Design Modo - Code-pal | eCommerce Website Template - Dead Stocker</title>
        <link rel="shortcut icon" href="http://www.code-pal.com/wp-content/themes/codpeal-new/favicon.ico">
        <link rel="stylesheet" type="text/css" href="css/check_style.css" />
        <link rel="stylesheet" href="css/wallpost/wtfdiary.css">
        <link rel="stylesheet" href="css/wallpost/tipsy_title.css">
        <script type="text/javascript" src="WallPost/js/jquery.min.js"></script>
        <script type="text/javascript" src="WallPost/js/jquery.tipsy.js"></script>
        <script src="WallPost/js/twitter.js"></script>
        <script type="text/javascript" src="WallPost/js/jquery.form.js"></script>
    </head>
    <body>
        <div class="page-wrap">
        <div class="clear"></div>
        <?php include("header.php"); ?>
        <div class="clear"></div>
        <div class="body-contents">
            <?php include("left-body1.php"); ?>
            <div class="center">
                <br>
                    <div class="txthead"><strong>Share thoughts here</strong></div>
                <?php
                    include("WallPost/index.php");
                ?>
            </div>
        </div>
        <?php include("right-body.php"); ?>
        <div class="clear"></div>
        </div>
        <!-- eo .body-content -->
        <?php include("footer.php"); ?>
        </div><!-- eo .page-wrap-->

这是我发布推特的代码:

index.php在WallPost文件夹下:

    <?php
    include("includes/db.php");
    include("session.php");
    include("includes/time_stamp.php");
    ?>
    <html>
    <head>
    <link rel="stylesheet" href="css/wtfdiary.css">
    <link rel="stylesheet" href="css/tipsy_title.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery.tipsy.js"></script>
    <script type="text/javascript" src="js/twitter.js"></script>
    <script type="text/javascript" src="js/jquery.form.js"></script>

    </head>
    <body>
    <div id="container">
    <div id="sidebar_container">
    <div id="sidebar">
    <div id='logo'><img src='images/1.png'></div>
    <div id="box">
    <div><textarea id="tweet" name="tweet" placeholder="Compose new Tweet..."></textarea></div>
    <div id='preview'></div>
    <div id="button_bar">
    <div id="icons">
    <div class="filebutton" title="Image Upload">
    <form id="imageform" method="post" enctype="multipart/form-data" action='ajax_image.php'>
    <span><input type="file" name="photoimg" id="photoimg"/></span>
    </form>
    </div>
    </div>
    <div><input type="submit"  value=" Tweet " class="btn" id="tweet_submit"/></div>
    </div>
    </div>
    </div>
    </div>
    <!-- <div id="main">  -->
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <div id='heading'>Tweets</div>
    <div id='content'>
    <?php include("loads_tweets1.php");?>
    <!-- </div>  -->
    </div>
    </div>
    </body>
    </html>

因此,在我使用了所有这些代码之后,Post会显示在我的页面中,但它的显示方式是不正确的。

帖子是随意插入的。(由于声誉不佳,我无法附上图片)

有人能告诉我出了什么问题吗?我无法将这些数据放在我其他帖子的正确位置。

提前感谢!!!

你的输入帮助了我,我解决了这个问题。内容在两个地方被调用,ajax在第一个位置插入了数据,但我没有把它放在我需要的地方。

谢谢。