2017-04-08 39 views
0

我创建了两个文件:index.html和main.js.我如何从index.html文件获取数据到main.js文件并插入数据库?如何从HTML表单获取数据到JavaScript文件并添加MySQL?

的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> 

我想创建whick要求发送index.html文件的功能,并从该文件中获取数据,然后添加到MySQL数据库。

main.js

// 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/'); 
    }); 
+0

action属性应设置为将保存数据的文件的url。还要将方法属性设置为发布。你必须保存数据的代码?使用mysqli函数mysql折旧。你需要看一些例子并做更多的研究。 –

回答

0

嗯,你可以使用这个JavaScript函数

的document.getElementById( '名称')值

这可能与IF语句中使用。

if(document.getElementById('age')。value == 100){

}

我还不了解MySQL。

相关问题