More often than not the feature I’m trying to implement doesn’t exist. Or it’s just not quite right and doesn’t get the job done.
- At face value this seemed fairly straight forward – build a web form that
Of course there are plenty of examples online of how to implement each of these features but no example that ties them all together.
Step 1 – The Basic PHP Form
Let’s start with a basic PHP web form. W3 Schools has good examples to get you up to speed on the basics. The idea is that form submits itself to itself allowing the fields to be validated, the values entered to remain in the inputs, and the ability to change what is displayed when the form is submitted. W3 Schools Complete Example.
</pre> <form action="#" method="post">; <input type="text" name="email" value="<?php echo $email;?>" /> <input type="submit" name="submit" value="submit" /></form> <pre>
Step 2 – PHP mail()
Now we can collect the required information it’s time to e-mail the results. Once again, fairly straight forward and there are plenty of examples online (W3 Schools) of how to use the PHP mail() function.
Step 3 – XML
My specific goal was to create an XML document that could be opened from an e-mail and imported directly into Maximizer. However, this code can be easily modified to plug the data into any XML schema. The full schema for Maximizer can be found in their Administrator Guide. The contents of a basic MXI file is shown below.
<!--?xml version="1.0"?--> Joe Blogs </pre> <address>email@example.com,</address> <pre> 123.456.7890 </pre> <address>1 Beach Road Unit #B Newport Beach CA USA 92663</address> <pre>
This XML document can be created in PHP using the DOMDocument and associated functions.
/* Create XML Document */ $xmlDoc = new DOMDocument('1.0'); /* Build Maximizer XML file */ $xmlRoot = $xmlDoc->createElement('AllData'); $xmlDoc->appendChild($xmlRoot); $xmlIndividual = $xmlDoc->createElement('Individual'); $xmlFirstName = $xmlDoc->createElement('FirstName', $input_firstname); $xmlIndividual->appendChild($xmlFirstName); $xmlLastName = $xmlDoc->createElement('LastName', $input_lastname); $xmlIndividual->appendChild($xmlLastName); /* Continue to add other collected data */ /* No need to create an actual file, just use the content of the created XML document */ $content = $xmlDoc->saveXML();
In this case there is no need create an actual XML file since we are going to be e-mailing it as an attachment. However, we must still encode the XML string before attaching it to the e-mail.
$content = chunk_split(base64_encode($xmlDoc->saveXML()));
Step 4 – Emailing the XML as an Attachment
In order to create an e-mail with an attachment using the PHP mail() function we have to build an e-mail header containing all the correct information. We want to send a multipart message – plain text & an attachment.
From: YourWebsite <info@YourWebsite.com> Reply-To: info@YourWebsite.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="5e7d7cb63cec87fd139c234904f7b693" This is a multi-part message in MIME format. --5e7d7cb63cec87fd139c234904f7b693 Content-type:text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 7bit Name: Joe Blogs Email: firstname.lastname@example.org Phone Number: 123.456.7890 Address: 1 Beach Road Unit #B City: Newport Beach State: CA Zip: 92663 Country: USA Comments: Awesome website! --5e7d7cb63cec87fd139c234904f7b693 Content-Type: application/xml; name="contact.mxi" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="contact.mxi" <!--?xml version="1.0"?--> Joe Blogs </pre> <address>email@example.com</address> <pre> 123.456.7890 </pre> <address>1 Beach Road Unit #B Newport Beach CA USA 92663</address> <pre> --5e7d7cb63cec87fd139c234904f7b693--
Step 5 – WordPress
To make it work inside a WordPress page / post it should be as easy as pasting our code into the page / post / widget where we want the form. Unfortunately WordPress doesn’t let you have any PHP code included inside the contents of a page / post / widget. The solution? A plugin! There are a variety but this one worked for me – PHP Code for posts.
Since the plugin works with “snippets” I found it more manageable to keep the form in a separate file and just include it inside the php snippet as follows.
<!--?php include '/home/account/public_html/mywebform.php';?-->
Now all that is left is including the “PHP Code for posts” shortcode in your page / post / widget.
Step 6 – reCaptcha
In order to add a little more security to the form we can add a reCapthca input that prohibits automated entries being submitted via our form. This shows how to use reCaptcha with PHP.