php Real-Time form post submit

E.Laemas Kim :

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.

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>
Bader :

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 .

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21332&siteId=1