7

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.

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

Thanks

1

7 Answers 7

8

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
  • 15
    Oh my god. not even a single form of validation. May 10, 2015 at 10:53
  • 5
    This solution is susceptible to SQL injection attack. Please validate the input. Feb 23, 2018 at 15:16
3

I am going to use this solution, but it seams to be very customize and can that be a problem with themes and upgrades?

2
  • The way I see it, you should separate the logic from the template file. Your method that handles the POST data should be located in functions.php and initialized by a initialization hook. To only handle the submitted data for a specific page template, you could check for a named (optionally hidden) input field that's unique for that template. You could also benefit of using is_page_template()
    – estrar
    Jul 11, 2014 at 6:50
  • really helpful url
    – Rohit
    Jan 3, 2020 at 6:26
3

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)

4
  • I have tried Contact Form 7 and it look great but I do have some question. Is it possible to add a incremental/identifier as column to the table. I also need to set the dropdown values based on a tables values. I have never works with a CMS before and don't know how much you should and need to customize your self.
    – boje
    Dec 26, 2012 at 20:48
  • 1
    @boje you can with the Post My CF7 Form extension plugin, it reorganises the CF7 dashboard table, allowing you to use std WP functionality to add a custom column to the table.
    – Aurovrata
    Oct 17, 2016 at 8:25
  • Can I display the CF7 Records of One Site in Another Site .?
    – Developer
    May 29, 2017 at 10:30
  • This is still a valid solution. CFDB supports even more form generator plugins.
    – mrmut
    Jan 31, 2020 at 23:04
0

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.

6
  • by using the 'wp_insert_post()' is that not only for comment. And if not why do is it good to use that one? I don't get the options table idea. A quick search on Google don't help me :(
    – boje
    Dec 26, 2012 at 20:59
  • The wp_insert_post() is not for comments, is for post content, create an new post. I update my answer now, was from mobile; not realy easy on WPSE. The other answer with the hint to plugins is the same, Contact Form 7 or other create new posts, but you have the requirements of a plugin. If you will control this, also the source and have enough knowhow about coding, then is a custom plugin to create posts the best way.
    – bueltge
    Dec 27, 2012 at 12:29
  • Thanks for your help so far Bueltge. My understanding of wp_insertpost is that it is a help-function for quick and save insert something into a database-table. If i have a table(name=Course) with these column(id, coursename, paricipant, startdate, enddate). Where do i define the tablename? Maybe I understood it comeplety wrong. Code example link
    – boje
    Dec 28, 2012 at 9:50
  • No, the function create data in the default wordpress tables; minimum in _posts and if you have attachments or post meta data also in the other tables, there are in nabership with post. But you ask for save data from form in wordpress and I think, this is the best way. You have without work a UI with many solutions for edit and maintenance the items from form. If you see the code from my behaviour link, then you see the solution.
    – bueltge
    Dec 28, 2012 at 11:41
  • Thanks, think i need to read some more or find some tutorials to understand the setup better
    – boje
    Dec 28, 2012 at 12:02
0

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.

-1

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/

2
  • Please disclose your affiliation and make answer specific to the requirements of the question. This reads more like advertisement than genuine solution.
    – Rarst
    May 20, 2016 at 14:55
  • I agree, this looks like an advertisement, and I am an owner of the Save Contact Form 7. But, the solution and answer perfectly aligns with the requirement of the question. So, this is definitely going to help the person who asked the question. May 22, 2016 at 7:34
-1
<?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 }  ?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.