Suggest User name based on full name and date of birth - html

HTML -
<form action="/addUser" method="post">
<div class="Fields hideme">
<label class="form__fields">Full Name :<span class="star"> * </span></label>
<input class="input" type="text" name="full_name" placeholder="Enter your name" required><br/>
</div>
<div class="Fields hideme">
<label class="form__fields">Address line 1:<span class="star"> * </span></label>
<input class="input" type="text" name="address_line_1" placeholder="Address line 1" required><br/>
</div>
<div class="Fields hideme">
<label class="form__fields">DOB :<span class="star"> * </span></label>
<input class="input" type="date" name="DOB" placeholder="DOB" required><br/>
</div>
</div>
<div class="Fields hideme">
<label class="form__fields">Email :<span class="star"> * </span></label>
<input class="input" type="email" name="email" placeholder="Enter your E-mail" required><br/>
</div>
<div class="Fields hideme">
<input class="Submit_button" type="submit" value="Submit">
</div>
</form>
I am saving this user using node.js
JS -
var nameSchema = new mongoose.Schema({
full_Name: String,
.
.
email: String
});
var User = mongoose.model("User", nameSchema);
const publicPath = path.join(__dirname,'/');
app.use(express.static(publicPath));
app.post("/addUser", (req, res) => {
var myData = new User(req.body);
myData.save()
.then(item => {
res.send("User saved to database");
})
.catch(err => {
res.status(400).send("Unable to save to database");
});
});
I have a html form in which I am asking a user to enter info like Name, DOB, address, Email and I have to suggest User Names based on these inputs to the user.
Please tell me how to send back a suggested userName based on the inputs by the user(full name, DOB, address).

I wanted to write in the comments but I don't have enough reputations.
So I will write it as an answer.
I personally recommend you to use heroku or digitalocean because they are very good for nodejs.
mlab is the best for your DB if you are using MongoDB.
And you can google it or search on youtube there are tons of information.
I hope you will find the answer.

Related

nodejs not listening to post requests and returns error 505

i'm just a begineer to nodejs and I am trying to complete this project and i have been stuck since almost 2 days and i'm unable to figure this out.
it seems nodejs is not listening to POST requests, I am console logging but it instead returns 405 error page.
Here's the code:
import express, { urlencoded } from 'express';
import path, {dirname} from 'path';
import { fileURLToPath } from 'url';
import mysql from 'mysql';
const app = express();
app.use(express.json());
app.use(urlencoded({ extended : true}));
app.use(express.static('public'));
// const __dirname = path.dirname(fileURLToPath(import.meta.url));
app.post('/' , (req, res)=>{
console.log(req.body);
// res.sendFile(path.join(__dirname ,'/public/index.html'));
});
app.listen(5501, ()=>{
console.log('Server is running at port 5501')
})
<form action="/" method="post" id="cont-form">
<h1 class="contact-h1">Contact us</h1>
<div>
<input type="text" name="name" placeholder="Your name" autocomplete="off">
</div>
<div>
<input type="text" name="subject" placeholder="Subject" autocomplete="off">
</div>
<div>
<input type="email" name="email" placeholder="Email" autocomplete="off">
</div>
<div>
<label for="name">
<textarea name="message" placeholder="Type your message" cols="30" rows="10"></textarea>
</label>
</div>
<input type="submit" name="submit" id="submit-ms" value="Send message">
</form>
<div class="buy">
<div class="quantity">
<i class="fa-solid fa-xmark close-buy"></i>
Select Quantity
</div>
<form action='/buy' method="post" >
<div class="inputs">
<div class="buy-input-div" style="margin-top:16px">
<box-icon name='user'></box-icon>
<input type="text" class="buy-input" name="title" id="title-text"></div>
<div class="buy-input-div" >
<box-icon name='user'></box-icon>
<input type="text" class="buy-input" placeholder="Full Name" name="fullname" id="person-name"></div>
<div class="buy-input-div">
<box-icon name='phone'></box-icon>
<input type="text" class="buy-input" placeholder="Phone Number" name="phone" id="phone"></div>
<div class="buy-input-div">
<box-icon name='envelope'></box-icon>
<input type="text" class="buy-input" placeholder="Email" name="email" id=""></div>
<div class="buy-input-div">
<box-icon name='building'></box-icon>
<input type="text" class="buy-input" placeholder="Address" name="address" id=""></div>
<div class="buy-input-div">
<box-icon name='plus'></box-icon>
<input type="number" class="buy-input" placeholder="Quantity" name="quantity" id="">
</div>
<button class="buy-btn">Buy</button>
</div>
</form>
</div>
my file structure:
--root
--node_modules
--public
--index.html
--AdminPanel.html
--styles.css
--script.js
--index.js
--package-lock.json
--package.json
This is how it looks like:
You've got only one endpoint using POST method. Browsers sends only GET requests. (405 means Method not allowed).
You need to define a GET endpoint which will serve the html code. And in browser you have to access that endpoint. When you'll press the submit from you server html it will send a POST request.
Edited: If you access that file statically (opening the html file in a browser) then you need to specify in action property of form tag the full address, since / will simply try the location on your machine

