Skip to content

Commit a30dbfc

Browse files
committed
Storing the newsletter emails in firebase DB
1 parent 965e8a5 commit a30dbfc

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

assets/js/main.js

+47-3
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,57 @@ jQuery(document).ready(function() {
203203
// Ensures valid email is supplied
204204
$(".subscribe").click(function () {
205205
if (/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(document.getElementById('email').value)) {
206-
document.getElementById('email').value = ""
207206
document.getElementById('email').placeholder = "Thank you for subscribing to our newsletters."
208207
} else {
209208
document.getElementById('email').value = ""
210209
document.getElementById('email').placeholder = "Please enter a valid email address."
211210
}
212211
});
213212
})(jQuery);
214-
// News letter Form
215-
// use email
213+
214+
215+
216+
// Storing Newsletter emails in Firebase DB
217+
218+
// Your web app's Firebase configuration
219+
var firebaseConfig = {
220+
apiKey: "XXXXXXXXXXX",
221+
authDomain: "XXXXXXXXXXX",
222+
databaseURL: "XXXXXXXXXXX",
223+
projectId: "XXXXXXXXXXX",
224+
storageBucket: "XXXXXXXXXXX",
225+
messagingSenderId: "XXXXXXXXXXX",
226+
appId: "XXXXXXXXXXX",
227+
measurementId: "XXXXXXXXXXX"
228+
};
229+
230+
// Initialize Firebase
231+
firebase.initializeApp(firebaseConfig);
232+
firebase.analytics();
233+
234+
// Reference messages collection
235+
var messagesRef = firebase.database().ref('Emails');
236+
237+
document.getElementById('newsletterForm').addEventListener('submit', submitForm);
238+
239+
//submit form
240+
function submitForm(e){
241+
e.preventDefault();
242+
243+
// Get values
244+
var email = document.getElementById("email").value;
245+
246+
// Save message
247+
saveMessage(email);
248+
249+
// Clear form
250+
document.getElementById('newsletterForm').reset();
251+
}
252+
253+
// Save message to firebase
254+
function saveMessage(email){
255+
var newMessageRef = messagesRef.push();
256+
newMessageRef.set({
257+
email:email
258+
});
259+
}

index.html

+16-6
Original file line numberDiff line numberDiff line change
@@ -820,12 +820,14 @@ <h3>Frequently Asked Questions</h3>
820820
<div class="col-sm-12">
821821
<div class="content">
822822
<h2>SUBSCRIBE TO OUR NEWSLETTER</h2>
823-
<div class="input-group">
824-
<input type="email" class="form-control" id="email" placeholder="Enter your email">
825-
<span class="input-group-btn">
826-
<button class="btn subscribe" type="submit" >Subscribe Now</button>
827-
</span>
828-
</div>
823+
<form id="newsletterForm">
824+
<div class="input-group">
825+
<input type="email" class="form-control" id="email" placeholder="Enter your email">
826+
<span class="input-group-btn">
827+
<button class="btn subscribe" type="submit" >Subscribe Now</button>
828+
</span>
829+
</div>
830+
</form>
829831
</div>
830832
</div>
831833
</div>
@@ -882,6 +884,14 @@ <h4>Contact Us</h4>
882884
<script src="assets/vendor/owl.carousel/owl.carousel.min.js"></script>
883885
<script src="assets/vendor/waypoints/jquery.waypoints.min.js"></script>
884886

887+
<!-- The core Firebase JS SDK is always required and must be listed first -->
888+
<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-app.js"></script>
889+
<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-database.js"></script>
890+
891+
<!-- TODO: Add SDKs for Firebase products that you want to use
892+
https://firebase.google.com/docs/web/setup#available-libraries -->
893+
<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-analytics.js"></script>
894+
885895
<!-- Template Main JS File -->
886896
<script src="assets/js/main.js"></script>
887897

0 commit comments

Comments
 (0)