doujie1917 2014-09-28 16:19
浏览 14
已采纳

不提取任何数据

I have this ajax request where i try to find some tags and add it to search results

The problems comes when i try with my php script to find something then it doesn't find anything

<div>
<form action="search()" method="post">
    <label for="tagsearch">Search tags: </label>
    <input type="text" id="tagSearch" name="tagsearch"/>
    <input type="button" id="tagenter" value="Add tag"/>
</div>
<div id="tagHolder">
    <!--Tags here-->
</div>
</div>
<script type="text/javascript">
    $(document).ready(function(){
        function search(){
            var tag=$("tagSearch").val();

            if(tag!=""){
                $("#tagHolder").html();
                $.ajax({
                   type:"post",
                    url:"gettag.php",
                    data:"tag="+encodeURIComponent(tag),
                    success:function(data){
                        $("#tagHolder").html(data);
                        $("#tagSearch").val("");
                    }
                });
            }
        }

        $("#tagenter").click(function(){
            search();
        });

        $('#tagSearch').keyup(function(e){
            if(e.keyCode == 13){
                search();
            }
        });
    });
</script>

The above code is the ajax request.

This is the php script

//Connect to database
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

//Check if connection is up
if(mysqli_connect_errno()){
    echo "Couldn't connect". mysqli_connect_error($con);
}

$tag = $_POST["tag"];

$result=mysqli_query($con, "SELECT * FROM tags WHERE tag = '".$tag."'");
$found=mysqli_num_rows($result);

if($found>0){
    while($row=mysqli_fetch_array($result)){
        echo "<div>". $row['tag'] . "</div>";
    }
}else{
    echo "<div>No suggestions</div>";
}

mysqli_close($con);

Im getting the "No suggestions" as search result. My guess is that the SQL query is wrong or the $found variable is wrong.

  • 写回答

2条回答 默认 最新

  • donglu5235 2014-09-28 16:38
    关注

    There are several significant problems here.

    1. As Jeff cryptically points out, you're not getting your value from tagSearch correctly, because $("tagSearch") looks for an element with the tag name tagSearch, not the id tagSearch. To use the id, add the # to your selector: var tag=$("#tagSearch").val(); This is the primary cause of the problem you've described, but keep reading, there's a lot more to do.

    2. You're wide open to SQL injection attacks. Humorous example:

      enter image description here

      Read more about them here, and about how to defend against them here.

    3. In your ajax call, data:"tag="+tag is wrong. It should be data:{tag:tag} (preferred) or data:"tag="+encodeURIComponent(tag). With your original code, if tag contains any character that isn't directly valid in URI-encoding (loosely, in a URL), your PHP page won't quite receive what you're sending. When use use an object (my first example), jQuery will handle encoding it for you; otherwise, do the encoding yourself with encodeURIComponent.

    4. As Fred -ii- helpfully points out, Your form action is wrong, and some browsers will try to use it when the user presses the Enter key in your text field. It should be action="javascript:search()", not just action="search()". The latter will be treated as a URL.

    5. Also as Fred -ii- points out, You probably wanted to close your form element with a </form> above the </div>.


    Error checking

    You're also not checking for any errors in your code, such as error reporting and mysqli_error().

    Those are important tools you should use during development.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分