httpwwwbethedevcom
324329524619168
Loading...

Custom form that store input in database | WordPress

Insert data in a database using the form in WordPress. Customs form that store input in the database.  What if you already have a databa...

Insert data in a database using the form in WordPress. Customs form that store input in the database. What if you already have a database of say, request information table, but you want to be able to save that data into a database it within a WordPress template? Today I’ll be showing you just how to do that, safely within the WordPress engine.

Requirements

  • Your own self-hosted WordPress site, obviously.
  • Basic PHP and MySQL skills
  • An existing dataset in MySQL.

What Are We Going to Do Before Coding?

Create a relevant table on database, I have created table like below (MySQL Code)

CREATE TABLE `request` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(255) NOT NULL,
  `Email` varchar(255) DEFAULT NULL,
  `Service` varchar(25) DEFAULT NULL,
  `Date` date DEFAULT NULL,
  `Address` varchar(255) DEFAULT NULL,
  `Phone` int(11) DEFAULT NULL,
  `Message` varchar(100) DEFAULT NULL,
  `status` varchar(10) NOT NULL DEFAULT 'start',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;


Making a Custom Template

If you want to use some of your own PHP code, the easiest way to do this is to create a custom template, then apply the template to a particular page you create in WordPress. Start by opening up your theme files and duplicating the page.php (or single.php if there isn’t one). You can find page.php file in wp-content\themes\<<Your Theme Name>> then duplicating. Rename it something obvious, like “Request.php” as I’ve chosen.

At the very top of the file, we need to tell WordPress this is a custom template. Do this by adding the following (this is a PHP style comment, so it should be after any opening PHP tag if present).

/*
Template Name: Request Page
*/
Obviously, call it whatever you like

Now, find the main content function. You can delete it if you want, but I’m just going add the extra code after it. With the default twenty-fifteen theme, you’re looking for: Here I have created template according to my table "request':

Request.php

Then we’ll need to create a page on the WordPress admin page, and apply our page template to it.



Publish, and check out the page to see if your form has worked.




Yeah! Now you can Submit data through this form, it will insert into request table.


Form Validation / Required

Here I have demonstrated how basically store data into a table using the form if have to validate every field and make as required  install Wp plugin Contact form 7, Then use these classes in your form.
Ref links All classes

example:
class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" aria-invalid="false"

Summary

I hope this comes in useful to some of you in your WordPress projects. Next week I’ll be tackling the slightly trickier topic of how to view database tables' data in your admin dashboard.  WordPress backend customization.

If you have any questions please post your comments below.

Thank you!
WordPress 222608647607191638

Post a Comment Default Comments Disqus Comments

  1. Thanks a lot. It's the easiest and works perfectly. Thanks again.

    ReplyDelete

emo-but-icon

Home item

Follow by Email

Popular Posts

Random Posts