【问题标题】:Using "a" tag to run php code on same page使用“a”标签在同一页面上运行 php 代码
【发布时间】:2013-08-12 19:03:33
【问题描述】:

我正在使用链接到同一页面的“a href”标签。使用 jQuery,我将其设置为 event.preventDefault,因此页面不会刷新。当我单击该链接时,它会在同一页面上运行 php 代码并根据需要更新数据库,但我也希望它使用 mysql 来“回显”返回的结果,但这不会发生。有没有人有更好的主意?

我希望结果在不重新加载的情况下返回,我不能使用“.load”或这样的 jQuery 函数,因为它会影响我的应用程序的其他部分。

编辑 我的 php 代码运行但不显示结果

$queryCC="UPDATE test SET count='$Count' WHERE name='$xyz' ";
$resultCC=mysql_query($queryCC,$db) or trigger_error(mysql_error(),E_USER_ERROR);
$queryCT="SELECT count FROM test WHERE name='$getInc' ";
$resultCT=mysql_query($queryCT,$db) or trigger_error(mysql_error(),E_USER_ERROR);
$extractChSt=mysql_fetch_array($resultCT);
echo $getPRevCount=$extractChSt['count'];

html 链接

<a href="index?param=xyz" class="ECvesnd">Click</a>

jQuery

$('.ECvesnd').click(function(event) {
    event.preventDefault();
    $.ajax({
        url: this.href
    });
});

【问题讨论】:

  • 请显示您的代码 - 因为您无法在 javascript 函数中运行 php 代码。
  • 如果您认为 AJAX“影响了应用程序的其他部分”,很可能是您做错了,或者应用程序出现了严重问题。

标签: php jquery mysql hyperlink


【解决方案1】:

您可以使用success 回调从服务器捕获返回的(在服务器端回显“您的结果”)数据并在客户端显示,例如:

$('.ECvesnd').click(function(event) {
    event.preventDefault();
    $.ajax({
        url: this.href,
        success: function(data){
            alert(data);
        }
    });
});

Look at jQuery documentation for more.

【讨论】:

    【解决方案2】:

    我想这就是你想要的。试试这个

    $(document).ready(function(){
    $( ".ECvesnd" ).click(function(event) {
     event.preventDefault();
        $.ajax({
            url:this.href ,
            success: function(data){
                alert(data);
            }
        });
    
    });
    });
    

    【讨论】:

    • 我已经尝试过了,但我希望通过使用 ${'#div').html(data) 在我返回时显示整页来自同一页面的数据
    • 是的,您只需将上述代码中的警告框更改为 $("#div").html(data);我猜“div”是您的 div 标签的 id。那么这将起作用
    • 你检查了吗?
    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    相关资源
    最近更新 更多