In this article, we will show you how to use default values from HTTP requests in your CF7 forms.
When to use default values in your forms
It’s useful to implement default values in form fields for a variety of cases:
- Help your user fill out a form faster.
- If your forms target registered users of the site.
- If the form needs to be called with a different default value depending on the referrer of the form.
- When you have a fixed value for a field but still need to display it to the user.
Getting default values from HTTP requests
You can use the value of either the GET request or the POST request to use as the default value in your forms.
Use the “get” keyword in the form tagForm Tag Form Tags in Contact Form 7 (CF7) (in the Form Tab) are used to define all CF7 form fields. Each Form Tag is replaced with an HTML element, which represents an input field, when it is displayed in an actual form. to get the default value from HTTP GET variables.
[text* name default:get]
Now, you can access the page containing the form using a similar URL:
You will now see the value sent via GET in your form.
Note: The name of the GET variable must be the same as the name of the form tag for this to work.
The use of POST is very similar to that of GET. You have to use the “post” keyword in the form tag to get the default value from HTTP POST variables.
[text* name default:post]
Now, when you access this page via a POST request with the variable “name” sent, you will see the value of the variable in your form.
The following form will have the default subject value as “Inquiry” when a GET variable with that name is sent. Use the following URL to test this:
- Contact Form 7 – Getting Default Values from the Context
- Contact Form 7 – Setting Default Values to the Logged-In User
- Getting default values from post meta in Contact Form 7
- Using multiple default values in Contact Form 7
- Getting default values from logged-in user in Contact Form 7