weixin_33736832 2017-05-06 19:45 采纳率: 0%
浏览 30

在PHP中通过Ajax显示数据

I want to create a search function. This page already has some details which I retrieved from DB in div. I want to when I click search button, hide that all retrieved details and show searched details. I used ajax for it. Now problem is when I click search button previous retrieved data is hidden, but not show as search results.

    <form action="../PHP/searchrmvvac.php" method="post">
                    <div class="search hidden-xs hidden-sm">
                        <input type="text" placeholder="Search" id="search" name="search">
                    </div>
                    <div>
                        <input type="button" value="Search" id="searchrmvcom" name="searchrmvcom">
                        <script>
                            $("#searchrmvcom").click(function () {
                                var comname=$('#search').val();
                                $.ajax({
                                    type:"post",
                                    url:"../PHP/searchrmvvac.php",
                                    data:{comname:comname},
                                    success:function (data3) {
                                        $('#rmvcomdiv').hide();
                                        $('#ela').html(data3)
                                    }
                                });
                            });
                        </script>
                    </div>
                    </form>

searchrmvvac.php

    <?php
    session_start();
    require('../PHP/dbconnection.php');
    $output=$_POST['comname'];
    $sql="select * from company where company_name='$output' and activation_code=1";
    $res=mysqli_query($conn,$sql);
    if(mysqli_num_rows($res)>0) {
   echo '
while ($row = mysqli_fetch_assoc($res)) {
    ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <table class="table table-striped table-bordered table-list">
        <thead>

        <tr>
            <th>Action</th>
            <th>ID</th>
            <th>Registration number</th>
            <th>Company Name</th>
            <th>Email</th>
        </tr>
        </thead>
        <tbody>

            <tr>
                <td align="center"><button type="submit" class="btn btn-default myButton" value="$row['/companyid/']" id="accept" name="accept">Remove</button></td>
                <td>$row['/companyid/']</td>
                <td>$row['/government_reg_no/']</td>
                <td>$row['/company_name/']</td>
                <td>$row['/email/']</td>
            </tr>

        </tbody>
    </table>

    <?php
}
';
}
?>[![enter image description here][1]][1]

line 27 is

    <td align="center"><button type="submit" class="btn btn-default myButton" value="$row['/companyid/']" id="accept" name="accept">Remove</button></td>

this is error messages

  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2017-05-06 20:34
    关注

    There are few issues with your code, such as:

    • You're not including/referring to any jQuery library in your code, so $("#searchrmvcom").click( ... won't work in the first place.
    • There's no point embedding your JavaScript/jQuery code in your HTML form, keep them separate.
    • There's no JavaScript/jQuery code in searchrmvvac.php page, so there's no point including the jQuery library here.
    • You're embedding PHP variables $row['...'] in your HTML table in the wrong way. Plus you're echoing things in the wrong way.

    Your form page would be like this:

    <form action="../PHP/searchrmvvac.php" method="post">
        <div class="search hidden-xs hidden-sm">
            <input type="text" placeholder="Search" id="search" name="search">
        </div>
        <div>
            <input type="button" value="Search" id="searchrmvcom" name="searchrmvcom">
        </div>
    </form>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>
        $("#searchrmvcom").click(function () {
            var comname=$('#search').val();
            $.ajax({
                type:"post",
                url:"../PHP/searchrmvvac.php",
                data:{comname:comname},
                success:function (data3) {
                    $('#rmvcomdiv').hide();
                    $('#ela').html(data3)
                }
            });
        });
    </script>
    

    Subsequently, your searchrmvvac.php page would be like this:

    <?php
        session_start();
        require('../PHP/dbconnection.php');
        $output=$_POST['comname'];
        $sql="select * from company where company_name='$output' and activation_code=1";
        $res=mysqli_query($conn,$sql);
        if(mysqli_num_rows($res)>0) {
            while ($row = mysqli_fetch_assoc($res)) {
                ?>
                <table class="table table-striped table-bordered table-list">
                    <thead>
    
                    <tr>
                        <th>Action</th>
                        <th>ID</th>
                        <th>Registration number</th>
                        <th>Company Name</th>
                        <th>Email</th>
                    </tr>
                    </thead>
                    <tbody>
    
                        <tr>
                            <td align="center"><button type="submit" class="btn btn-default myButton" value="<?php echo $row['companyid']; ?>" id="accept" name="accept">Remove</button></td>
                            <td><?php echo $row['companyid']; ?></td>
                            <td><?php echo $row['government_reg_no']; ?></td>
                            <td><?php echo $row['company_name']; ?></td>
                            <td><?php echo $row['email']; ?></td>
                        </tr>
    
                    </tbody>
                </table>
                <?php
            }
        }
    ?>
    

    Sidenote: Learn about prepared statement because right now your query is susceptible to SQL injection attack. Also see how you can prevent SQL injection in PHP.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!