Pass your POST or GET vars through Contact Form7
After having used during a few times plugin CFORMII, I was interested in the plugins allowing to generate forms. I stopped on ” Contact Form 7″ and I was seduced by its simplicity, its partial edition mode allowing to integrate its code HTML/Css and to customise its forms. A think was missing… The ability to pass my variables POST contained in the page of the form (I took data on user) towards the emails which was generated by Contact Form 7 ! So I used the clever system of module set up by its author, and so is borned a small module (downloadable below) allowing to pass GET or POST famous variables through the plugin
The module ” httpvars” – Why is it useful?
for example in the php page which contains your form you have :
[php] $_POST [" myVar"][/php]
or in l’ URL…
[php] $_GET [" myVar"][/php]
… and you want pass and to recover this variable (or these variables) in the email generated by CF7.
Installation :
You unzip the contents of the file ” httpvars_contactform7.zip” and send the file ” httpvars.php” in the directory :
” ROOT wordpress/wp-content/plugins/contact-form-7/modules/”
Use :
very simple, in the screen of the plugin CF7, in the form and email code editor, you have to add these shortcode :
[php] /* in your form you insert */
[httpvars myVar]
[/php]
[php]/* in your email you insert */
[myVar]
[/php]
… where ” myVar” is the name of your variable to be recovered and pass.
How it operates :
The module generates a hidden field (of HIDDEN type) in your form for each variable which you call with the shortcode [httpvars myVar]. When the email is generated by CF7 the value of your GET|POST variables will be passed through.
In your CF7 form, when you add the shortcode [httpvars myVar] in the edition part of the plugin, the module Httpvars seeks if a $_POST [" myVar" ] exists, if not he seeks in URL if a $_SERVER [' QUERY_STRING' ] exists. When the module finds a String of GET or a POST variable corresponding to your variable ” myVar”, it automatically adds them to your EMAIL (With the shortcode [myVar]).
Downloading :
"HTTPVars" : a module for 'Contact Form7' (891 bytes, 130 hits)
French
How did you send the variable from page1 containing contact form 7 to page2 containing contact form7 ? Or in other words, how do you set the action=”/page2/” in the Contact form 7?
Welcome WPA,
Contact Form 7 generates himself the code of your form on 1 page.
I presumed in anyway you got your “GET” or “POST” values from a previous page/action, but what you could to do this :
PAGE1 :
a 1st classical POST form, such as :
< form name="visitorFormFieldsToFill" action="/page2/" method="POST">
< input type="hidden" name="var_to_pass_to_cf7" value= "im_the_value" />
< / form >
PAGE2 :
the page containing the CF7 form, such as :
< form action="/page2/index.php#wpcf7-f2-p3-o1" method="post" class="wpcf7-form">
< input type="hidden" name="var_to_pass_to_cf7" value="im_the_value" /> // my module will generate this line
< / form>
I hope I helped you.
Merci ! Ça fait exactement ce qui manquait
Hi, any idea how I can send a variable to the form differently to how you’ve done here? What I want is a button in a post that says ‘contact us’. That button links to the contact us page where a form field is populated with the slug or Id of the post you’ve just come from. Is this possible?
Cheers!
Hi Toby,
what I do to pass something from a post to the contact form (and which populates a form field) :
- In your (html code) post (PAGE 1), get your variables to pass. Eg. (here with POST method) :
< form name="myPostButton" action="/page2/" method="POST">
< input type="hidden" name="postID" value= "166" />
< input type="submit" name="Contact Us" />
< / form >
- In your contact us page (PAGE 2), use a JS script running in the code page after the execution of ContactForm (assuming the form field you want to populate has the element property, such “name=PostID_IwantTOget” ) :
< script type="text/javascript" >
function populate(nameElement,newvalue)
{
document.getElementsByName(nameElement)[0].value = newvalue;
}
populate(”PostID_IwantTOget”,”< ?php echo $_POST["postID"]; ?>“);
< /script >
Hope It answers !
Regards
This is very helpful! Thank you!
What I’d love to do is have the form pull a variable from the URL and populate one of the fields, which could then be edited by the user. Your example sends it directly to the email, but I’d like to give the user the option to change the value.
Example: I create a link like http://www.mysite.com/form/?city=Chicago
The form on that page would automatically put “Chicago” in the city text box.
Any suggestions?
This is just what I needed, but wouldn’t it be better to code the latter part of Toby’s request in PHP rather than JS since WP doesn’t handle JS quite well? I’m not quite well-versed in PHP though…?
Bonjour,
J’aimerais ajouter un menu déroulant dans mon formulaire qui contiendrait une liste dynamique des titres articles de mon blog.
Le but est de pouvoir ajouter une loop dans le formulaire.
vous pensez que ce plugin permettrais de réaliser ca ?
C’est à dire de pouvoir ajouter notre propre code php dans le formulaire.
Merci beaucoup,
SOUSSI
Bonjour Soussi,
Contact Form7 utilise par défaut des [shortcodes], donc introduire le PHP directement dans le formulaire ne sera pas possible. Mais ensuite il reste la possibilité de développer un plugin qui permettra de faire ce que vous souhaitez, c’est ce que j’ai fait avec mon plugin. Il est maintenant possible de passer des variables au formulaire que l’on récupère ainsi avec mes propres [shortcodes].
Peut-être quelqu’un aura développé d’autres plugin, mais ensuite il faut réussir à les trouver !
Bonne continuation.
Thanks!!
Very nice job.
Thanks! Just saved me loads of work. Very nice job!
Hi, the zip won’t open for me….? …..
Says its damaged or unknown format??
-Paul
Hi Paul,
Thanks for your message so I realized my path to the file was corrupted ! I repaired it so go on and download it now
Hope it will help you,
Robin