将表单提交到 api 并通过短代码保存在数据库中


Submit a form to api and save in db via shortcode

我想在wordpress中通过简码显示表单,当用户提交条目时,一些信息应该发送到api,一些应该存储在数据库中:

<?php
function form($atts, $content = null) {
    extract( shortcode_atts( array(
    ), $atts ) );
    echo '
        <form id="trello" class="form-horizontal" method="post">
            <div class="form-group">
                <div class="col-md-6">
                    <input type="text" name="name" placeholder="Name" class="form-control"/>
                </div>
                <div class="col-md-6">
                    <input type="email" name="email" placeholder="eMail" class="form-control" />
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    <input type="text" name="title" placeholder="Title" class="form-control" required/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    <textarea name="content" cols="40" rows="10" placeholder="Content" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    <input type="file" name="attachment" class="form-control">
                </div>
            </div>
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="notification"> Get an email, if something happens to the card
                </label>
            </div>
            <div class="form-group">
                <input class="btn btn-primary" type="submit" name="submit">
            </div>
        </form>';
        if (isset($_POST['title'])) { $title = $_POST['title']; };
        if (isset($_POST['content'])) {$content = $_POST['content']; };
        if (isset($_POST['name'])) {$name = $_POST['name']; };
        if (isset($_POST['email'])) {$email = $_POST['email']; };
        if (isset($_POST['attachment'])) {$file = $_POST['attachment']; };
        if (isset($_POST['notification'])) {$notification = $_POST['notification']; };
        if(isset($_POST['submit'])) {
            trello_handler($title, $content, $name, $email, $file);
            db_save($email, $notification);
        }
}
add_shortcode('form', 'form');

Trello 处理程序和db_save如下所示:

function trello_handler($title, $content, $name, $email, $file) {
    $titan = TitanFramework::getInstance( 'trello-tools' );
    $key = $titan->getOption( 'trello_key' );
    $token = $titan->getOption( 'trello_token' );
    $trello = new Trello($key, null, $token);
    $desc = $content."'r'n Reported by ".$name." (".$email.")";
    $trello->actions->post("cards", json_encode(['name' => $title, 'desc' => $desc, 'idList' => "54f882603b3b0af795078283", 'pos' => "top", 'fileSource' => $file ]));
}
function db_save($email, $notification) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'trello_submits';
    $wpdb->insert( 
        $table_name, 
        array( 
            'email' => $email, 
            'notification' => $notification, 
        ) 
    );
}

问题是,当我按下提交按钮时,页面会重新加载并显示 404 错误(url 完全相同),并且没有保存任何数据库条目。

(trello 的东西由这个框架管理:https://bitbucket.org/mattzuba/php-trello 和设置由 http://www.titanframework.net)

有人可以告诉我,为什么表格提交不正确?

找出问题所在:https://wordpress.org/support/topic/404-pops-after-custom-form-submission-by-post

WordPress使用"name"实习生,因此这导致了404错误。

将"名称"

更改为"客户端名称"解决了该问题。