Reputation: 694
I am adding Contact Form 7 to a page that also has a dynamically-generated HTML table with data specific to that user. When that user fills out the form and submits it, I have the form set up to send an HTML-formatted email back to us. I need to add the table as an HTML element in the email, but none of the methods I've tried are working. Assume a very basic table:
<table class="shop_table cart" id="carttable" name="carttable">
<tbody><tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Ach zo</td>
<td>Hans Gruber</td>
<td>Germany</td>
</tr>
<tr>
<td>El Moctezuma</td>
<td>Juan Epstein</td>
<td>Mexico</td>
</tr>
</tbody>
</table>
I tried adding this Javascript event listener to the PHP code of the WordPress page in question, as suggested by Contact Form 7's documentation:
document.addEventListener( 'wpcf7submit', function( event ) {
var carttable = document.getElementById("carttable").innerHTML;
document.getElementsByClassName("wpcf7-form").appendChild(carttable);
}, false );
But this throws an error:
Uncaught TypeError: document.getElementsByClassName(...).appendChild is not a function
Since appendChild
can only return a node, I tried:
document.addEventListener( 'wpcf7submit', function( event ) {
var carttable = document.getElementById("carttable").innerHTML;
document.getElementsByClassName("wpcf7-form")[0].appendChild(carttable);
}, false );
But this returned the error:
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
As it is, I don't know if, given the plugin's functionality, this approach would even result in getting the HTML of the table added to the email of the form details we get sent back to us, as it only gathers specific fields identified in the mail settings for that specific form. Is there a direct way to append HTML into that email when submitting the form? Thanks in advance for any help you can provide here.
EDIT: As requested, here is the HTML of the Contact 7 form I am using on this page:
<form action="/cart/#wpcf7-f8938-p5-o1" method="post" class="wpcf7-form" novalidate="novalidate">
<div style="display: none;">
<input type="hidden" name="_wpcf7" value="8938">
<input type="hidden" name="_wpcf7_version" value="5.1.3">
<input type="hidden" name="_wpcf7_locale" value="en_US">
<input type="hidden" name="_wpcf7_unit_tag" value="wpcf7-f8938-p5-o1">
<input type="hidden" name="_wpcf7_container_post" value="5">
</div>
<p><label> Your Name (required)<br>
<span class="wpcf7-form-control-wrap your-name"><input type="text" name="your-name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false"); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: auto;"></span> </label></p>
<p><label> Your Email (required)<br>
<span class="wpcf7-form-control-wrap your-email"><input type="email" name="your-email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email" aria-required="true" aria-invalid="false"></span> </label></p>
<p><label> Company<br>
<span class="wpcf7-form-control-wrap your-company"><input type="text" name="your-company" value="" size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false"></span> </label></p>
<p><label> Phone<br>
<span class="wpcf7-form-control-wrap cell"><input type="tel" name="cell" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-tel wpcf7-validates-as-tel" aria-invalid="false"></span> </label></p>
<p><label> Your Message<br>
<span class="wpcf7-form-control-wrap TestTextArea"><textarea name="TestTextArea" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required" aria-required="true" aria-invalid="false"></textarea></span> </label></p>
<p><input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit"><span class="ajax-loader"></span></p>
<div class="wpcf7-response-output wpcf7-display-none"></div></form>
Upvotes: 0
Views: 4346
Reputation: 694
So, the solution I was able to discover with the help of another developer was completely outside of the events made available by Contact Form 7, because they all fire after submitting the form. I had to add this piece of code to the end of the theme.js file located at the WordPress theme we're using: /wp-content/themes/vg-ebuilder/assets/js/theme.js. This captures the submit before the form is serialized for the Ajax call and parses the table into new arrays to be pushed to the email body:
jQuery(".wpcf7-form").on('submit', function(event){
var carttable = document.getElementById("carttable");
var cartHTML = document.getElementById("ShoppingCart");
if (!cartHTML)
{
cartHTML = document.createElement("INPUT");
cartHTML.setAttribute("type", "hidden");
cartHTML.id = "ShoppingCart";
cartHTML.name = "ShoppingCart";
}
var csv = [];
var rows = carttable.querySelectorAll("table tr");
var header = [];
//Build the header
if (rows.length > 0)
{
var headCols = rows[0].querySelectorAll("td, th");
for (var t = 1; t < headCols.length - 2; t++)
{
header.push(headCols[t].innerText);
}
}
//Retrieve the data
for (var i = 1; i < rows.length - 1; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 1; j < cols.length - 2; j++)
{
if (jQuery(cols[j]).find("INPUT").length > 0)
{
row.push(header[j - 1] + ": " + jQuery(cols[j]).find("INPUT").val());
}
else
row.push(header[j - 1] + ": " + cols[j].innerText);
}
csv.push(row.join("\n"));
csv.push("\n");
csv.push("-------------------------------------------------------------");
csv.push("\n");
}
cartHTML.value = csv.join("\n");
document.getElementsByClassName("wpcf7-form")[0].appendChild(cartHTML);
});
You then need to update the Mail portion of the form in the Contact Form 7 settings to include a new variable called ShoppingCart:
It's a shame that so much effort has to be made for such simple DOM retrieval/injection. Since they have deprecated Javascript inclusion before submitting in favor of DOM event listeners, it would be nice if they could allow some customization of an email prior to sending one from a form submit.
Upvotes: 1
Reputation: 244
The two examples that you show you tried are
document.addEventListener( 'wpcf7submit', function( event ) {
var carttable = document.getElementById("carttable").innerHTML;
document.getElementsByClassName("wpcf7-form").appendChild(carttable);
}, false );
document.addEventListener( 'wpcf7submit', function( event ) {
var carttable = document.getElementById("carttable").innerHTML;
document.getElementsByClassName("wpcf7-form")[0].appendChild(carttable);
}, false );
document.getElementsByClassName()
returns an array, which means that you would have to use the first element([0]
), but don't use the document.getElementById("carttable").innerHTML
, because that doesn't return the node, it returns the text inside the node, so it would look like this
document.addEventListener( 'wpcf7submit', function( event ) {
var carttable = document.getElementById("carttable");
document.getElementsByClassName("wpcf7-form")[0].appendChild(carttable);
}, false );
Upvotes: 1