{"id":4554,"date":"2020-09-18T14:28:07","date_gmt":"2020-09-18T21:28:07","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=4554"},"modified":"2020-09-18T14:28:07","modified_gmt":"2020-09-18T21:28:07","slug":"code-share-a-javascript-image-gallery-javascript-php-countdown-timer-and-a-very-basic-javascript-php-email-form","status":"publish","type":"post","link":"http:\/\/www.nathalielawhead.com\/candybox\/code-share-a-javascript-image-gallery-javascript-php-countdown-timer-and-a-very-basic-javascript-php-email-form","title":{"rendered":"Code share: a javascript image gallery, javascript &#038; php countdown timer, and a very basic javascript &#038; php email form&#8230;"},"content":{"rendered":"<p>It&#8217;s been a while since I shared code of things I&#8217;m working on here. Since one of the projects is not yet announced (a secret!), but the code for it is useful, I thought it&#8217;s about time that I do one of these again&#8230;<\/p>\n<p>My last little code project was to FINALLY make a vanilla-javascript based image gallery (just plain javascript, no jquery or anything). I&#8217;ve been putting this off for years, and the last one I had was just a hacked together thing.<br \/>\nI got around to making one that&#8217;s extremely simple, no massive features, and easy to customize!<\/p>\n<p>The resulting gallery is really cool, and I finally have a home for all my animated gif art from my games. I&#8217;ll be regularly updating it with art. <a href=\"http:\/\/unicornycopia.com\/artdump\/\" rel=\"noopener noreferrer\" target=\"_blank\">Check it out here!<\/a><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/noodles\/codeshare\/demo.png\" alt=\"\" \/><\/p>\n<p>This image gallery lists a bunch of thumbnails. If you click on a thumbnail there&#8217;s a small CSS fade transition, and the larger image shows.<br \/>\nIt works on mobile and all the browsers (including IE).<br \/>\nVisitors can even save or share images posted there. If you look at the code there&#8217;s an option for making sharing and saving images harder (by setting backgroundImage to the fullscreen image, instead of it being in it&#8217;s own image element)&#8230;<br \/>\nThere&#8217;s also space for your own header or footer (optional).<br \/>\nThere&#8217;s also optional image and thumbnail captions&#8230;<br \/>\nI mean, that&#8217;s it! Super simple.<\/p>\n<p>* <a href=\"https:\/\/github.com\/alienmelon\/super_simple_image_gallery.js\" rel=\"noopener noreferrer\" target=\"_blank\">Download it on github here.<\/a><br \/>\n* <a href=\"https:\/\/github.com\/alienmelon\/super_simple_image_gallery.js\/blob\/master\/README.md\" rel=\"noopener noreferrer\" target=\"_blank\">Read the documentation for how to customize it here.<\/a><br \/>\n* <a href=\"http:\/\/unicornycopia.com\/artdump\/\" rel=\"noopener noreferrer\" target=\"_blank\">Live demo of it here.<\/a><\/p>\n<p>You are absolutely free to use any and all code from it!<\/p>\n<p>&#8230;So that&#8217;s one thing.<\/p>\n<p>The other project I&#8217;ve been working on is a desktop Electron App. It does a bunch of fun things. It&#8217;s a bit of a web-app too&#8230; I&#8217;m really proud of it and can&#8217;t wait to actually show the finished thing.<\/p>\n<p>In the meantime here&#8217;s some code that I decided to share because I feel like these are oddly specific things where it would be great if people google for it and find an easy copy-paste solution for themselves, without having to extend too much effort in making their own.<\/p>\n<p>&#8212; &#8212; &#8212; &#8212; &#8212; &#8212; &#8212; <\/p>\n<p><strong>Javascript and PHP countdown timer (by server time)<\/strong><\/p>\n<p>The first one is a countdown timer that counts down based on server time. It pings the server every 5 minutes (or you can set that to less if you want), to update the countdown.<br \/>\nThe countdown clock also rounds down the numbers by days (if there are more), then if you get closer to the end date it rounds by hours, and then minutes&#8230;<\/p>\n<p>The thing about countdown timers is that you (sometimes) don&#8217;t really want to use whatever javascript solution, and (in this case) PHP was the easiest solution, because people can set their system clock forward or backward and that kinda is cheating&#8230; In cases where you want something to terminate after a set amount of time.<\/p>\n<p>So here it is! Use whatever you want from it&#8230;<\/p>\n<p>* <a href=\"http:\/\/nathalielawhead.com\/noodles\/codeshare\/countdown.zip\" rel=\"noopener noreferrer\" target=\"_blank\">You can download the code here<\/a><br \/>\n* <a href=\"http:\/\/nathalielawhead.com\/noodles\/codeshare\/countdown\/\" rel=\"noopener noreferrer\" target=\"_blank\">Here&#8217;s a small demo<\/a><\/p>\n<p>The javascript:<br \/>\n&#8211;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/from eventinfo .php<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> eventExpiration;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> serverDate;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> int_timer;\r\n\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> url_to_phpFile <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"countdown.php\"<\/span>;\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> calculateTimeLeft(boolShouldRefresh){\r\n\t\r\n\t<span style=\"color: #888888\">\/\/for the interval, refresh serverDate from php<\/span>\r\n\t<span style=\"color: #888888\">\/\/set to false if called outside of the interval<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(boolShouldRefresh){\r\n\t\t<span style=\"color: #888888\">\/\/get time from server again (update time)<\/span>\r\n\t\tloadServerTimePHP(<span style=\"color: #008800; font-weight: bold\">false<\/span>);\r\n\t}\r\n\t\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> num_currentTime <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> <span style=\"color: #007020\">Date<\/span>(<span style=\"color: #007020\">Date<\/span>.parse(serverDate));\r\n\t<span style=\"color: #888888\">\/\/will expire at midnight that day<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> num_targetDate <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> <span style=\"color: #007020\">Date<\/span>(eventExpiration);\r\n\t\r\n\t<span style=\"color: #888888\">\/\/parse it for calculating<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> distance <span style=\"color: #333333\">=<\/span> num_targetDate <span style=\"color: #333333\">-<\/span> num_currentTime;\r\n\t<span style=\"color: #888888\">\/\/Time calculations for days, hours, minutes and seconds<\/span>\r\n\t<span style=\"color: #888888\">\/\/values calculated for the timer<\/span>\r\n\t<span style=\"color: #888888\">\/\/math...<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> seconds<span style=\"color: #333333\">=<\/span><span style=\"color: #007020\">Math<\/span>.floor(distance<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">1000<\/span>);\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> minutes<span style=\"color: #333333\">=<\/span><span style=\"color: #007020\">Math<\/span>.floor(seconds<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">60<\/span>);\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> hours<span style=\"color: #333333\">=<\/span><span style=\"color: #007020\">Math<\/span>.floor(minutes<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">60<\/span>);\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> days<span style=\"color: #333333\">=<\/span><span style=\"color: #007020\">Math<\/span>.floor(hours<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">24<\/span>);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #888888\">\/\/math...<\/span>\r\n\tseconds<span style=\"color: #333333\">%=<\/span><span style=\"color: #0000DD; font-weight: bold\">60<\/span>;\r\n\tminutes<span style=\"color: #333333\">%=<\/span><span style=\"color: #0000DD; font-weight: bold\">60<\/span>;\r\n\thours<span style=\"color: #333333\">%=<\/span><span style=\"color: #0000DD; font-weight: bold\">24<\/span>;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #888888\">\/\/numbers to strings <\/span>\r\n\tsec <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">String<\/span>(seconds);\r\n\tmin <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">String<\/span>(minutes);\r\n\thrs <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">String<\/span>(hours);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\td <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">String<\/span>(days);\r\n\t<span style=\"color: #888888\">\/\/if single digit \"0\" infront<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (sec.length<span style=\"color: #333333\"><<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) {\r\n\t\tsec<span style=\"color: #333333\">=<\/span><span style=\"background-color: #fff0f0\">\"0\"<\/span><span style=\"color: #333333\">+<\/span>sec;\r\n\t}\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (min.length<span style=\"color: #333333\"><<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) {\r\n\t\tmin<span style=\"color: #333333\">=<\/span><span style=\"background-color: #fff0f0\">\"0\"<\/span><span style=\"color: #333333\">+<\/span>min;\r\n\t}\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (hrs.length<span style=\"color: #333333\"><<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) {\r\n\t\thrs<span style=\"color: #333333\">=<\/span><span style=\"background-color: #fff0f0\">\"0\"<\/span><span style=\"color: #333333\">+<\/span>hrs;\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/the above formatted<\/span>\r\n\tt <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">String<\/span>(hrs) <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\" hours, \"<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"color: #007020\">String<\/span>(min) <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\" minutes.\"<\/span>;\r\n\t<span style=\"color: #888888\">\/\/format days to hide, be plural, or singular...<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #007020\">String<\/span>(days)<span style=\"color: #333333\"><<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>){\r\n\t\td <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"\"<\/span>;\r\n\t}<span style=\"color: #008800; font-weight: bold\">else<\/span> <span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #007020\">String<\/span>(days)<span style=\"color: #333333\"><<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>){\r\n\t\td <span style=\"color: #333333\">=<\/span> days <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\" Day, \"<\/span>;\r\n\t}<span style=\"color: #008800; font-weight: bold\">else<\/span>{\r\n\t\td <span style=\"color: #333333\">=<\/span> days <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\" Days\"<\/span>;\r\n\t\tt <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"\"<\/span>; <span style=\"color: #888888\">\/\/hide the time because there are too many days<\/span>\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/atesat set asfasd<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> str_formatted_time <span style=\"color: #333333\">=<\/span> d <span style=\"color: #333333\">+<\/span> t;<span style=\"color: #888888\">\/\/ + \":\" + String(sec);<\/span>\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\r\n\t<span style=\"color: #007020\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0\">\"_countdown\"<\/span>).innerHTML <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"<span id=&#39;id_countdown_title&#39;>\"<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\"Event time left: \"<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\"<\/span>\"<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\"<span id=&#39;id_countdown_number&#39;>\"<\/span> <span style=\"color: #333333\">+<\/span> str_formatted_time <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\"<\/span><br>\"<\/span>;\r\n\t\r\n\t<span style=\"color: #888888\">\/\/start or stop the app here, hide ALL UI, throw error!<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (num_currentTime<span style=\"color: #333333\">>=<\/span>num_targetDate) {\r\n\t\t<span style=\"color: #888888\">\/\/event is over<\/span>\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">\"The event is over\"<\/span>);\r\n\t\t<span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\t}<span style=\"color: #008800; font-weight: bold\">else<\/span>{\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">\"The event is still ongoing\"<\/span>);\r\n\t\t<span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\t}\r\n\t\r\n\tconsole.log(str_formatted_time);\r\n}\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> start_timer(){\r\n\t<span style=\"color: #888888\">\/\/timer fires off every 5 minutes<\/span>\r\n\t<span style=\"color: #888888\">\/\/timer is therefore updated every five minutes<\/span>\r\n\t<span style=\"color: #888888\">\/\/this keeps the server from getting flooded with requests to check the time every second<\/span>\r\n\tclearInterval(int_timer);\r\n\tint_timer <span style=\"color: #333333\">=<\/span> setInterval(calculateTimeLeft, <span style=\"color: #0000DD; font-weight: bold\">300000<\/span>, [<span style=\"color: #008800; font-weight: bold\">true<\/span>]);\r\n}\r\n\r\n<span style=\"color: #888888\">\/\/LOAD THE PHP<\/span>\r\n<span style=\"color: #888888\">\/\/load the date information<\/span>\r\n<span style=\"color: #888888\">\/\/date is taken from server to prevent cheating<\/span>\r\n<span style=\"color: #888888\">\/\/server time is permanent<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> loadServerTimePHP(){\r\n\t<span style=\"color: #888888\">\/\/load the event info php and get all values from that array<\/span>\r\n\t<span style=\"color: #888888\">\/\/set the download locations too<\/span>\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> req <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> XMLHttpRequest();\r\n\t<span style=\"color: #888888\">\/\/listeners...<\/span>\r\n\treq.onload <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>() {\r\n\t\t\r\n\t\t<span style=\"color: #888888\">\/\/get the array values, set vars...<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> arr <span style=\"color: #333333\">=<\/span> JSON.parse(<span style=\"color: #008800; font-weight: bold\">this<\/span>.responseText);\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\teventExpiration <span style=\"color: #333333\">=<\/span> arr[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>];\r\n\t\tserverDate <span style=\"color: #333333\">=<\/span> arr[<span style=\"color: #0000DD; font-weight: bold\">1<\/span>];\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">\"eventExpiration: \"<\/span> <span style=\"color: #333333\">+<\/span> eventExpiration);\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">\"serverDate: \"<\/span> <span style=\"color: #333333\">+<\/span> serverDate);\r\n\t\t<span style=\"color: #888888\">\/\/for debug pruposes show time and expiration...<\/span>\r\n\t\t<span style=\"color: #007020\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0\">\"_currTime\"<\/span>).innerHTML <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"Event expiration: \"<\/span> <span style=\"color: #333333\">+<\/span> eventExpiration <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\"<br>Current Server Time: \"<\/span> <span style=\"color: #333333\">+<\/span> serverDate;\r\n\t\t\r\n\t\tcalculateTimeLeft(<span style=\"color: #008800; font-weight: bold\">false<\/span>);<span style=\"color: #888888\">\/\/call on first run to populate text field<\/span>\r\n\t\t<span style=\"color: #888888\">\/\/successfully loaded, start the timer<\/span>\r\n\t\tstart_timer();\r\n\t\t\r\n\t\t\r\n\t};\r\n\t\r\n\treq.loadend <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>() {\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">\"load ended\"<\/span>);\r\n\t}\t\r\n\t<span style=\"color: #888888\">\/\/on errors, call again...<\/span>\r\n\treq.onerror <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>(){\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">\"error loading php\"<\/span>);\r\n\t}\r\n\treq.ontimeout <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>(){\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">\"timeout loading php\"<\/span>);\r\n\t}\r\n\t\r\n\t<span style=\"color: #888888\">\/\/start...<\/span>\r\n\treq.open(<span style=\"background-color: #fff0f0\">\"get\"<\/span>, url_to_phpFile, <span style=\"color: #008800; font-weight: bold\">true<\/span>);\r\n\treq.send();\r\n\t\r\n}\r\n\r\n<span style=\"color: #888888\">\/\/CALL loadServerTimePHP() to start<\/span>\r\n<\/pre>\n<\/div>\n<p>&#8211;<\/p>\n<p>And the PHP file:<br \/>\n&#8211;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #557799\"><?php<\/span> \r\n<span style=\"color: #888888\">\/\/set event to your desired timezone<\/span>\r\n<span style=\"color: #888888\">\/\/see: https:\/\/www.php.net\/manual\/en\/function.date-default-timezone-set.php<\/span>\r\n<span style=\"color: #007020\">date_default_timezone_set<\/span>(<span style=\"background-color: #fff0f0\">&#39;America\/Los_Angeles&#39;<\/span>);\r\n\r\n<span style=\"color: #888888\">\/\/CHANGE THIS VALUE TO SET TO DESIRED EVENT EXPIRATION<\/span>\r\n<span style=\"color: #888888\">\/\/the format is imporant<\/span>\r\n<span style=\"color: #888888\">\/\/month should be spelled out, day and year must be numbers<\/span>\r\n<span style=\"color: #888888\">\/\/MONTH DAY YEAR<\/span>\r\n<span style=\"color: #996633\">$eventExpiration<\/span> <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"September 20 2030\"<\/span>;\r\n\r\n<span style=\"color: #888888\">\/\/DO NOT change these...<\/span>\r\n<span style=\"color: #996633\">$serverdate<\/span> <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">date<\/span>(<span style=\"background-color: #fff0f0\">&#39;D M d Y H:i:s O&#39;<\/span>);\r\n<span style=\"color: #996633\">$arr<\/span> <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">array<\/span>(<span style=\"color: #996633\">$eventExpiration<\/span>, <span style=\"color: #996633\">$serverdate<\/span>);\r\n\r\n<span style=\"color: #888888\">\/\/send as array<\/span>\r\n<span style=\"color: #888888\">\/\/echo( json_encode($arr) );<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">echo<\/span> <span style=\"color: #007020\">json_encode<\/span>(<span style=\"color: #996633\">$arr<\/span>);\r\n\r\n<span style=\"color: #557799\">?><\/span>\r\n<\/pre>\n<\/div>\n<p>&#8211;<\/p>\n<p>Note that you need to upload these and access them from your server in order for them to work.<\/p>\n<p>&#8212; &#8212; &#8212; &#8212; &#8212; &#8212; &#8212; <\/p>\n<p><strong>Super basic PHP email form (absolutely no frills)<\/strong><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/noodles\/codeshare\/form.png\" alt=\"\" \/><\/p>\n<p>The other thing is a simple PHP email form. There&#8217;s a lot out there that do fancy checking, and regex, and <a href=\"https:\/\/nodemailer.com\/about\/\" rel=\"noopener noreferrer\" target=\"_blank\">Node Mailer<\/a> is definitely something, then there&#8217;s <a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\" rel=\"noopener noreferrer\" target=\"_blank\">PHPmailer<\/a>&#8230; but in this case I really needed an absolutely basic one that just sends an email (even if it&#8217;s from a bogus address).<\/p>\n<p>Note that there is an issue with gmail addresses receiving mail from <a href=\"https:\/\/www.php.net\/manual\/en\/function.mail.php\" rel=\"noopener noreferrer\" target=\"_blank\">php&#8217;s mail<\/a>. <a href=\"https:\/\/stackoverflow.com\/questions\/10021655\/php-mail-function-will-not-send-to-gmail-but-will-send-to-my-non-gmail-account\" rel=\"noopener noreferrer\" target=\"_blank\">See this stackoverflow discussion<\/a>.<br \/>\nYou will have to set up an email account on your own server (non-gmail) that catches and forwards these emails to your gmail, if you want to use gmail for this&#8230; or use a better solution like NodeMailer or PHPMailer.<\/p>\n<p>Here it is&#8230;<\/p>\n<p>* <a href=\"http:\/\/nathalielawhead.com\/noodles\/codeshare\/mailer.zip\" rel=\"noopener noreferrer\" target=\"_blank\">Download the code here.<\/a><\/p>\n<p>The Javascript:<\/p>\n<p>&#8211;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #333333\"><<\/span>script<span style=\"color: #333333\">><\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold\">function<\/span> init(){\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888\">\/\/ui...<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> inputName <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0\">\"txt_inputName\"<\/span>);\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> inputEmail <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0\">\"txt_inputEmail\"<\/span>);\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> inputMessage <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0\">\"_feedbackForm\"<\/span>);\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> btn_send <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.getElementById(<span style=\"background-color: #fff0f0\">\"_submitBtn\"<\/span>);\r\n\t\t\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> urlPhp <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"mailer.php\"<\/span>;\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> _feedbackFor <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"YOUR NAME HERE\"<\/span>;\r\n\t\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> _subject <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"subject\"<\/span>;\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> _name <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"name\"<\/span>;\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> _from <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"email\"<\/span>;\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> _message <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"message\"<\/span>;\r\n\t\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888\">\/\/get values when ready<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">function<\/span> getInput(){\r\n\t\t\t\t_name <span style=\"color: #333333\">=<\/span> inputName.value;\r\n\t\t\t\t_from <span style=\"color: #333333\">=<\/span> inputEmail.value;\r\n\t\t\t\t_message <span style=\"color: #333333\">=<\/span> inputMessage.value;\r\n\t\t\t\t<span style=\"color: #888888\">\/\/make subject<\/span>\r\n\t\t\t\t_subject <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"Feedback for: \"<\/span> <span style=\"color: #333333\">+<\/span> _feedbackFor <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">\" from \"<\/span> <span style=\"color: #333333\">+<\/span> _name;\r\n\t\t\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t\t\tconsole.log(_name, _from, _subject, _message)\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">function<\/span> submitInput(){\r\n\t\t\t\tgetInput();\r\n\t\t\t\t\r\n\t\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> xmlhttp<span style=\"color: #333333\">=<\/span><span style=\"color: #008800; font-weight: bold\">new<\/span> XMLHttpRequest();\r\n\t\t\t\txmlhttp.onreadystatechange<span style=\"color: #333333\">=<\/span><span style=\"color: #008800; font-weight: bold\">function<\/span>() {\r\n\t\t\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (<span style=\"color: #008800; font-weight: bold\">this<\/span>.readyState<span style=\"color: #333333\">==<\/span><span style=\"color: #0000DD; font-weight: bold\">4<\/span> <span style=\"color: #333333\">&&<\/span> <span style=\"color: #008800; font-weight: bold\">this<\/span>.status<span style=\"color: #333333\">==<\/span><span style=\"color: #0000DD; font-weight: bold\">200<\/span>) {\r\n\t\t\t\t\t\t<span style=\"color: #888888\">\/\/get a reply (this shows it works...)<\/span>\r\n\t\t\t\t\t\tconsole.log(<span style=\"color: #008800; font-weight: bold\">this<\/span>.responseText);\r\n\t\t\t\t\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t\t\t\t\tinputMessage.value <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"Your feedback has been sent!\"<\/span>;\r\n\t\t\t\t\t\t<span style=\"color: #888888\">\/\/close the form!!<\/span>\r\n\t\t\t\t\t\t<span style=\"color: #888888\">\/\/window.close();<\/span>\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\txmlhttp.open(<span style=\"background-color: #fff0f0\">\"GET\"<\/span>,urlPhp<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">\"?subject=\"<\/span><span style=\"color: #333333\">+<\/span>_subject<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">\"&name=\"<\/span><span style=\"color: #333333\">+<\/span>_name<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">\"&message=\"<\/span><span style=\"color: #333333\">+<\/span>_message<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">\"&frommail=\"<\/span><span style=\"color: #333333\">+<\/span>_from,<span style=\"color: #008800; font-weight: bold\">true<\/span>);\r\n\t\t\t\txmlhttp.send();\r\n\t\t\t\t\r\n\t\t\t\t<span style=\"color: #888888\">\/\/close this when done (incase window.close doesn&#39;t work)...<\/span>\r\n\t\t\t\tbtn_send.style.visibility <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"hidden\"<\/span>;\r\n\t\t\t\tinputName.style.visibility <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"hidden\"<\/span>;\r\n\t\t\t\tinputEmail.style.visibility <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">\"hidden\"<\/span>;\r\n\t\t\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t\t\tbtn_send.removeEventListener(<span style=\"background-color: #fff0f0\">\"click\"<\/span>, submitInput);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t<span style=\"color: #888888\">\/\/UI<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">function<\/span> clearOnFocus(event){\r\n\t\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> field <span style=\"color: #333333\">=<\/span> event.currentTarget;\r\n\t\t\t\tfield.value <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&#39;&#39;<\/span>;\r\n\t\t\t\t<span style=\"color: #888888\">\/\/only do this once...<\/span>\r\n\t\t\t\tfield.removeEventListener(<span style=\"background-color: #fff0f0\">\"focus\"<\/span>, clearOnFocus);\r\n\t\t\t\tconsole.log(field.name);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\r\n\t\t\tinputName.addEventListener(<span style=\"background-color: #fff0f0\">\"focus\"<\/span>, clearOnFocus);\r\n\t\t\tinputEmail.addEventListener(<span style=\"background-color: #fff0f0\">\"focus\"<\/span>, clearOnFocus);\r\n\t\t\tinputMessage.addEventListener(<span style=\"background-color: #fff0f0\">\"focus\"<\/span>, clearOnFocus);\r\n\t\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t\tbtn_send.addEventListener(<span style=\"background-color: #fff0f0\">\"click\"<\/span>, submitInput);\r\n\t\t\t\r\n\t\t}\r\n\t\t\r\n\t<span style=\"color: #333333\"><<\/span><span style=\"color: #FF0000; background-color: #FFAAAA\">\/script><\/span>\r\n<\/pre>\n<\/div>\n<p>&#8211;<\/p>\n<p>And the PHP file:<\/p>\n<p>&#8211;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #557799\"><?<\/span>\r\n\t<span style=\"color: #888888\">\/\/in the future use PHPMailer https:\/\/github.com\/PHPMailer\/PHPMailer<\/span>\r\n\t<span style=\"color: #888888\">\/\/var $name;<\/span>\r\n\t<span style=\"color: #996633\">$name<\/span><span style=\"color: #333333\">=<\/span><span style=\"color: #996633\">$_GET<\/span>[<span style=\"background-color: #fff0f0\">\"name\"<\/span>];\r\n\t<span style=\"color: #996633\">$message<\/span><span style=\"color: #333333\">=<\/span><span style=\"color: #996633\">$_GET<\/span>[<span style=\"background-color: #fff0f0\">\"message\"<\/span>];\r\n\t<span style=\"color: #996633\">$subject<\/span><span style=\"color: #333333\">=<\/span><span style=\"color: #996633\">$_GET<\/span>[<span style=\"background-color: #fff0f0\">\"subject\"<\/span>];\r\n\t<span style=\"color: #996633\">$frommail<\/span><span style=\"color: #333333\">=<\/span><span style=\"color: #996633\">$_GET<\/span>[<span style=\"background-color: #fff0f0\">\"frommail\"<\/span>];\r\n\t\r\n\t<span style=\"color: #888888\">\/\/CHANGE THIS TO YOUR EMAIL<\/span>\r\n\t<span style=\"color: #996633\">$to<\/span> <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&#39;<you@your_email.com>&#39;<\/span>;\r\n\t\r\n\t<span style=\"color: #996633\">$headers<\/span> <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&#39;From: &#39;<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"color: #996633\">$frommail<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"background-color: #fff0f0\">\"<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0\">\\r\\n<\/span><span style=\"background-color: #fff0f0\">\"<\/span> <span style=\"color: #333333\">.<\/span>\r\n\t    <span style=\"background-color: #fff0f0\">&#39;Reply-To: &#39;<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"color: #996633\">$frommail<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"background-color: #fff0f0\">\"<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0\">\\r\\n<\/span><span style=\"background-color: #fff0f0\">\"<\/span> <span style=\"color: #333333\">.<\/span>\r\n\t    <span style=\"background-color: #fff0f0\">&#39;X-Mailer: PHP\/&#39;<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"color: #007020\">phpversion<\/span>();\r\n\r\n\t<span style=\"color: #007020\">mail<\/span>(<span style=\"color: #996633\">$to<\/span>, <span style=\"color: #996633\">$subject<\/span>, <span style=\"color: #996633\">$message<\/span>, <span style=\"color: #996633\">$headers<\/span>);\r\n\t\r\n\t<span style=\"color: #888888\">\/\/echo reply to see if it worked...<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">echo<\/span> <span style=\"background-color: #fff0f0\">\"Message success: \"<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"background-color: #fff0f0\">\" \"<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"color: #996633\">$name<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"background-color: #fff0f0\">\" \"<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"color: #996633\">$message<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"background-color: #fff0f0\">\" \"<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"color: #996633\">$subject<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"background-color: #fff0f0\">\" \"<\/span> <span style=\"color: #333333\">.<\/span> <span style=\"color: #996633\">$frommail<\/span>;\r\n<span style=\"color: #557799\">?><\/span>\r\n<\/pre>\n<\/div>\n<p>&#8211;<\/p>\n<p>Again, you&#8217;ll have to upload this to your server and access it there&#8230; in order for this to work.<br \/>\nAlso, be sure to update the .php file with your desired email address.<\/p>\n<p>&#8212; &#8212; &#8212; &#8212; &#8212; &#8212; &#8212; <\/p>\n<p>Ok, that&#8217;s all!<\/p>\n<p>I hope someone coming in from google finds some of this useful. :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a while since I shared code of things I&#8217;m working on here. Since one of the projects is not yet announced (a secret!), but the code for it is useful, I thought it&#8217;s about time that I do one of these again&#8230; My last little code project was to FINALLY make a vanilla-javascript based image gallery (just plain javascript, no jquery or anything). I&#8217;ve been putting this off for years, and the last one I had was just a hacked together thing. I got around to making one that&#8217;s extremely simple, no massive features, and easy to customize!&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4556,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"http:\/\/nathalielawhead.com\/noodles\/codeshare\/demo.png","fifu_image_alt":"Code share: a javascript image gallery, javascript & php countdown timer, and a very basic javascript & php email form\u2026","footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-4554","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/4554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/comments?post=4554"}],"version-history":[{"count":4,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/4554\/revisions"}],"predecessor-version":[{"id":4559,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/4554\/revisions\/4559"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media\/4556"}],"wp:attachment":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media?parent=4554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=4554"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=4554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}