Onclick Send To Ajax
I'm trying to complete some ajax requests to insert a textarea into a database without refresh. Here is my code: HTML:
Solution 1:
<textareaname='Status'></textarea><inputtype='button'value='Status Update'>
You have few problems with your code like using .
for concatenation
Try this -
$(function () {
$('input').on('click', function () {
varStatus = $(this).val();
$.ajax({
url: 'Ajax/StatusUpdate.php',
data: {
text: $("textarea[name=Status]").val(),
Status: Status
},
dataType : 'json'
});
});
});
Solution 2:
Tried and working. you are using,
<textareaname='Status'></textarea><inputtype='button'onclick='UpdateStatus()'value='Status Update'>
I am using javascript , (don't know about php), use id ="status" in textarea like
<textarea name='Status'id="status"> </textarea>
<input type='button' onclick='UpdateStatus()' value='Status Update'>
then make a call to servlet sending the status to backend for updating using whatever strutucre(like MVC in java or anyother) you like, like this in your UI in script tag
<srcipt>
functionUpdateStatus(){
//make an ajax call and get status value using the same 'id'var var1= document.getElementById("status").value;
$.ajax({
type:"GET",//or POSTurl:'http://localhost:7080/ajaxforjson/Testajax',
// (or whatever your url is)data:{data1:var1},
//can send multipledata like {data1:var1,data2:var2,data3:var3//can use dataType:'text/html' or 'json' if response type expected success:function(responsedata){
// process on dataalert("got response as "+"'"+responsedata+"'");
}
})
}
</script>
and jsp is like
the servlet will look like: //webservlet("/zcvdzv") is just for url annotation@WebServlet("/Testajax")publicclassTestajaxextendsHttpServlet {
privatestaticfinallongserialVersionUID=1L;
publicTestajax() {
super();
}
protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// TODO Auto-generated method stub
String data1=request.getParameter("data1");
//do processing on datas pass in other java class to add to DB// i am adding or concatenate
String data="i Got : "+"'"+data1+"' ";
System.out.println(" data1 : "+data1+"\n data "+data);
response.getWriter().write(data);
}
protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Post a Comment for "Onclick Send To Ajax"