When I click autofill, It only fills the first input. Is there a way to make it autofill, the other inputs. I am using reactjs.
<div className={`${styles.formGroup} ${styles.address}`}>
<label className={styles.label} htmlFor="address">
Address
</label>
<input
value={address}
onChange={(e) => setAddress((prev) => e.target.value)}
className={styles.input}
type="text"
id="address"
name="address"
required
/>
</div>
<div className={`${styles.formGroup} ${styles.address}`}>
<label className={styles.label} htmlFor="address">
Apartment
</label>
<input
value={apartment}
onChange={(e) => setApartment((prev) => e.target.value)}
className={styles.input}
type="text"
id="address"
name="address"
required
/>
</div>
<div className={`${styles.formGroup} `}>
<label className={styles.label} htmlFor="governorate">
Governorate
</label>
<Select
options={governorates}
value={governorate}
onChange={(e) => setGovernorate((prev) => e)}
styles={{
control: (baseStyles, state) => ({
...baseStyles,
borderRadius: "4px",
padding: "4.6px",
}),
}}
/>
</div>
How do I make it so that it automatically fills the other inputs
Related
<div>
<body >
<div class="containerr flex m-0 p-0 absolute">
<div class="leftPart w-[50%] h-screen bg-red-600">
<img src={SignupImg}></img>
</div>
<div class="rightPart w-[50%] h-screen bg-blue-600">
<div class="main">
<div class="test1 w-[320px] h-[100%] bg-green-500">
<form onSubmit={onSubmit}>
<label>Sign up</label>
<input class="w-60 h-10 my-3 ml-5 text-[#573b8a] flex rounded-[4px] font-medium indent-1.5 border-[1px] border-b-[1px] border-b-[#949090] border-[#b4adad] mb-6 focus:bg-[#e0dede]" type="text" placeholder="Name" name='name' id='name' required="" value={name} onChange={onChange} />
<input type="date"placeholder="Date of Birth" required="" name='dob'id='dob' value={dob} onChange={onChange} />
<input type="tel" placeholder="Phone Number" name='phone' id='phone' required="" value={phone} onChange={onChange} />
<input type="email" placeholder="Email" name='email' id = 'email' required="" value={email} onChange={onChange} />
<select type="text" placeholder="Location" name='location' id = 'location' required="" value={location} onChange={onChange} >
<option value="" selected disabled hidden>Select an Option</option>
{
locations.map((location,i)=>{
return(
<option>{location.city}</option>
)
})
}
</select>
<input type="Password" placeholder="Password" name='password' id='password' required="" value={password} onChange={onChange}/>
<input type="password" placeholder="Confirm Password" name='ConfirmPassword' id='Confirm Password' required="" value={ConfirmPassword} onChange={onChange}/>
<button >Sign up</button>
</form>
</div>
</div>
</div>
</div>
</body>
</div>
Im trying to create a slpit columned sign up page where I'd like the right part of my website to include the sign up form in a design but when I try creating a div with the specified width and heights the parent div shrinks to that size as well and I can;t quite figure out how to not keep that from happening as I want my parent width and height to not change from the defined values. I've attached an image to better clarify the issue. The class named "test1" is the one I'm facing issues with "the green coloured div"
After tweaking the child div class="test1"
Without tweaking the child div class="test1"
SERVICE.TS
addP(nome: string, cognome: string, anno_n: string): Observable<any> {
return this.http.post<Partecipanti>(this.partecipantiUrl, {
nome: nome,
cognome: cognome,
anno_n: anno_n
}, this.httpOptions).pipe(
tap((newPartecipante: Partecipanti) => this.log(`partecipante aggiunto w/ id=${newPartecipante.id}`)),
catchError(this.handleError<Partecipanti>('addP'))
);
}
COMPONENT.TS
add(nome: string, cognome: string, anno_n: string): void {
this.PartecipantiService.addP(nome, cognome, anno_n).subscribe(res => {
console.log(res);
this.loadUser();
})
}
<form class="row row-cols-lg-auto g-3 align-items-center float-center" (ngSubmit)="add(nome.value, cognome.value, anno_n.value)" (ngSubmit)="loadUser()" style="justify-content: center;">
<div class="col-12">
<label class="visually-hidden" for="inlineFormInputGroupUsername">Nome</label>
<div class="input-group">
<input type="text" class="form-control" id="inlineFormInputGroupUsername" required #nome placeholder="Nome">
</div>
</div>
<div class="col-12">
<label class="visually-hidden" for="inlineFormInputGroupUsername">Cognome</label>
<div class="input-group">
<input type="text" class="form-control" id="inlineFormInputGroupUsername" required #cognome placeholder="Cognome">
</div>
</div>
<div class="col-12">
<label class="visually-hidden" for="inlineFormInputGroupUsername">Data di nascita</label>
<div class="input-group">
<input type="text" class="form-control" id="inlineFormInputGroupUsername" required #anno_n useValueAsDate placeholder="Data di nascita (GG/MM/AAAA)">
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary shadow-lg">Salva</button>
</div>
</form>
How can I take the date value in my function "add"?
In my function add I have 3 field (name, surname and date), when I call my function add on a button I use (ngSubmit)="add(name.value, surname.value, date...?). What I have to use? Value is for string, I can't find something for Date! This part is on component.html Can you explain me how it works? My input is type="date"
I would suggest you use ngForm. This is the easiest for handling this scenario.
See here the Documentation: https://angular.io/api/forms/NgForm
Here is a working example i did: https://stackblitz.com/edit/angular-ivy-pqgewm?devtoolsheight=33&file=src/app/app.component.html
In Essence you want your HTML look like this:
<form #form="ngForm" (ngSubmit)="userService.submitForm(form)">
<div>
<label for="firstName">First Name</label>
<input name="firstName" id="firstName" type="text" ngModel required />
</div>
<div>
<label for="lastName">Last Name</label>
<input name="lastName" id="lastName" ngModel type="text" required />
</div>
<div>
<label for="dateOfBirth">Date of Birth</label>
<input name="dateOfBirth" id="dateOfBirth" ngModel type="date" required />
</div>
<br />
<button type="submit">Submit</button>
</form>
And your Submit function to look like this:
submitForm(form: NgForm) {
if (form.valid) {
const user: User = {
name: form.value['firstName'],
surname: form.value['lastName'],
birth: new Date(form.value['dateOfBirth']),
};
}
}
So I have a simple form with a few inputs and two radio buttons, but when I click any of them, they seem like they don't react. What could be the problem?
import { useState } from "react";
import uuid from "react-uuid";
import "./style.css";
export default function LargeForm() {
const [people, setPeople] = useState([]);
const [newUser, setNewUser] = useState({
id: uuid(),
firstName: "",
lastName: "",
sex: "",
dateOfBirth: "",
email: "",
contact: "",
password: ""
});
const handleChange = (value, type) => {
setNewUser((prev) => {
return { ...prev, [type]: value };
});
};
const handleSubmit = (e) => {
e.preventDefault();
if (
newUser.firstName &&
newUser.lastName &&
newUser.dateOfBirth &&
newUser.email &&
newUser.contact &&
newUser.password
) {
setPeople([...people, newUser]);
setNewUser({
firstName: "",
lastName: "",
sex: "",
dateOfBirth: "",
email: "",
contact: "",
password: ""
});
} else {
console.log("Error!");
}
};
return (
<>
<form className="container" autoComplete="off" onSubmit={handleSubmit}>
<div>
<label htmlFor="firstName" className="label">
First name:{" "}
</label>
<input
type="text"
id="firstName"
name="firstName"
className="input"
value={newUser.firstName}
onChange={(e) => handleChange(e.target.value, "firstName")}
/>
<br />
<label htmlFor="lastName" className="label">
Last name:{" "}
</label>
<input
type="text"
id="lastName"
name="lastName"
className="input"
value={newUser.lastName}
onChange={(e) => handleChange(e.target.value, "lastName")}
/>
<br />
<label htmlFor="sex" className="label">
Sex:
</label>
<input
type="radio"
id="male"
name="sex"
value={newUser.sex}
onChange={(e) => handleChange(e.target.value, "sex")}
/>
<label for="male">Male</label> {" "}
<input
type="radio"
id="female"
name="sex"
value={newUser.sex}
onChange={(e) => handleChange(e.target.value, "sex")}
/>
<label for="female">Female</label>
<br />
<label htmlFor="dateOfBirht" className="label">
Date of birth:{" "}
</label>
<input
type="date"
id="dateOfBirht"
name="dateOfBirht"
className="input"
value={newUser.dateOfBirth}
onChange={(e) => handleChange(e.target.value, "dateOfBirth")}
/>
<br />
<label htmlFor="email" className="label">
Email:{" "}
</label>
<input
type="email"
id="Email"
name="Email"
className="input"
value={newUser.email}
onChange={(e) => handleChange(e.target.value, "email")}
/>
<br />
<label htmlFor="contact" className="label">
Contact:{" "}
</label>
<input
type="text"
id="contact"
name="contact"
className="input"
value={newUser.Contact}
onChange={(e) => handleChange(e.target.value, "contact")}
/>
<br />
<label htmlFor="password" className="label">
Password:{" "}
</label>
<input
type="password"
id="password"
name="password"
className="input"
value={newUser.password}
onChange={(e) => handleChange(e.target.value, "password")}
/>
</div>
<br />
<button type="submit" className="btn">
Submit
</button>
</form>
{people.map((person) => {
return (
<div className="list" key={person.id}>
{person.firstName}
<br />
{person.lastName}
<br />
{person.sex}
<br />
{person.dateOfBirth}
<br />
{person.email}
<br />
{person.contact}
<br />
{person.password}
</div>
);
})}
</>
);
}
The CodeSandbox link is given below.
https://codesandbox.io/s/learning-react-5vj5g?file=/src/useReducer/exampleForm/LargeForm.js
In your code you are setting handleSubmit to be called on onClick of the form. That means anything that you do on that form HTML element it will be a click event first.
It is a small change from onClick to onSubmit
...
<form className="container" autoComplete="off" onSubmit={handleSubmit}>
...
Also the part where you define radio inputs, you should use checked state of that input and set value to be the desired value for that radio input:
<label htmlFor="sex" className="label">
Sex:
</label>
<input
type="radio"
id="male"
name="sex"
value='male'
checked={newUser.sex === 'male'}
onChange={(e) => handleChange(e.target.value, "sex")}
/>
<label for="male">Male</label> {" "}
<input
type="radio"
id="female"
name="sex"
value='female'
checked={newUser.sex === 'female'}
onChange={(e) => handleChange(e.target.value, "sex")}
/>
<label for="female">Female</label>
You must use checked attribute to specify which item is checked. And use value property to set the value of any radio item. You also define onClick attribute on Form tag that it's incorrect and you must use onSubmit instead of that.
This is the correct link:
https://codesandbox.io/s/learning-react-forked-4wic3
change onClick to onSubmit. it'll work
index.html
<form action="http://127.0.0.1:5555/sign" method="POST">
<div class="main">
<div class="name-container">
<input type="text" class="input" placeholder="First Name" id="fname" />
<input type="text" class="input" placeholder="Last Name" id="lname" />
</div>
<div class="gender_date">
<div class="gen">
<input type="radio" name="gender" value="male" id="male" />
<label for="male">Male</label>
<input type="radio" name="gender" value="female" id="female" />
<label for="female">Female</label>
<input type="radio" name="gender" value="other" id="other" />
<label for="other">Other</label>
</div>
<div class="date-con">
<input type="date" class="input" id='date' name="bday" />
</div>
</div>
</div>
</form>
the post request works properly but the names aren't showing up.
back-end nodejs file => serve.js
const express = require("express");
const fs = require("fs");
const bodyParser = require("body-parser");
const app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.get("/sign", (req, res) => {
res.send("Hello");
});
app.post("/sign", urlencodedParser, (req, res) => {
res.send(req.body);
let student = req.body;
let students = [];
students.push(student);
let data = JSON.stringify(students, null, 2);
fs.writeFile("./file.json", data, err => console.log("success"));
console.log(students);
});
const PORT = 5555;
app.listen(PORT, err => {
console.log(`Server Running at port: ${PORT}`);
});
file.json: Output
[
{
"gender": "male",
"bday": "1999-07-05"
}
]
The value from "First Name" and "Last Name" fields aren't showing up in the final output.
No matter what I change I couldn't get it to work properly.
Thank you in advance.
Try it
<form action="http://127.0.0.1:5555/sign" method="POST">
<div class="main">
<div class="name-container">
<input type="text" class="input" placeholder="First Name" name="fname" />
<input type="text" class="input" placeholder="Last Name" name="lname" />
</div>
<div class="gender_date">
<div class="gen">
<input type="radio" name="gender" value="male" id="male" />
<label for="male">Male</label>
<input type="radio" name="gender" value="female" id="female" />
<label for="female">Female</label>
<input type="radio" name="gender" value="other" id="other" />
<label for="other">Other</label>
</div>
<div class="date-con">
<input type="date" class="input" id='date' name="bday" />
</div>
</div>
</div>
</form>
This is my form for user or admin. This formed is saved to database using shared service. After clicking button toaster service is not opening. At first click form will be submitted but form not reseted and toaster service will not open.After doule click only toaster opens and form submitted with empty.
<form method="post" class="minimal" #customerForm=ngForm (submit)="addCustomer()" >
<label for="username">
Username:
<input type="text" name="username" id="username" [(ngModel)]="user.username" placeholder="Enter Username" required="required">
</label>
<label for="password">
Password:
<input type="password" name="password" id="password" [(ngModel)]="user.password" placeholder="Enter Password" required="required" />
</label>
<label for="isAdmin">
Admin:
<select class="admin" type="text" name="admin" [(ngModel)]="user.isAdmin" required>
<option>User</option>
<option>Admin</option>
</select>
</label>
<label for="email">
Email:
<input type="email" name="email" id="email" [(ngModel)]="user.emailId" placeholder="Enter Email" required="required" />
</label>
<label for="phone">
Phone No:
<input type="number" name="phone" id="phone" [(ngModel)]="user.phoneNo" placeholder="Enter Phone" required="required" />
</label>
<label for="dob">
Date of Birth:
<input type="date" name="date" id="date" [(ngModel)]="user.dob" placeholder="Enter Date" required="required" />
</label>
<button type="submit" class="btn btn-minimal btn-submit" [disabled]="!customerForm.form.valid">Create</button>
<toaster-container></toaster-container>
This is my ts file
addCustomer() {
console.log("user",this.user);
this.persistanceService.addUser(this.user).subscribe((result: User) => {
this.cdRef.detectChanges();
console.log("admin",this.user.isAdmin)
this.toast();
this.user = new User();
});