Html Form not submitting data to server

I'm trying to submit Customer data to the server for processing. I'm using an HTML form. But every time I press submit the server doesn't receive any data in the request.
<form data-customer-information-form="true" autocomplete="on" method="POST" action="addticket/submit" name="ticketForm" id="ticketform" accept-charset="UTF-8">
<p>
<label for="customerFirstName">First Name:</label></br>
<input type="text" name="customerFirstName" id="customerFirstName" placeholder="first name" pattern="[A-Za-z]+" tabindex="0">
</p>
<p>
<label for="customerLastName">Last Name:</label></br>
<input type="text" name="customerLastName" id="customerLastName" placeholder="last name" tabindex="0">
</p>
<p>
<label for="phoneNumber">Phone:</label></br>
<input type="tel" id="phone" maxlength="12" name="phoneNumber" placeholder="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" tabindex="0">
</p>
<p>
<label for="email">Email:</label></br>
<input type="email" name="email" id="email" placeholder="email">
</p>
<p>
<label for="service">Computer/Service Name:</label></br>
<input type="text" name="service" placeholder="computer model or service">
</p>
<p>
<label for="description">Anything else we need to know:</label></br>
<textarea type="text" maxlength="200" name="description" id="description" placeholder="What's gone wrong?"></textarea>
</p>
<input type="submit" name="submit" value="Submit" id="submit">
</form>
const router = require('express').Router();
const fs = require('fs');
var path = require('path');
const customer = require("../models/customer");
router.get('/', function(req, res) {
req.session.cookie
res.sendFile(path.join(__dirname + "/pages/ticket.html"));
});
router.post('/submit', (req, res) => {
console.log(req.body)
})
module.exports = router;
The request headers all look good telling the server that is a form. But it doesn't have the data. req.body is undefined basically.
Express.js will only populate the body property of a request object if suitable body parsing middleware is used. You aren't using any.
By default, a form will encode form data in the URL encoded format, so you should use that middleware.
const express = require('express');
router.use(express.urlencoded())

HTML form not sending data in Express

I have the following form in HTML:
<form method="post" id="registration-form" action="/register">
<div class="form-group">
<label for="UsernameRegistration">Username:</label>
<input type="text" class="form-control" id="UsernameRegistration">
</div>
<div class="form-group">
<label for="PasswordRegistration">Password:</label>
<input type="password" class="form-control" id="PasswordRegistration">
</div>
<div class="form-group">
<label for="ConfirmPasswordRegistration">Confirm Password:</label>
<input type="password" class="form-control" id="ConfirmPasswordRegistration">
</div>
<input type="submit" class="form-control" />
</form>
The /register endpoints looks like the following:
router.post('/register', function(req, res, next) {
console.log(req);
});
In req.query and req.body, there is no data. What am I doing wrong?
<input type="password" class="form-control" id="PasswordRegistration">
Here the attribute name is not specified.
It should be like
<input type="password" name="password" class="form-control" id="PasswordRegistration">
You haven't provided name attribute for input elements .
I you provide name attribute to element eg:
<form method="post" id="registration-form" action="/register">
<div class="form-group">
<label for="UsernameRegistration">Username:</label>
<input name="username" type="text" class="form-control" id="UsernameRegistration">
</div>
<div class="form-group">
<label for="PasswordRegistration">Password:</label>
<input name="password" type="password" class="form-control" id="PasswordRegistration">
</div>
<div class="form-group">
<label for="ConfirmPasswordRegistration">Confirm Password:</label>
<input name="confpass" type="password" class="form-control" id="ConfirmPasswordRegistration">
</div>
<input type="submit" class="form-control" />
</form>
router.post("/registration", (req, res) => {
var username = req.params.username;
var pass = req.params.password;
var confpass = req.params.confpass;
})
You will get data in req.params object.
I think you are missing these two things :-
1.Have you added body parser in our app ?? (Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser)
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
Missing name attribute in your form elements

