2017-04-08 32 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。

相關問題