Skip to content

Commit

Permalink
Added backend in the suggestion form
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushSharma72 committed Nov 1, 2024
1 parent c1d7cf8 commit 4c882b1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
42 changes: 38 additions & 4 deletions F&Q.html
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,15 @@ <h2>Most Frequent Questions</h2>
<!-- Suggestions/Questions Form Section -->
<section id="suggestions-form">
<h2>Suggestions/Questions</h2>
<form id="suggestionForm" action="/submit-suggestion" method="POST">
<form id="suggestionForm" >
<label for="name">Name:</label>
<input type="text" id="name" name="name" required placeholder="Your Name"><br><br>
<input type="text" id="name" required placeholder="Your Name"><br><br>

<label for="email">Email:</label>
<input type="email" id="email" name="email" required placeholder="Your Email"><br><br>
<input type="email" id="email" required placeholder="Your Email"><br><br>

<label for="message">Message:</label><br>
<textarea id="message" name="message" rows="5" required placeholder="Your Message"></textarea><br><br>
<textarea id="message" rows="5" required placeholder="Your Message"></textarea><br><br>

<button type="submit">Submit</button>
</form>
Expand All @@ -406,6 +406,38 @@ <h2>Suggestions/Questions</h2>
</div>
</section>

<script>
document.getElementById("suggestionForm").addEventListener("submit", async function (event) {
event.preventDefault();

const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const message = document.getElementById("message").value;

console.log(name,email,message);
try {
const response = await fetch("http://localhost:5000/api/contact/suggestion", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name, email, message }),
});

const data = await response.json();

if (data.success) {
alert("Suggestion recorded!");
} else {
alert(data.message);
}
} catch (error) {
console.error("Error:", error);
alert("Internal server error");
}
});

</script>

<script>
// Get elements
Expand Down Expand Up @@ -456,6 +488,8 @@ <h2>Suggestions/Questions</h2>
})
</script>





<style>
Expand Down
40 changes: 32 additions & 8 deletions backend/controllers/ContactController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const ContactUs = require("../models/ContactUs");

const FeedbackModal = require("../models/Feedback");

const SuggestionModal = require("../models/suggestion");

const nodemailer = require("nodemailer");

exports.submitContactForm = async (req, res) => {
Expand All @@ -11,7 +13,6 @@ exports.submitContactForm = async (req, res) => {
const newContact = new ContactUs({ name, email, message });
await newContact.save();
return res.status(201).json({ message: "Message sent successfully!" });

} catch (error) {
console.error("Error saving contact form:", error);
return res
Expand Down Expand Up @@ -50,13 +51,6 @@ exports.userfeedback = async (req, res) => {
success: false,
message: "internal server error ",
});
=======
} catch (error) {
console.error("Error saving contact form:", error);
return res
.status(500)
.json({ message: "Failed to send message. Please try again later." });

}
};

Expand Down Expand Up @@ -90,3 +84,33 @@ exports.sendEmail = async (req, resp) => {
}
});
};

exports.suggestion = async (req, resp) => {
try {
const { name, email, message } = req.body;
if (!name || !email || !message) {
return resp.status(400).send({
success: false,
message: "all fields are required",
});
}

const suggestion = await new SuggestionModal({
name: name,
email: email,
message: message,
}).save();

if (suggestion) {
return resp.status(201).send({
success: true,
message: "suggestion recorded",
});
}
} catch (error) {
return resp.status(500).send({
success: false,
message: "intenral server error",
});
}
};
22 changes: 22 additions & 0 deletions backend/models/suggestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require("mongoose");

const SuggestionSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
},
email: {
type: String,
trim: true,
},
message: {
type: String,
trim: true,
},
},
{ timestamps: true }
);

module.exports = mongoose.model("Suggestion", SuggestionSchema);
5 changes: 3 additions & 2 deletions backend/routes/ContactRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ const {
userfeedback,

sendEmail,

suggestion,
} = require("../controllers/ContactController.js");

const router = express.Router();

router.post("/", submitContactForm);


router.post("/feedback", userfeedback);

router.post("/email", sendEmail);

router.post("/suggestion", suggestion);

module.exports = router;

0 comments on commit 4c882b1

Please sign in to comment.