How to store form values on localStorage?

I need to make a JqueryMobile app for college work,
i have that form -
<form name="local_storage_form" method="post" action="#" data-ajax="false" autocomplete="on" data- theme="e" style="padding: 10px;" onsubmit="save_data()">
<label for="id">identity card number</label><input type="text" name="id" id="id" placeholder="identity card number..." data-theme="e" required class="localStore">
<label for="hphone">Home Phone Number</label><input type="tel" name="hphone" id="hphone" placeholder="Home Phone Number..." data-theme="e" class="localStore">
<label for="mphone">Mobile Phone Number</label><input type="text" name="mphone" id="mphone" placeholder="Mobile Phone Number..." data-theme="e" required class="localStore" oninvalid="setCustomValidity('Invaild Phone Number (05********)')">
<label for="bdate">Birth Date</label><input type="date" name="bdate" placeholder="Birth Date..." id="bdate" data-theme="e" required class="localStore">
<label for="email">Email Address</label><input type="email" name="email" id="email" autocomplete="off" placeholder="Email Address..." data-theme="e" required class="localStore">
<button type="button" value="Save" id="Save" data-theme="a">Submit</button>
</form>
i need to store all the details on localstorage and get them after,
how can i do it when i click on the submit button?
btw,it's an offline website so when i click submit it gives me an error.
Have a look into the Jquery API documentation.
$('#local_storage_form').submit(function(e) {
e.preventDefault(); // prevent the form from attempting to send to the web server
var $inputs = $('#local_storage_form :input');
var values = {};
$inputs.each(function() {
values[this.name] = $(this).val();
});
localStorage.setItem('testForm', JSON.stringify(values));
});

How to handle HTML form submission

