Custom form that store input in database

I have never used WordPress before, but have a lot of programming experience. My question is, how to create customs forms in WordPress?

I need to create a form where a user fills some input fields and on submit the data should be stored in the database. I don’t need any notification on saving.

Read More

I also need to query the data and get the output in an HTML table.

Thanks

Related posts

Leave a Reply

7 comments

  1. I got the problem solution myself. See the code below this will do that.

    Put the code inside your newly created custom template.

    <?php
        if (!empty($_POST)) {
            global $wpdb;
            $table = wp_achord;
            $data = array(
                'name' => $_POST['yourname'],
                'chord'    => $_POST['chord']
            );
            $format = array(
                '%s',
                '%s'
            );
            $success=$wpdb->insert( $table, $data, $format );
            if($success){
                echo 'data has been save' ; 
            }
        } else {
            ?>
            <form method="post">
                <input type="text" name="yourname">
                <textarea name="chord"></textarea>
                <input type="submit">
            </form>
            <?php 
        }  
    ?>
    
  2. My suggestion:

    Use Contact Form 7 plus Contact Form DB. You can “design” your form in the backend and putting in the frontend via a simple shortcode.

    The Contact Form DB extension let you put your data in the database and offers to you shortcodes to display it (or you can query the data directly from the database if you prefer)

  3. It give different ways. For store data, like options is the options table, the options API, the right place with a entry and a array as store element. But for store data to use it like posts and each request from the form is it better to save also like post. But also here different possibilities. You can save for different post types. The default is ‘post’ and also you can save in a custom post type. Use the function wp_insert_post() to save for all post type after send Form from users. You find very fine answers here to this function, this topic ans also in the wild of www. You can also see a small example in this answer 73653, inlcude attachments in the form.

  4. You could also use the excellent Contact Form 7 plugin along with the Post My CF7 Form extension plugin which will allow you to save any custom forms to a post, including images as featured attachments, and select/checkbox/radio inputs as taxonomies.

    The Post My CF7 Form plugin has a rich functionality that can be leveraged to further customise and tweak the way your forms should be saved. There is a detailed documentation section too.

  5. Your solution is good and may work very well. But, there are few limitations. eg:

    1) What about searching and sorting entries which are posted through your form?
    2) What about exporting the data filled into your form to Excel or CSV or PDF?
    3) What if you want to print the inserted data.

    All that can be achieved with Contact Form 7 + Save Contact Form 7 plugins.

    both are free widely used plugins: http://savecontactform7.com/
    http://contactform7.com/

  6. <?php
    /**
    Use these line of codes, its working more than 100%
     */
    get_header();?> <?php
            if (!empty($_POST)) {
            global $wpdb;
                $table = wp_contact;
                $data = array(
                    'name' => $_POST['aname'],
                    'email'    => $_POST['aemail'],
                     'subject' => $_POST['asubject'],
                    'msg'    => $_POST['amsg']
                );
                $format = array(
                    '%s',
                    '%s'
                );
                $success=$wpdb->insert( $table, $data, $format );
                if($success){
                echo 'data has been saved' ; 
    }
    }
    else   {
    ?>
            <form action="<?php echo get_option('siteurl'); ?>/form/" method="post">
            <input type="text" name="aname">
             <input type="text" name="aemail">
              <input type="text" name="asubject">
            <textarea type="text" name="amsg"></textarea>
    
            <input type="submit">
            </form>
    
           <?php }  ?>