简体   繁体   English

当我试图再次在同一页面上调用它时,Ajax无法正常工作

[英]Ajax not working properly when I tried to call it on same page again

I have a PHP page which has two sections (top and bottom). 我有一个PHP页面,它有两个部分(顶部和底部)。 The top section has a table where I have options to Edit the data. 顶部有一个表格,我可以选择编辑数据。

Once the Edit button is pressed, the content is loaded at the bottom of page and the user can change the data. 按下“编辑”按钮后,内容将加载到页面底部,用户可以更改数据。

Here is part of my PHP page: 这是我的PHP页面的一部分:

            <div id="product_entry_<?php echo $id?>" class="product_entry_<?php echo $id?>">
                <tr>
                    <td><font size=2px><?php echo $date?></font></td>
                    <td><font size=2px><?php echo $ProductName?></font></td>
                    <td><font size=2px><?php echo $Category.' / '.$SubCategory?></font></td>
                    <td><font size=2px><?php echo $MRP.' / '.$Price?></font></td>
                    <td><button type="submit" class="btn btn-primary btn-xs" style="padding:2px 2px;font-size: 9px;line-height: 10px;" onClick="DirectPromoteSubmit(<?php echo $id?>)">Promote</button></td>
                    <td><button type="submit" class="btn btn-primary btn-xs" style="padding:2px 2px;font-size: 9px;line-height: 10px;" onClick="RePromoteSubmit(<?php echo $id?>)">Edit</button></td>
                    <td><button type="submit" class="btn btn-primary btn-xs" style="padding:2px 2px;font-size: 9px;line-height: 10px;" onClick="DelPromoteSubmit(<?php echo $id?>)">X</button></td>
                </tr>
            </div>

            <!-- page where data is loaded -->
            <div class="box box-warning" id="RePromoteReplace">
             ....some html content here...
            </div> 

Here is my Javascript: 这是我的Javascript:

            function RePromoteSubmit(id){
                //alert('got into Edit Promotions');
                var dataString = "id="+ id;
                alert(dataString);
                if(dataString=='')
                {
                    alert('Some Problem Occurred, Please try Again');
                }
                else
                {
                //alert('into post');
                $.ajax({
                    type: "POST",
                    url: "SellerPanel/post_repromote.php",
                    data: dataString,
                    cache: false,
                    success: function(html){
                        //$("#tweet").val('');
                        //$("#preview").hide();
                        $("#RePromoteReplace").replaceWith(html);
                        alert('Product Successfully Loaded!!! Edit(Optional) & Click Promote Button in bottom section')
                    }
                    });
                }return false;
                } 

Here is my PHP page which loads the bottom section - post_repromote.php: 这是我的PHP页面加载底部 - post_repromote.php:

            <?php
            include("../dbconnection.php");
            include("session.php");
            if(!isset($_SESSION))
            {
                session_start();
            }

            $id=$_POST['id'];

            $query1=mysqli_query($con,"select promotiondata from sellerpromotions where id=$id");
            while($row=mysqli_fetch_array($query1))
                {
                .....some code here.....
                }
            ?>
                        <div class="box box-warning">
                            <div class="box-header">
                            <h3 class="box-title">Fill Product Details</h3>
                            </div><!-- /.box-header -->
                            <div class="box-body">

                        <!-- <form role="form " name="PromoteForm"> -->
                        <div>
                                <!-- text input -->
                            <table class="table">
                                ....some data here from query..
                            </table>        

                            <div class="box-header with-border">
                                        <h3 class="box-title">Upload your Product Image</h3>
                            </div><!-- /.box-header -->
                            <div class="box-body no-padding">
                                <div id='preview'>
                                    <?php if ($uploadid){ ?>
                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=<?php echo "SellerPanel/uploads/".$imagename?> id="<?php echo $uploadid?>" alt="User Image" class='preview' style='width:340px;height:200px;border-color:black'/>
                                    <?php } 
                                    ?>
                                </div>    
                                <?php 

                                    include ("index_photo.php");

                                ?>

                                <!-- <span class="users-list-date">Yesterday</span> -->

                            </div><!-- /.box-body -->
                            <div class="box-footer">
                                <button type="submit" class="btn btn-primary" onClick="PromoteSubmit()">Promote</button>
                            </div>
                    </div>
                            </div><!-- /.box-body -->
                        </div><!-- /.box -->
            <?php }
            ?>