Thanks in advance for the help. I know this is probably a simple question, as I am relatively new to HTML.
I have a form on my site for people to become members.. I want users to input their information and click submit. When they do that, I want an email to be sent to me. However, I want the user to be redirected to a different page on the site where they will pay for their membership. How can I do both of those things (send email, redirect) at the same time?
Here is my form:
<form>
First name: <input type="text" name="firstname">
Last name: <input type="text" name="lastname"><br>
Street: <input type="text" name="street" size="60"><br>
City: <input type="text" name="city">
State: <input type="text" name="state">
ZIP: <input type="text" name="zip"><br>
Email: <input type="text" name="email" size="60"><br>
Phone: <input type="text" name="phone" size="60"><br>
City/Township: <input type="text" name="votingcity">
Precinct No.: <input type="text" name="precinct"><br>
<br>
<hr>
<br>
<p>Government regulations require the following information:<br></p><br>
Occupation: <input type="text" name="occupation" size="60"><br>
Employer's Name: <input type="text" name="employer" size="60"><br>
Employer's Street: <input type="text" name="emp_street" size="60"><br>
Employer's City: <input type="text" name="emp_city">
Employer's State: <input type="text" name="emp_state">
Employer's ZIP: <input type="text" name="emp_zip"><br>
<br>
<hr>
<br>
Submit <input type="submit">
</form>
Try this at your opening statement it can be done in html it's not pretty but it might work for you
<form action="MAILTO:someone#example.com" method="post" enctype="text/plain">
You should use a server side page for form action to send email and display membership page. When users submit form, they will be directed to this page.
<form method="post" action="link to subscription page. Eg: payment.php">
</form>
In payment.php, display payment form and write an function to send email.
<?php
//Write send email function here
?>
<html>
<!-- Display payment form here -->
</html>
This cannot be done using just HTML, you will need to know a server side scripting language such as PHP or ASP.
How are you processing payments? If I were doing this for myself I would probably have the form data sent to the payment processor (e.g. PayPal) and included in the invoice. I would imagine most payment processors have an option to notify you via email when a payment is received.
But you didn't ask how I would do it, you asked how you can submit a form such that an email is sent and the browser is redirected to another page.
If you can't use my solution I really think you're best off with throwing PHP into the mix.
signup.html
<form action="process.php" method="post">
First name: <input type="text" name="firstname">
Last name: <input type="text" name="lastname"><br>
Street: <input type="text" name="street" size="60"><br>
City: <input type="text" name="city">
State: <input type="text" name="state">
ZIP: <input type="text" name="zip"><br>
Email: <input type="text" name="email" size="60"><br>
Phone: <input type="text" name="phone" size="60"><br>
City/Township: <input type="text" name="votingcity">
Precinct No.: <input type="text" name="precinct"><br>
<br><hr><br>
<p>Government regulations require the following information:<br></p><br>
Occupation: <input type="text" name="occupation" size="60"><br>
Employer's Name: <input type="text" name="employer" size="60"><br>
Employer's Street: <input type="text" name="emp_street" size="60"><br>
Employer's City: <input type="text" name="emp_city">
Employer's State: <input type="text" name="emp_state">
Employer's ZIP: <input type="text" name="emp_zip"><br>
<br><hr><br>
Submit <input type="submit">
</form>
process.php
<?php
$message = '';
foreach($_POST as $key => $value) {
$message .= "$key => $value\n";
}
mail('youraddress#whatever.com','New User Registration',$message);
header('Location: payment.html');
?>
Make sure that <?php is the very beginning of process.php; no preceding text or whitespace of any kind.
The email you'll receive will look something like this:
firstname => Homer
lastname => Simpson
street => 724 Evergreen Terrace
city => Springfield
state => Unknown
zip => ?????
email => hsimpson#compuserve.net
phone => (939) 555-0113
votingcity => Springfield
precinct => N/A
occupation => Nuclear Safety Inspector
employer => C. Montgomery Burns
emp_street => 100 Industrial Way
emp_city => Springfield
emp_state => Unknown
emp_zip => ?????
I recommend you to have a look of PHP tutorials (cf: php.net). The following code will redirect you to an "action.php" page where you will can execute php code (send email, redirect to another, etc.)
index.html
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Action.php
<?php
$message = "your message";
// Send mail
mail('abc#something.com', 'Your Subject', $message);
header('Location: payment.php');
?>
UPDATE:
In your PHP page you can also manage your form data. For example you can access the user first name with this $_POST['firstname']. Or display user informations in you payement page like this:
Hi M. <?php echo htmlspecialchars($_POST['lastname']); ?>.
I don't know what you want to do with the form data, but you can send them in the email or store them in a database.
You can also use other language than PHP such as Java, VB.net, etc. But in my opinion, PHP will probably be easier to learn.
All actions will be taken at server side. so you have to use any server side language. in your case php would be suitable. you can send data from html form after validating all fields. You can send it with ajax or traditional way and if you receive response as ok then you can move to next page.
With jQuery forminteract you can add validation tags in your HTML inputs as i have used below in firstNamelike this.
<form action="process.php" method="post">
First name: <input type="text" name="firstname" data-validation-notNull data-validation-notNull-message="Please enter first Name">
<span data-error="firstName"></span>
Last name: <input type="text" name="lastname"><br>
Street: <input type="text" name="street" size="60"><br>
City: <input type="text" name="city">
State: <input type="text" name="state">
ZIP: <input type="text" name="zip"><br>
Email: <input type="text" name="email" size="60"><br>
Phone: <input type="text" name="phone" size="60"><br>
City/Township: <input type="text" name="votingcity">
Precinct No.: <input type="text" name="precinct"><br>
<br><hr><br>
<p>Government regulations require the following information:<br></p><br>
Occupation: <input type="text" name="occupation" size="60"><br>
Employer's Name: <input type="text" name="employer" size="60"><br>
Employer's Street: <input type="text" name="emp_street" size="60"><br>
Employer's City: <input type="text" name="emp_city">
Employer's State: <input type="text" name="emp_state">
Employer's ZIP: <input type="text" name="emp_zip"><br>
<br><hr><br>
<button type="button" id="btn-submit">Submit</button>
</form>
javascript at bottom of page would be.
var form=$("form").find("[name]").formInteract();
$("#btn-submit").click(function(){
if(!form.validate()){
//return if form is not valid
return;
}
form.postAjax("url", function(rsp){
//do your stuff with response.
});
});
Here is the linke to forminteract project
https://bitbucket.org/ranjeet1985/forminteract
You can use form-endpoint services like formspree.io or formcarry.com they provide email notifications and user redirection after the submit, if you want to use their data later, you can use Formcarry's Zapier integration for Mysql or MongoDB to save the data to the database, or you can set up a webhook to your PHP file, and save processed data.