Given that the Contact Form 7 (CF7) syntax and custom placeholders are used, it is likely that you're trying to integrate this into a WordPress environment where CF7 processes these tags. The CF7 syntax [text* name-764 id:name-764 placeholder "Name*"] should be parsed by the CF7 plugin into actual HTML input elements. However, this would not work if you directly preview this as plain HTML without WordPress processing it.
Ensuring CF7 Syntax Works in WordPress
If you're editing the CF7 form directly in a WordPress environment, you typically wouldn't need to worry about plain HTML preview issues. But if testing locally without WordPress, it won't parse these tags correctly. Here’s how you typically include CF7 forms in WordPress:
- Create or Edit Your CF7 Form: Go to Contact > Contact Forms, and either create a new form or edit an existing one. Ensure to specify the required fields correctly in CF7 syntax.
- Customize the Form HTML: Use the CF7 specific shortcodes and syntax within the WordPress editor.
Example CF7 Form in WordPress
Instead of directly using the CF7 syntax outside of a WordPress environment, ensure you're editing it within the plugin's environment. Here’s a complete integration:
CF7 Shortcode in WordPress:
<div class="form-container">
[contact-form-7 id="fd88d2c" title="Steel Construction Cost Estimate"]
<div>
<label for="name-764" class="required">Name:</label>
[text* name-764 id:name-764 placeholder "Name*"]
<label for="email-254" class="required">Email:</label>
[email* email-254 id:email-254 placeholder "Email*"]
<label for="tel-784" class="required">Telephone Number:</label>
[tel* tel-784 id:tel-784 placeholder "Telephone Number*"]
</div>
<div>
<p>Standard Height: 4.5m</p>
</div>
<div class="radio-group">
<label>Select Material:</label>
[radio material use_label_element default:1 "chromadek" "galvanized"]
</div>
<div class="dropdown-group">
<label for="lengthInput">Enter Length (in meters):</label>
[text length id:lengthInput name:length]
</div>
<div class="dropdown-group">
<label for="widthDropdown">Select Width (in meters):</label>
[select width id:widthDropdown "Select Width" "40" "35" "30" "25" "20" "18" "15" "12"]
</div>
<div class="dropdown-group">
<label for="sourceDropdown">Where did you hear about us?</label>
[select source id:sourceDropdown "Select Source" "website" "referral" "onsite" "linkedin" "facebook" "instagram" "vehicle" "other"]
</div>
[submit "Calculate Cost"]
[hidden hiddenCost id:hiddenCost]
</form>
<div id="estimatedCost"></div>
</div>
The JavaScript Part
Ensure your JavaScript functions are included correctly within WordPress using actions or enqueue scripts. You can place them in your theme’s functions.php file or within a separate JS file.
function my_custom_scripts() {
wp_enqueue_script( 'calculate-cost-script', get_stylesheet_directory_uri() . '/js/calculate-cost.js', array('jquery'), null, true );
}
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );
Example JS File (calculate-cost.js):
var pricing = {
'chromadek': {
'5-20': {'40': 1091, '35': 1201, '30': 1068, '25': 1022, '20': 944, '18': 871, '15': 799, '12': 1010},
'21-30': {'40': 1057, '35': 1167, '30': 1029, '25': 984, '20': 913, '18': 844, '15': 778, '12': 983},
'31-40': {'40': 1018, '35': 1127, '30': 985, '25': 942, '20': 877, '18': 814, '15': 754, '12': 952},
'41-50': {'40': 996, '35': 1104, '30': 960, '25': 917, '20': 857, '18': 796, '15': 741, '12': 935},
'51-60': {'40': 983, '35': 1090, '30': 945, '25': 902, '20': 845, '18': 786, '15': 733, '12': 924},
'61-70': {'40': 973, '35': 1081, '30': 934, '25': 892, '20': 836, '18': 778, '15': 727, '12': 917},
'71-80': {'40': 966, '35': 1073, '30': 926, '25': 884, '20': 830, '18': 773, '15': 723, '12': 911},
'81-90': {'40': 961, '35': 1068, '30': 920, '25': 878, '20': 825, '18': 769, '15': 720, '12': 907},
'91-100': {'40': 951, '35': 1064, '30': 915, '25': 874, '20': 821, '18': 765, '15': 717, '12': 903}
},
'galvanized': {
'5-20': {'40': 1029, '35': 1140, '30': 1006, '25': 960, '20': 882, '18': 807, '15': 734, '12': 930},
'21-30': {'40': 995, '35': 1105, '30': 968, '25': 922, '20': 851, '18': 781, '15': 713, '12': 903},
'31-40': {'40': 957, '35': 1066, '30': 925, '25': 880, '20': 816, '18': 751, '15': 690, '12': 873},
'41-50': {'40': 935, '35': 1044, '30': 900, '25': 856, '20': 796, '18': 734, '15': 678, '12': 856},
'51-60': {'40': 922, '35': 1030, '30': 885, '25': 841, '20': 783, '18': 724, '15': 670, '12': 845},
'61-70': {'40': 912, '35': 1020, '30': 874, '25': 831, '20': 775, '18': 716, '15': 664, '12': 838},
'71-80': {'40': 905, '35': 1013, '30': 866, '25': 823, '20': 768, '18': 711, '15': 660, '12': 832},
'81-90': {'40': 900, '35': 1008, '30': 860, '25': 818, '20': 764, '18': 707, '15': 657, '12': 828},
'91-100': {'40': 896, '35': 1004, '30': 855, '25': 813, '20': 760, '18': 704, '15': 654, '12': 825}
}
};
function checkFormValidity() {
var name = document.getElementById('name-764').value.trim();
var email = document.getElementById('email-254').value.trim();
var tel = document.getElementById('tel-784').value.trim();
var lengthInput = document.getElementById('lengthInput').value.trim();
var widthDropdown = document.getElementById('widthDropdown').value;
var sourceDropdown = document.getElementById('sourceDropdown').value;
var isValid = true;
if (!name) {
document.getElementById('estimatedCost').innerHTML = "Please enter your name.";
isValid = false;
} else if (!email) {
document.getElementById('estimatedCost').innerHTML = "Please enter your email.";
isValid = false;
} else if (!tel) {
document.getElementById('estimatedCost').innerHTML = "Please enter your telephone number.";
isValid = false;
} else if (!lengthInput) {
document.getElementById('estimatedCost').innerHTML = "Please enter length.";
isValid = false;
} else if (!widthDropdown) {
document.getElementById('estimatedCost').innerHTML = "Please select width.";
isValid = false;
} else if (!sourceDropdown) {
document.getElementById('estimatedCost').innerHTML = "Please select source.";
isValid = false;
} else {
document.getElementById('estimatedCost').innerHTML = ""; // Clear any previous error messages
}
return isValid;
}
function calculateCost(event) {
// Prevent form submission
event.preventDefault();
if (!checkFormValidity()) {
return false;
}
var name = document.getElementById('name-764').value.trim();
var email = document.getElementById('email-254').value.trim();
var tel = document.getElementById('tel-784').value.trim();
var lengthInput = document.getElementById('lengthInput').value.trim();
var widthDropdown = document.getElementById('widthDropdown').value;
var sourceDropdown = document.getElementById('sourceDropdown').value;
var material = document.querySelector('input[name="material"]:checked').value;
var totalCost = calculateLengthWidthCost(lengthInput, widthDropdown, material);
var formattedCost = totalCost.toLocaleString();
document.getElementById('hiddenCost').value = formattedCost;
document.getElementById("estimatedCost").innerHTML = "Estimated Total Cost: R " + formattedCost + " Excl. VAT";
return false;
}
function calculateLengthWidthCost(lengthInput, width, material) {
var totalLength = parseInt(lengthInput);
var widthCost = parseInt(width);
var lengthRange = getLengthRange(totalLength);
var costPerSquareMeter = pricing[material][lengthRange][widthCost.toString()];
return totalLength * widthCost * costPerSquareMeter;
}
function getLengthRange(length) {
if (length >= 5 && length <= 20) {
return '5-20';
} else if (length >= 21 && length <= 30) {
return '21-30';
} else if (length >= 31 && length <= 40) {
return '31-40';
} else if (length >= 41 && length <= 50) {
return '41-50';
} else if (length >= 51 && length <= 60) {
return '51-60';
} else if (length >= 61 && length <= 70) {
return '61-70';
} else if (length >= 71 && length <= 80) {
return '71-80';
} else if (length >= 81 && length <= 90) {
return '81-90';
} else if (length >= 91 && length <= 100) {
return '91-100';
} else {
return '';
}
}
document.addEventListener('DOMContentLoaded', function() {
var form = document.getElementById('fd88d2c');
form.addEventListener('submit', calculateCost);
});
Key Points:
- CF7 Syntax: Include CF7 tags in WordPress using CF7 plugin.
- Event Preventions:
event.preventDefault() in calculateCost() function.
- Appending event listeners to prevent default form submission behavior.
- Script Enqueueing: Ensure the
calculate-cost.js file is properly enqueued in WordPress using wp_enqueue_script.
Testing:
- Ensure to test the form within your WordPress environment to make sure CF7 parses the placeholder tags correctly.
- Adjust styles in WordPress using custom CSS and ensure your styles are correctly applied by inspecting elements in the browser’s developer console.
By following these steps, you should address form submission, input validation, and correct cost display while keeping CF7 syntax intact.