{"id":7495,"date":"2013-12-18T10:40:00","date_gmt":"2013-12-18T10:40:00","guid":{"rendered":"https:\/\/www.heartinternet.uk\/blog\/?p=7495"},"modified":"2013-12-18T10:40:00","modified_gmt":"2013-12-18T10:40:00","slug":"hostpay-basket-recovery-tutorial","status":"publish","type":"post","link":"https:\/\/www.heartinternet.uk\/blog\/hostpay-basket-recovery-tutorial\/","title":{"rendered":"HostPay: Basket Recovery Tutorial"},"content":{"rendered":"<p>I&rsquo;m a long time user of HostPay, and while I appreciate its reliability and easy integration with Heart Internet&rsquo;s services, I decided to create a bit of code to ask a user for their contact information before choosing a domain, hosting and add-ons.<\/p>\n<p>I put together a simple bit of JavaScript to take the user&rsquo;s email address before they enter anything else. This then sends an email to me, which I can forward to the user to see if they need any help if they haven&rsquo;t signed up after an hour or so.<\/p>\n<p>This tutorial will show you how I did it. Feel free to improve on my code and make changes as you wish. Warning &ndash; I&rsquo;m assuming a reasonable familiarity with Javascript and PHP code, although you can just copy and paste.<\/p>\n<h3><strong>Step 1<\/strong><\/h3>\n<p>First, edit the domain-needed file and put in the following code just before the line<\/p>\n<pre>\n<code>&lt;div id=&#39;t-package-chooser&#39;&gt;<\/code><\/pre>\n<p>If you don&rsquo;t use the domain-needed page, you should put the code in the first page your customer comes to in the sign up process.<\/p>\n<pre>\n&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.9.1\/jquery.min.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;&lt;!-- you dont need etc. --&gt;&#10;&lt;div id=&#39;getemail&#39; style=&#39;display:none&#39;&gt;&#10;&nbsp; &lt;h2&gt;Enter Your Email Address&lt;\/h2&gt;&#10;&nbsp; &lt;p&gt;Start by entering your email address in the box below:&lt;\/p&gt;&#10;&nbsp; &lt;form accept-charset=&#39;utf-8&#39; id=&#39;target&#39; method=&#39;post&#39;&gt;&#10;&nbsp;&nbsp;&nbsp; &lt;p&gt;Your email address: &lt;input type=&#39;email&#39; size=&#39;30&#39; name=&#39;email&#39; id=&#39;emailval&#39; pattern=\"((([a-z]|d|[!#$%&amp;amp;&#39;*+-\/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&amp;amp;&#39;*+-\/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(\\([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?\"\/&gt;&lt;\/p&gt;&#10;&nbsp;&nbsp;&nbsp; &lt;p&gt;&lt;input type=&#39;submit&#39; value=&#39;Continue&#39;\/&gt;&lt;\/p&gt;&#10;&nbsp; &lt;\/form&gt;&#10;&lt;\/div&gt;&#10;&#10;<\/pre>\n<h3><strong>Step 2<\/strong><\/h3>\n<p>Now we have the form, we need some jQuery to process the data. The code below should go toward the bottom of your HTML. It does the following things:<\/p>\n<ul>\n<li>Checks local storage for the email, and if present hides the email request form.<\/li>\n<li>Creates a function which runs when the form is submitted, which firstly checks for a valid email address and then calls&nbsp;newemail.php&nbsp;to send the details to you.<\/li>\n<li>When the email has been sent, the normal domain purchase page will be displayed, with the user&rsquo;s email address already filled in. You&rsquo;ll need to add an<code> id=&#39;username&#39;<\/code> tag to the email field to make this work.<\/li>\n<\/ul>\n<p><code>&lt;script type=\"text\/javascript\"&gt;<br \/> &nbsp; if (window.localStorage &amp;&amp; (!localStorage.isNewUntil || new Date(localStorage.isNewUntil) &lt; new Date())) &#123; $(&#39;#getemail&#39;).show(); $(&#39;#t-package-chooser&#39;).hide() &#125;;<br \/> &nbsp; $(&#39;#target&#39;).submit(function() &#123;<br \/> &nbsp;&nbsp;&nbsp; var new_username = $(&#39;#emailval&#39;).val();<br \/> &nbsp;&nbsp;&nbsp; if (new_username==&#39;&#39;) &#123;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&#39;An email address is required&#39;);<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br \/> &nbsp;&nbsp;&nbsp; &#125;<br \/> &nbsp;&nbsp;&nbsp; if( !new_username.match( new RegExp( $(&#39;#emailval&#39;).attr(\"pattern\") ) ) ) &#123;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(&#39;Your email address is not valid - please re-enter it&#39;);<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br \/> &nbsp;&nbsp;&nbsp; &#125;<br \/> &nbsp;&nbsp;&nbsp; $.post(\"newemail.php\", &#123;email: new_username&#125;, function(data) &#123;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(&#39;input[name=\"username\"]&#39;).val( new_username );<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $(&#39;#getemail&#39;).hide(); $(&#39;#t-package-chooser&#39;).show();<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; localStorage.isNewUntil = new Date(new Date().getTime() + 21 * 86400 * 1000);<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; localStorage.newEmail = new_username;<br \/> &nbsp;&nbsp;&nbsp; &#125;);<br \/> &nbsp;&nbsp;&nbsp; return false;<br \/> &nbsp; &#125;);<br \/> &lt;\/script&gt;<\/code><\/p>\n<h3><strong>Step 3<\/strong><\/h3>\n<p>Almost there now! We need to create&nbsp;<strong>newemail.php<\/strong>&nbsp;and add the code below which simply sends you an email. You should edit this email as you can then forward it directly to the customer.<\/p>\n<p><code>&lt;?php<br \/> &nbsp; if ($_POST[&#39;email&#39;] &amp;&amp; preg_match(\"\/^[[:alnum:].&#39;-]+@[[:alnum:].-]+$\/\", $_POST[&#39;email&#39;])) &#123;<br \/> &nbsp;&nbsp;&nbsp; \/\/ You could add a function here to check if the email address is already on your system<br \/> &nbsp;&nbsp;&nbsp; $headers = &#39;From: &#39;.$_POST[&#39;email&#39;] . \"rn\" .<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;Reply-To: &#39;.$_POST[&#39;email&#39;] . \"rn\" .<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;X-Mailer: PHP\/&#39; . phpversion();<br \/> &nbsp;&nbsp;&nbsp; if (mail(\"YOUR@EMAIL.ADDRESS\", &#39;Web Hosting Signup&#39;, \"Dear Sir\/Madam,<br \/> &nbsp;&nbsp;&nbsp; I notice that you started signing up for a package but didn&#39;t complete the process. Is there any information I can provide you with, or would you like me to give you a call?<br \/> &nbsp;&nbsp;&nbsp; Yours sincerely,<\/code><\/p>\n<p><code>&nbsp;&nbsp;&nbsp; \", $headers)) echo 1; else echo 2;<br \/> &nbsp; &#125; else &#123;<br \/> &nbsp;&nbsp;&nbsp; \/\/ did not get email<br \/> &nbsp; &#125;<br \/> ?&gt;<\/code><\/p>\n<p>The AJAX call from the previous script will read the result of this and continue with the order process.<\/p>\n<h3><strong>Step 4<\/strong><\/h3>\n<p>Finally, edit the login reminder page and add the code below at the bottom. This will look for a cookie for the email address and use it to fill in the email in the form if present. Again, you&rsquo;ll need to add an<code> id=&#39;username&#39;<\/code> tag to the email field.<\/p>\n<p><code>&lt;script&gt;<\/code><\/p>\n<p><code>if(window.localStorage &amp;&amp; localStorage.newEmail!=null) &#123;<\/code><\/p>\n<p><code>&nbsp; $(&#39;#username&#39;).val(localStorage.newEmail); &#125;;<\/code><\/p>\n<p><code>&lt;\/script&gt;<\/code><\/p>\n<p>I hope this helps &#8211; it definitely works well for me. Any recommendations or feedback, let me know in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&rsquo;m a long time user of HostPay, and while I appreciate its reliability and easy integration with Heart Internet&rsquo;s services, I decided to create a bit of code to ask a user for their contact information before choosing a domain, hosting and add-ons.<\/p>\n","protected":false},"author":2,"featured_media":8186,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-7495","post","type-post","status-publish","format-standard","has-post-thumbnail","category-reseller-hosting"],"_links":{"self":[{"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/posts\/7495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/comments?post=7495"}],"version-history":[{"count":0,"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/posts\/7495\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/media\/8186"}],"wp:attachment":[{"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/media?parent=7495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/categories?post=7495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.heartinternet.uk\/blog\/wp-json\/wp\/v2\/tags?post=7495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}