Submit a form to api and save in db via shortcode

I want to display a form via shortcode in wordpress and when the user submits the entry, some of the information should be send to an api and some should be stored in the database:

<?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');

the trello handler and the db_save looks like this:

Read More
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."rn 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, 
        ) 
    );
}

The problem is, that when I press the submit button, the page reloads and it displays a 404 error (the url is exactly the same) and no database entries are saved.

(The trello thing is managed by this framework: https://bitbucket.org/mattzuba/php-trello and settings by http://www.titanframework.net)

Can somebody tell me, why the form isn’t correct submitted?

Related posts

Leave a Reply

1 comment