screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; const App = () => { const [selectedRivers, setSelectedRivers] = useState([]); const rivers = [ { id: 1, name: "Ganga" }, { id: 2, name: "Yamuna" }, { id: 3, name: "Brahmaputra" }, { id: 4, name: "Godavari" }, { id: 5, name: "Krishna" }, { id: 6, name: "Narmada" }, { id: 7, name: "Tapti" }, { id: 8, name: "Mahanadi" }, // Add more rivers as needed ]; const handleCheckboxChange = (event) => { const { value, checked } = event.target; if (checked) { setSelectedRivers([...selectedRivers, value]); } else { setSelectedRivers( selectedRivers.filter((river) => river !== value) ); } }; const handleSubmit = (event) => { event.preventDefault(); // Perform your post request with the selectedRivers data axios .post("/your-api-endpoint", { selectedRivers }) .then((response) => { console.log(response.data); }) .catch((error) => { console.error(error); }); }; return ( <div className="container"> <h3>React Js post checkbox data to API </h3> <form onSubmit={handleSubmit}> {rivers.map((river) => ( <div key={river.id}> <label> <input type="checkbox" value={river.name} checked={selectedRivers.includes(river.name)} onChange={handleCheckboxChange} /> {river.name} </label> </div> ))} <button type="submit">Submit</button> </form> </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { max-width: 400px; margin: 0 auto; padding: 20px; background-color: #f2f2f2; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .container h3 { font-size: 20px; margin-bottom: 16px; } .container form { display: flex; flex-direction: column; } .container label { display: flex; align-items: center; margin-bottom: 8px; } .container input[type="checkbox"] { margin-right: 8px; } .container button { padding: 8px 16px; font-size: 16px; background-color: #4caf50; color: #fff; border: none; border-radius: 4px; cursor: pointer; } .container button:hover { background-color: #45a049; } .container button:disabled { background-color: #cccccc; cursor: not-allowed; } </style> </body> </html>