将表单提交给api并通过短代码保存在db中

时间:2015-03-08 14:02:24

标签: php wordpress

我想在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错误(网址完全相同),并且不保存任何数据库条目。

(trello由此框架管理:https://bitbucket.org/mattzuba/php-trellohttp://www.titanframework.net设置

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

1 个答案:

答案 0 :(得分:1)

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

Wordpress使用“名称”实习生,因此导致404错误。

Channing“name”改为“clientname”解决了这个问题。