Saturday 8 April 2017

Tell me how can get data from html form to js file and to add mysql

 I have created two files index.html and main.js.Tell how I can get data from index.html file to main.js file and insert the in database.


     index.html

    // html form
        <form method="POST" action = "">

        <input type="text"  id="name" name ="name" placeholder="First Name">
        <input type="number"  id="age"  name="age" placeholder="Age">
                <select  id="gender"  name="gender">
            <option value = "male" >Male</option>
            <option value = "female">Female</option>
          </select>
                <input type="text"  id="phone"  name="phone" placeholder="Phone Number">
                <button type="submit" class="btn">Submit</button>

          </form>

        main.js //js file

I want to create a function whick send a index.html file on request and take data from that file and add to mysqldatabase. // creating connection to mysql databases var con = mysql.createConnection({ host:'localhost', user:'root', password:'1234', database:'project1' }); // checking connection con.connect(function(err){ if(err){ console.log('error connecting to database'); }else{ console.log('connected to databases'); } }); var publicPath = path.resolve(__dirname, '/');

       app.get('/', function(req, res) {
          res.sendFile(path.join('index.html',{root:publicPath}));
        });
        app.listen(5000, function() {
          console.log('Server running at http://127.0.0.1:5000/');
        });



via Suyog Pipliwal

No comments:

Post a Comment