【问题标题】:It's possibly change html element via PHP?它可能通过 PHP 更改 html 元素?
【发布时间】:2015-02-02 01:26:54
【问题描述】:

我有

<span id="first_name"></span>

从 mysql 我得到“first_name”值,我想把它写到#first_name。 JS做得很好

$('span').text(firstNameFromDb);

但我不能在 php 上做到这一点。有可能吗?我该怎么做?

【问题讨论】:

  • id="first_name">
  • 我有“index.html”和 和“some.php”。如何从“some.php”更改
  • @CapitanSlow 您必须将 index.html 更改为 index.php,然后将其放入。另一种选择是将您的 html 文件包含在您的 php 文件中,如下所示:

标签: php html mysql web request


【解决方案1】:

您不能直接从 PHP 更改 HTML,但您可以通过 javascript 使用 AJAX 函数来做到这一点。如果你的项目中已经包含了 jQuery js 库,请在 js 中创建如下代码:

$.ajax({
        url: getFirstName.php,
        data: {
            user: $first_name("#user-id").val(),
        },
        success: function(data){
            $("#first_name").html(data);
        }

    });

在相应的事件上触发上述 jQuery 函数。

在 getFirstName.php 中执行以下逻辑:

$user_id = $_GET['user'];
$get_firstname = //mysql code to get the first name//;
echo $get_firstname;

这样就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-18
    • 2014-06-28
    • 2012-07-26
    • 2020-03-31
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    相关资源
    最近更新 更多