简体   繁体   English

如何更改HTML的 <p> 从PHP内部文本?

[英]How to change html's <p> inner text from PHP?

I'm trying to change a HTML element's inner text from PHP.. is this even possible? 我正在尝试从PHP更改HTML元素的内部文本。这甚至可能吗? I am building a chat application for my website, (live chat) and I need to constantly update the chat by querying to a database, but I'm stuck on getting the element and changing its inner text from HTML from PHP. 我正在为我的网站构建一个聊天应用程序(实时聊天),我需要通过查询数据库来不断更新聊天,但是我一直坚持获取元素并从PHP的HTML更改其内部文本。 Thanks in advance. 提前致谢。

<?php
if (isset($_POST['iChat'])){
    $db_address = 'localhost';
    $db_username = 'root';
    $db_password = '';
    $db_name = 'chat';
    $connection = mysqli_connect($db_address, $db_username, $db_password, $db_name);
    $message = $_POST['iChat'];
    $personaname = $_COOKIE['personaname'];

    if ($message != '' && $personaname != ''){
        $query = "INSERT INTO chat VALUES (null, '{$personaname}', '{$message}')";
    mysqli_query($connection, $query);
    }
}

function get(){
    $db_address = 'localhost';
    $db_username = 'root';
    $db_password = '';
    $db_name = 'chat';
    $connection = mysqli_connect($db_address, $db_username, $db_password,     $db_name);
    $query = "SELECT `sender`, `message` FROM chat";
    $result = mysqli_query($connection, $query);

    >>>> GET ELEMENT HERE <<<<<<

    return json_encode($result);
}
?>

To change content inside any element or at any moment or location on your website the easiest and probably best way would be an Ajax request. 要在网站的任何元素,任何时间或任何位置更改内容,最简单也是最好的方法是Ajax请求。

With ajax requests you basically can send a post or get to your server to any given URL. 使用ajax请求,您基本上可以将帖子发送或到达服务器的任何给定URL。

You can also pass data (variables/object). 您还可以传递数据(变量/对象)。 This way you can send any user generated data to your server and do something with it. 这样,您可以将任何用户生成的数据发送到服务器并对其进行处理。 (input fields for instance). (例如输入字段)。

After the request has been made you have 3 callbacks: success , complete , error . 发出请求后,您将获得3个回调: successcompleteerror

The complete is always fired no matter what after either the success or error callback. 无论成功或错误回调之后执行什么操作,都会始终触发完成事件。 In the success callback you can return the result of your request (the value you return from your php file). 在成功回调中,您可以返回请求的result (从php文件返回的值)。

In the error callback you can notify the user that something went wrong, when for instance the request responded with bad request or bad gateway or time outs, 404/503 etc. error回调中,您可以通知用户出了点问题,例如,请求以错误的请求或错误的网关或超时,404/503等响应。

More info can be found here including some other usefull functions): http://api.jquery.com/category/ajax/ 可以在此处找到更多信息,包括一些其他有用的功能): http : //api.jquery.com/category/ajax/

Example code: 示例代码:

jQuery(document).ready(function($){
    $.ajax({
        url: '?act=process',
        type: 'post',
        data: {
            id: $id,
            name:$name,
            description:$description
        },
        success: function (result) {
            $('.alert-success').show();
            console.log('Yay it worked');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('.alert-danger').show();
            console.log('Something went wrong');
        }
    });
});

I guess you are looking for Ajax 我想您正在寻找Ajax

  1. You may have a trigger button 您可能有一个触发按钮

  2. Click the button, start ajax request (For example: Using jQuery ajax ) request your database data which return by your PHP code. 单击按钮,启动ajax请求(例如:使用jQuery ajax )请求数据库数据,这些数据由PHP代码返回。

  3. If the request is success, put the returning data into HTML element. 如果请求成功,则将返回的数据放入HTML元素。

     document.getElementById("ElementID").innerHTML = "Data return by PHP!"; 

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

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