简体   繁体   English

php实时表单提交后

[英]php Real-Time form post submit

Let me explain what I want to.让我解释一下我想要什么。

I want to add a value in a list with db add without page change when I input in form , and click submit .我想在form输入时使用 db add 在列表中添加一个值而不更改页面,然后单击submit

but in this code, I must refresh one more time to add, and also added twice a time.但是在这段代码中,我必须再刷新一次才能添加,而且一次也添加了两次。

How can I do that?我怎样才能做到这一点?

<?php
    $conn = mysqli_connect('127.0.0.1','MYID','MYPASS','MYDB');
    $sql = "SELECT * FROM MYTABLE";
    $rs = mysqli_query($conn, $sql);

    $list = '';
    while($row = mysqli_fetch_array($rs)) {
            $list = $list."<li><a href=\"index.php?id={$row['id']}\">{$row['title']}</a></li>";
    }

    $article = array(
        'title' => 'Welcome!',
        'description' => 'Hello, Web!'
    );

    if (isset($_GET['id'])){
        $filtered_id = mysqli_real_escape_string($conn, $_GET['id']);

        $sql = "SELECT * FROM topic WHERE id={$filtered_id}";
        $rs = mysqli_query($conn, $sql);
        $row = mysqli_fetch_array($rs);
        $article['title'] = $row['title'];
        $article['description'] = $row['description'];
    }

    if ($_POST['title'] != null){
        $sql_in = "INSERT INTO topic (title, description, created) VALUES ('{$_POST['title']}', '{$_POST['description']}', NOW())";

        $rs_in = mysqli_query($conn, $sql_in);

        if ($rs_in === false) {
                $msg = mysqli_error($conn);
        } else {
                $msg = 'Success.';
        }
    } else {
        $msg = 'Fill in';
    }
?>

<!doctype html>
<html>
        <head>
                <meta charset="utf-8">
                <title>WEB</title>
        </head>

        <body>
                <h1><a href="index.php">WEB</a></h1>
                <ol>
                        <?=$list?>
                </ol>
                <details>
                        <summary>Create</summary>
                        <form action="./index.php" method="POST">
                                <p><input type="txt" name="title" placeholder="title"></p>
                                <p><textarea name="description" placeholder="description"></textarea></p>
                                <p><input type="submit"></p>
                                <p><?=$msg?></p>
                        </form>
                </details>
        </body>
</html>
                <h2><?=$article['title']?></h2>
                <?=$article['description']?>
        </body>
</html>

In php realtime isn't a thing but there is a workaround you can use events services like pusher https://pusher.com/docs and receive the events in client side instantly .在 php 实时不是一件事,但有一种解决方法,您可以使用诸如 pusher https://pusher.com/docs 之类的事件服务并立即在客户端接收事件。

there other website offers this so do your search before choosing them also you can always build your event server in node.js , c# , go有其他网站提供此功能,因此在选择它们之前请先进行搜索,您也可以随时在 node.js、c#、go 中构建您的事件服务器

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

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