The problem I'am facing: 我面临的问题是:

  • I'am able to load the data when I press the Edit button first. 我首先按下“编辑”按钮时能够加载数据。
  • When I press it again, I'm not able to load the new data unless I refresh the page and click the Edit button again. 当我再次按下它时,除非我刷新页面并再次单击“编辑”按钮,否则我无法加载新数据。

I tried to read the id in JS and printed it, I found that id is being passed correctly. 我试图在JS中读取id并打印它,我发现id正在正确传递。

Any help would be very much appreciated. 任何帮助将非常感谢。

Thanks in advance! 提前致谢!

JS after using solution: JS使用解决方案后:

            function RePromoteSubmit(id){
            //alert('got into Edit Promotions');
            var dataString = "id="+ id;
            alert(dataString);
            function getRandomInt() {
                return Math.floor(Math.random() * Math.pow(10,6));
            }
            //var url = "SellerPanel/post_repromote.php?"+getRandomInt();
            //alert ("url is: "+ url);
            if(dataString=='')
            {
                alert('Some Problem Occurred, Please try Again');
            }
            else
            {
            //alert('into post');
            $.ajax({
                type: "POST",
                url: "SellerPanel/post_repromote.php?rnd="+getRandomInt();
                data: dataString,
                cache: false,
                success: function(html){
                    //$("#tweet").val('');
                    //$("#preview").hide();
                    $("#RePromoteReplace").replaceWith(html);
                    alert('Product Successfully Loaded!!! Edit(Optional) & Click Promote Button in bottom section')
                }
                });
            }return false;
            }

Try changing your callback to use .html() instead of replaceWith() : (in your original code) 尝试更改回调以使用.html()而不是replaceWith() :(在原始代码中)

 success: function(html){
       //$("#tweet").val('');
       //$("#preview").hide();
         $("#RePromoteReplace").html(html);
         alert('Product Successfully Loaded!!! Edit(Optional) & Click Promote Button in bottom section')
  }

I also recommend changing your selector to a class instead of an ID , you can do this by just adding an extra class in your HTML: 我还建议您将选择器更改为class而不是ID ,只需在HTML中添加一个额外的类即可:

<div class="box box-warning promoReplace" id="RePromoteReplace">

and then updating the selector in your success callback: 然后更新success回调中的选择器:

$(".promoReplace").html(html);

Sidenote: For debugging, its usually easier to use console.log() instead of alert() (when using ajax, you usually would have the console open anyway) 旁注:对于调试,它通常更容易使用console.log()而不是alert() (当使用ajax时,通常会让控制台打开)

Your AJAX response is getting cached by the browser due to some server setting or page header. 由于某些服务器设置或页眉,您的AJAX响应将被浏览器缓存。 Easiest way to disable this is to append some randomly generated parameter to your url every time you send a request. 最简单的禁用方法是每次发送请求时将一些随机生成的参数附加到您的网址。

The function getRandomInt will generate a 6 digit random number. 函数getRandomInt将生成一个6位数的随机数。 If you want to have more/less digits, change the second argument passed to Math.pow . 如果您想要更多/更少的数字,请更改传递给Math.pow的第二个参数。

function getRandomInt() {
      return Math.floor(Math.random() * Math.pow(10,6));
}
//then, inside your ajax function, use:
    url: "SellerPanel/post_repromote.php?rnd="+getRandomInt(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM