midhun k midhun k - 6 months ago 11
jQuery Question

post response is not getting?

input.js => when submit button clicked values are passed to insert .

$(document).ready(function()
{
$('#submit').click(function()
{
var name=$("#name").val();
var address=$("#address").val();
var gender=$("#sex:checked").val();
var dob= $("#dob").val();
var course=$("#course").val();
var email= $("#emailid").val();
var mobileno=$("#mobileno").val();
var data={'name':name,'address':address,'gender':gender,'dob':dob,'course':course,'email':email,'mobileno':mobileno};
$.post('http://localhost:3111/insert',data,function(result){
if(result=='success')
{
alert("insertion successful");
}
});
});
});


routes.js => the values are successfully passed here

var express = require('express');
var router = express.Router();
var db=require('./dbentry.js');

router.post('/insert',function(req,res)
{
var name=req.body.name;
var address=req.body.address;
var gender=req.body.gender;
var dob=req.body.dob;
var course=req.body.course;
var email=req.body.email;
var mobileno=req.body.mobileno;
var insertValues={'name':name,'address':address,'gender':gender,'dob':dob,'course':course,'email':email,'mobileno':mobileno};
db.insert(insertValues,function(result){
if(result=="success")
{
res.end("success");
}
else
res.end("fail");
});
});
module.exports = router;


dbentry.js => function for inserting values

var client=require('./dbconnection.js');
function insert(insertValues,callback)
{
client.connection.query('insert into detail set ?',insertValues,function(err,rows)
{
if(!err)
callback("success");
else
callback("fail");
});
}
}
module.exports.insert=insert;


The values are successfully inserted in database and a success message is passed from
dbentry.js
to
route.js
.
but the problem is from the
route.js
the success message is not passed to the
input.js
.I inspected the code for a long time but I was unable to find the error.

Answer

Just check your HTML file, if you added "form" tag without onsubmit and check,submit is happened by $('#btnId').click(function(){});

if it is the case, then remove the unused "form" tag, and check your code is working or not.

i think its happened because of default behavior of form. here the form onSubmit might be empty.