• Techblog369, India
  • September 29, 2022

How to create PHP QR code and Save it to Database?

Hi In this Article I am going to show how to create QR code and save it to database also fetch QR Code in a Table Create a Page addqrcode.php in …

How to Send Multiple Files Attachment Mail using Laravel

Hi in this article i am going to show how to send multiple files attachment mail in Laravel. It’s simple example of Laravel send multiple attachment in mail. I explained simply …

Addservice.js


import { useState } from 'react'
import Swal from 'sweetalert2';
function Addservice() {
   const [ServiceName, setName] = useState("");
   const [ServiceDesc, setDesc] = useState("");
   const [file, setFile] = useState("");
   async function Addservice() {
       // const token = localStorage.getItem("token")
       //const token = JSON.parse(localStorage.getItem('token'));
       const user = JSON.parse(localStorage.getItem('token'));
      
       const formData = new FormData();
       formData.append('ServiceName', ServiceName);
       formData.append('ServiceDesc', ServiceDesc);
       formData.append('file', file);


      await fetch(
           'http://127.0.0.1:8000/api/addservice',
           {
               method: 'POST',
               body: formData,
               headers:{
                   // 'Authorization': "Bearer " + "139|qMtIApnGoPFxPGrEXGzKSEWTbbXrHXCAU5uuTar3"
                   'Authorization': 'Bearer ' + user.token,
               },
                                     
               }
           
       )
           .then((response) => response.json())
           .then((result) => {
               console.log('Success:', result);
               Swal.fire({
                   // icon: 'success',
                   title: 'Data Saved Successfully'
               })
           })
           .catch((error) => {
               console.error('Error:', error);
               Swal.fire({
                   // icon: 'error',
                   title: 'All Field required',
                   text: error.text,
               })
           });

   }

   return (
       <div>


           <div className="col-sm-6 offset-sm-3 my-4">
               <h1 className="text-center">AddService</h1>
               <input type="text" onChange={event => setName(event.target.value)} placeholder="
                   Service Name" className="form-control" required="" />
               <br />
               <input type="text" onChange={event => setDesc(event.target.value)} placeholder="Service Description" className="form-control" />
               <br />
               <input type="file" onChange={event => setFile(event.target.files[0])} className="form-control" />
               <br />
               <button onClick={Addservice} className="btn btn-primary">Save</button>
           </div>

       </div>


   )
}
export default Addservice

const user = JSON.parse(localStorage.getItem(‘token’));

We already set token, here we only get the token and set it into header

JSON.parse()

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

headers:{
                   // 'Authorization': "Bearer " + "139|qMtIApnGoPFxPGrEXGzKSEWTbbXrHXCAU5uuTar3"
                   'Authorization': 'Bearer ' + user.token,
               },

Thanks for reading…

Author

admin@techblog369.in

Leave a Reply

Your email address will not be published.