-
Notifications
You must be signed in to change notification settings - Fork 40
/
Select Country and State using ReactJs
82 lines (66 loc) · 2.41 KB
/
Select Country and State using ReactJs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useState, useEffect } from "react";
import { Container } from "react-bootstrap";
function Country() {
const [country, setCountry]= useState([]);
const [countryid, setCountryid]= useState('');
const [stetes, setSat]= useState([]);
useEffect( ()=>{
const getcountry= async ()=>{
const req= await fetch("http://localhost/devopsdeveloper/country");
const getres= await req.json();
console.log(getres);
setCountry(await getres);
}
getcountry();
},[]);
const handlecountry=(event)=>{
const getcoutryid= event.target.value;
setCountryid(getcoutryid);
event.preventDefault();
}
useEffect( ()=>{
const getstate= async ()=>{
const resstate= await fetch(`http://localhost/devopsdeveloper/state/getstate/${countryid }`);
const getst= resstate.json();
setSat(await getst);
}
getstate();
},[countryid]);
return (
<Container className="content">
<div className="row">
<div className="col-sm-12">
<h5 className="mt-4 mb-4 fw-bold">Output { }</h5>
<div className="row mb-3">
<div className="form-group col-md-4">
<label className="mb-2">Country</label>
<select name="country" className="form-control" onChange={(e)=>handlecountry(e)}>
<option>--Select Country--</option>
{
country.map( (getcon)=>(
<option key={getcon.country_id} value={getcon.country_id }> { getcon.country_name}</option>
))
}
</select>
</div>
<div className="form-group col-md-4">
<label className="mb-2">State</label>
<select name="state" className="form-control">
<option>--Select State--</option>
{
stetes.map( (st,index)=>(
<option key={index} value={st.state_id}>{ st.state_name}</option>
))
}
</select>
</div>
<div className="form-group col-md-2 mt-4">
<button className="btn btn-success mt-2" >Submit</button>
</div>
</div>
</div>
</div>
</Container>
);
}
export default Country;