通过搜索栏进行Ajax内联编辑

时间:2014-12-23 01:33:52

标签: javascript php jquery ajax search

基本上,我有一个系统可以显示我网站上的用户。还有一个ajax搜索栏,允许我输入他们的一封信,当我走的时候,它会立即给我带上那个字母的名字。现在我的编辑系统是使用html5&amp ;;进行内联编辑。阿贾克斯。我遇到了一个问题,但是不允许我内联编辑在搜索内部显示的任何内容,尽管按钮和链接在其中工作。代码中可能存在错误,或者只是错误的方法。

这是我在home.php上的search.php函数:

    <script>
function search(string){
    var xmlhttp;
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }else{
        xmlhttp = new ActiveXObject("XMLHTTP");
    }
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById("search").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "search.php?s="+string, true);
    xmlhttp.send(null);
}
</script>

这是我的ajax.php:

<?php
if(!empty($_POST))
{
    //database settings
    include "classes/forms/config.php";
    foreach($_POST as $field_name => $val)
    {
        //clean post values
        $field_userid = strip_tags(trim($field_name));
        $val = strip_tags(trim(mysql_real_escape_string($val)));

        //from the fieldname:user_id we need to get user_id
        $split_data = explode(':', $field_userid);
        $user_id = $split_data[1];
        $field_name = $split_data[0];
        if(!empty($user_id) && !empty($field_name) && !empty($val))
        {
            //update the values
            mysql_query("UPDATE players SET $field_name = '$val' WHERE id = $user_id") or mysql_error();
            echo "Updated";
        } else {
            echo "Invalid Requests";
        }
    }
} else {
    echo "Invalid Requests";
}
?>

这是我的内联编辑功能:

<script>
$(function(){
    //acknowledgement message
    var message_status = $("#status");
    $("td[contenteditable=true]").blur(function(){
        var field_userid = $(this).attr("id") ;
        var value = $(this).text() ;
        $.post('ajax.php' , field_userid + "=" + value, function(data){
            if(data != '')
            {
                message_status.show();
                message_status.text(data);
                //hide the message
                setTimeout(function(){message_status.hide()},1500);
            }
        });
    });

});
</script>

最后我的search.php:

    <?php include_once('header.php'); ?>
    <?php include_once('classes/check.class.php'); ?>
    <?php include('classes/forms/connect-db.php'); ?>
    <?php include('classes/functions.php'); ?>
    <?php include("classes/forms/config.php"); ?>
    <style>
    #status {padding:10px;font-weight:bold;font-size:24px;margin-bottom:10px;display:none;width:50%;background-color:#dff0d8;border-color:#d6e9c6;color:#468847;}
    </style>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
    $(function(){
        //acknowledgement message
        var message_status = $("#status");
        $("td[contenteditable=true]").blur(function(){
            var field_userid = $(this).attr("id") ;
            var value = $(this).text() ;
            $.post('ajax.php' , field_userid + "=" + value, function(data){
                if(data != '')
                {
                    message_status.show();
                    message_status.text(data);
                    //hide the message
                    setTimeout(function(){message_status.hide()},1500);
                }
            });
        });

    });
    </script>
    <?php echo "<div id='status'></div>"; ?>
    <?php


    if(isset($_GET['s']) && $_GET['s'] != ''){
        $s = $_GET['s'];
        $sql = "SELECT * FROM `players` WHERE user LIKE '%$s%'";
        $result = mysql_query($sql);

        echo " <div class='table-responsive'><table class='table'>";
        echo "<tr><th>Username</th> <th>Rank</th> <th>Position</th> <th>DoP</th> <th>Tag</th> <th>AiT</th> <th>Service Stripes</th> <th>Notes</th><th></th><th></th><th></th></tr>";
        while($row = mysql_fetch_array( $result )) {

            echo "<tr>";
            echo '<td id="user:'.$row['id'].'" contenteditable="true">' . $row['user'] . '</a></td>';
            echo '<td>'. member_rank($row['rank']) . '</td>';
            echo '<td id="position:'.$row['id'].'" contenteditable="true">' . $row['position'] . '</td>';
            echo '<td id="date:'.$row['id'].'" contenteditable="true">' . $row['date'] . '</td>';
            echo '<td id="notes:'.$row['id'].'" contenteditable="true">' . $row['notes'] . '</td>';
            echo '<td><a href="classes/forms/promote.php?id=' . $row['id'] . '">Promote</a><BR /><a href="classes/forms/demote.php?id=' . $row['id'] . '">Demote</a></td>';
            echo '<td><a href="classes/forms/delete.php?id=' . $row['id'] . '">Delete</a></td>';
            echo '<td><a href="classes/forms/dd.php?id=' . $row['id'] . '">DD</a><BR /><a href="classes/forms/hd.php?id=' . $row['id'] . '">HD</a></td>';
            echo "</tr>"; 
        } 
        echo "</table> </div>";

    }

    ?>

当我刚刚浏览用户时,内联编辑工作非常完美,但就像我之前说过的那样,搜索栏无效。文本不会更新它只是坐在那里等待我搜索其他人或刷新页面。

0 个答案:

没有答案