moh.mehrabi moh.mehrabi - 27 days ago 10
Ajax Question

how add some textbox values in session in jsp page after click a button except form submit?

i have 5 textboxes and a buttom in my page.i want add these textboxes values in my session without form submitting.whats the problem in my code?

<head>
<meta charset="UTF-8">
<title>$Title$</title>
<script src="js/jquery-1.10.2.js"></script>
<script>
$('#button').click(function() {
var val1 = $('#text1').val();
var val2 = $('#text2').val();
$.ajax({
type: 'POST',
url: 'add.jsp',
data: { text1: val1, text2: val2 },
success: function(response) {
$('#result').html(response);
}
});
});
</script>
</head>
<body>
<form action="register.do" method="post">
<br>
<label>name</label>
<input type="text" name="txt1" id="text1">
<br>
<label>family</label>
<input type="text" name="family" id="text2">
<br>
<label>article name</label>
<input type="text" name="uni">
<br>
<label>article place</label>
<input type="text" name="uniname">
<br>
<label>article grade</label>
<input type="text" name="unigrade">
<br>
<label>article date</label>
<input type="text" name="unitype">
<input type="button" name="button" id="button">
<input type="submit">
</form>
</body>


i dont recive any parameter in my add.jsp page to store in session.

Answer

$('#button').click is prematurely registered, not even the DOM elements are completely loaded, that's why it's not triggered. Move your code inside as follows:

$(function(){ //  hold till DOM becomes safe to manipulate.
    Put your code here.  
});
Comments