-
Notifications
You must be signed in to change notification settings - Fork 40
/
Select Country and State with disable state drop Down in React Js
86 lines (83 loc) · 3.16 KB
/
Select Country and State with disable state drop Down in React Js
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
83
84
85
86
import React, { useState, useEffect } from "react";
import { Container } from "react-bootstrap";
import axios from "axios";
function Countryapi() {
const [countrydata, setCountrydata]= useState([]);
const [countryid, setCountryid]= useState('');
const [statedata, setStatedata]= useState([]);
const [stateid, setStateid]= useState('');
const [enable, setEnable]= useState(true);
useEffect( ()=>{
const getCountry = async()=>{
const reqdata= await fetch("http://localhost/devopsdeveloper/Countryapi/countrydata");
const resdata= await reqdata.json();
//console.log(resdata);
setCountrydata(resdata);
}
getCountry();
},[]);
const handlecountry =async(e)=>{
const countryid= e.target.value;
if(countryid!=='')
{
const reqstatedata= await fetch("http://localhost/devopsdeveloper/Countryapi/statedata/"+countryid);
const resstatedata= await reqstatedata.json();
setStatedata(await resstatedata);
setCountryid(countryid);
setEnable(false);
} else {
setStatedata([]);
setEnable(true);
}
//console.log(countryid);
}
const handlestate= (e)=>{
const getstateid= e.target.value;
setStateid(getstateid);
}
const handleSubmit =(e)=>{
e.preventDefault();
const counrtyState={ countryid:countryid, stateid:stateid};
axios.post("http://localhost/devopsdeveloper/Countryapi/countrystatedata", counrtyState)
.then(result=>{console.log(result)});
}
return (
<Container className="content">
<div className="row">
<div className="col-sm-12">
<h2 className="mt-4 mb-4 fw-bold">Select Country and State using ReactJs</h2>
<form onSubmit={ handleSubmit}>
<div className="row mb-3">
<div className="form-group col-md-4">
<label className="mb-2">Country</label>
<select name="country" className="form-control p-2" onChange={(e)=>handlecountry(e)}>
<option value="">--Select Country--</option>
{
countrydata.map( (getcountry, index)=>(
<option key={index} value={getcountry.country_id}> { getcountry.country_name } </option>
))
}
</select>
</div>
<div className="form-group col-md-4">
<label className="mb-2">State</label>
<select name="state" className="form-control p-2" disabled={enable} onChange={(e)=>handlestate(e)}>
<option>--Select State--</option>
{
statedata.map( (getstate, index)=>(
<option key={index} value={ getstate.state_id}> { getstate.state_name} </option>
))
}
</select>
</div>
<div className="form-group col-md-2 mt-4">
<button name="submit" className="btn btn-success btn-lg p-1 mt-2" >Submit</button>
</div>
</div>
</form>
</div>
</div>
</Container>
);
}
export default Countryapi;