Alternative to php echo within code

I can’t speak to how the above code is actually implemented, but the very first part is a misuse of “echo.” Everything under “” should be outside of of it.

How exactly you handle the rest of the code (and whether it makes sense) is impossible for me to say, but you might see something like (I’d probably not write it this way, but whatever…):

function add_content2() {   

    // CONNECTS TO DATABASE
    $dbtype = mysqli_connect("localhost", "JohnLyons", "2012Zombies123!", "b1s243006078611");

    if( ! $dbtype ) {
    die(mysqli_error($dbtype));
    }

    // WHAT HAPPENS ON SUBMIT
    if(isset($_POST["submit"])) {
        $name = $_POST["name"];
        if(empty($name)) {
            wc_add_notice(("Fill in the entire field"), "error");
        } else {
        // Queries database. How to get header to stay on page
            mysqli_query($dbtype, "INSERT INTO todo(name) VALUES ("$name")");
            $referer = $_SERVER["HTTP_REFERER"]; 
            header("location: $referer");
        }
    }

    $name_loop = mysqli_query($db, "SELECT * FROM todo"); 

    //END FIRST LINES - maybe something more/else goes here or even is in a separate function or file?   

    echo '<tbody>' ;

    while ($row = mysqli_fetch_array($name_loop)) { 

        echo '<tr>
                <td>' . $row["name"] . '</td>
            </tr>' ;
                
    }   
    
    echo '</tbody> ' 

    echo '<form style="padding-top: 30px" method= "' . $_SERVER["PHP_SELF"]; . ' >' ;

    if (isset($error)) { 
        echo '<p>' . $error . '</p>' ;  
    } 

    echo '<input type="text" placeholder="Enter student information" name="task">' ;

    echo '<button type="submit" name="submit" style="margin-top: 10px">Register Student</button>' ;

    echo '</form>' ;
    
}