Rohit Cha
Rohit Cha

Reputation: 49

TypeError: Cannot read property 'id' of undefined

I am trying to post data into nodejs server from a html file sent data below are my html file and nodejs file i am getting above type error please help

in script code i am sending data from html file to nodejs server

Index.html file

<html>
<head>
<title>contacts</title>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">  </script>


<script>
$(document).ready(function(){




     var id,name,phone,email;
   $("#save").click(function(){

       id=$("#id").val();
       name=$("#name").val();
       phone=$("#phone").val();
       email=$("#email").val();
        $.post("http://localhost:8083/save",{id:id,name:name,phone:phone,email:email},function(data){



        });
    });
});
</script>

</head>

<body>
  <div id="a">
  </div>
<form class="form-horizontal">
  <div class="form-group">
    <label for="exampleInputID" class="col-sm-2 control-label">ID</label>
    <input type="ID" class="form-control" id="id" placeholder="ID">
  </div>
  <div class="form-group">
    <label for="exampleInputName" class="col-sm-2 control-label">Name</label>
    <input type="Name" class="form-control" id="name" placeholder="Name">
  </div>
 <div class="form-group">
    <label for="exampleInputPhone" class="col-sm-2 control-label">Phone</label>
    <input type="Phone" class="form-control" id="phone" placeholder="Phone">
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1" class="col-sm-2 control-label">Email address</label>
    <input type="email" class="form-control" id="email" placeholder="Email">
  </div>




  <input id='save' type="button" value="save"  >
</form>

 <input type="button" id='get' value="Get" style="float: right;">
 </div>
</body>

</html>

Nodejs File


var express = require('express');
var app = express();
var http = require("http");
var mysql =  require('mysql');
var connection =  mysql.createConnection({
    host : 'localhost',
 user : 'root',
 password:''
});

connection.connect();

connection.query('use mysql');
var strQuery = 'select * from Person';
 app.use(express.static(__dirname + '/../Desktop'));


 app.post('/save',function(req,res){
  //res.setEncoding('utf8');
  var user_id=JSON.stringify(req.body.id);
  var user_name=JSON.stringify(req.body.name);

// this is my post request above



  console.log("ID = "+user_id+", name is "+user_name+", phone no. is "+user_phone+", email is "+user_email);

});

 app.listen(8083);
console.log('server running\n');

Upvotes: 1

Views: 7221

Answers (1)

Bhavin Solanki
Bhavin Solanki

Reputation: 4818

Here is your problem :

 <input type="ID" class="form-control" id="id" placeholder="ID">

There is not such type="ID" still available for input tag. for valid type of input tag, you ca refer http://www.w3schools.com/html/html_form_input_types.asp

There are some HTML5 added several new input types:

color
date
datetime
datetime-local
email
month
number
range
search
tel
time
url
week

Upvotes: 2

Related Questions