I am using a Form in a LightBox which contains some input element.
<form name="imageUploadForm" action="uploadImage.do" method="post" enctype="multipart/form-data">
<input type="text" id="id" name="id" style="display: none;" value="">
<div id="fileUploaderDiv">
<input type='file' name="file0" id ="file0" />
</div>
<input type="submit" value="Submit">
</form>
when i am submitting the form than the form redirect to it's action location. I just want to submit form without redirecting user, so user stay on lightbox without loosing his data.
I have tried jquery ajax call for this
var data = new FormData();
var $inputs = $('#imageUploadForm :input');
var values = {};
$inputs.each(function() {
values[this.name] = $(this).val();
data.append(this.name, $(this).val());
});
$.ajax({
url: 'uploadImage.do',
data: data,
cache: false,
contentType: 'multipart/form-data',
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
But getting error at server side in my FileUploader servlet.
The request was rejected because no multipart boundary was found
can anybody tell me what am i missing in this ?
You need to prevent the default action of submitting the form:
$('form[name="imageUploadForm"]').on('submit', function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'uploadImage.do',
data: data,
cache: false,
contentType: false,
processData: false,
success: function(data){
alert(data);
}
});
});
I believe you should set the contentType option to false when using the FormData class, forcing jQuery not to add a Content-Type header, otherwise the boundary string will be missing, and that's probably the reason for your server error.
You must also leave the processData flag set to false, otherwise, jQuery will try to convert your FormData into a string, which will fail.
Here is the simplest form of a form submit using jquery Ajax. I have not tested this but it should work :-)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test form</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#sbmt').click(function(){
var text1 = $('#text1').val();
var text2 = $('#text2').val();
/// validation of inputs
///then
$.ajax(
{
url :'submit.php',
type : 'post',
data : {'text1' : text1, 'text2' : text2 },
success: function(data){
alert("form submitted . response is :"+ data);
}
}
).fail(function(){alert("Failed!!");});
});
});
</script>
</head>
<body>
<form id="myform">
<input type="text" id="text1" />
<input type="text" id="text2" />
<input type="button" id="sbmt" value="submit"/>
</form>
</body>
</html>
Related
I am new to AJAX Request, and my project need help.
JQuery / AJAX part :
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'POST',
url: 'ajax_save.php',]
success: function(msg) {
alert(msg);
}
error: function(msg) {
alert('error');
}
});
});
});
</script>
html part :
<input type="text" id="txt" name="text" />
<span id="txt">Result Maybe Here</span>
<button>Get External Content</button>
ajax_save.php
echo base64_encode( $_GET["text"] );
This don't really perform well, and just silent with out any trace.
Example of performing well :
<input type="text" id="txt" name="text" /> >>> agnes (user type)
<span id="txt">Result Maybe Here</span> >>> YWduZXM= (result)
<button>Get External Content</button>
About Ajax
To perform AJAX request on browser(client side), you need JavaScript. Maybe you can take a look at jQuery.ajax. $.get('process.php?text=something');
To perform AJAX request in PHP(server side), use file_get_contents('process.php?text=something');
To make your code work, you should perform a GET request instead of a POST one:
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'GET',
url: 'ajax_save.php?text=' + $('#text').val(),
success: function(msg) {
$('#span').text(msg)
}
error: function(msg) {
alert('error');
}
});
});
});
</script>
And every id in your HTML element should be unique, so they shouldn't both be id="txt".
About Base64
BTW, if you're just going to perform Base64 encoding, why not do it in JavaScript?
$(function(){
$('#text').on('input', function(){
$('#span').text(btoa($(this).val()));
});
});
<input type="text" id="text" name="text" />
<span id="span">Here the result!</span>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I want Open/Save file dialog appears on page with notification about execution status.
JSP:
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" language="javascript">
function call() {
var msg = $('form').serialize();
$.ajax({
type: 'POST',
url: 'compressor',
data: msg,
success: function(data) {
$('#results').html("Success");
},
error: function(xhr, str){
$('#results').html("Failed");
}
});
}
</script>
</head>
<body>
<h2>Web compressor</h2>
<form method="post" action="javascript:void(null);" onsubmit="call()">
<table>
/*input*/
</table>
</form>
Result: <div id="results"></div>
</body>
Servlet:
On input servlet gets, source file path and creates temporary file. After that i use:
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment; filename="fileName");
And write file to OutputStream. But dialog pop-up doesn't appear, only "Success" message. Without ajax it works correctly. How can i solve this problem?
I am trying to create a comment section using jsp, servlet and ajax. The problem I am facing is that each comment replaces it's previous one instead of showing next to it.
Highly appreciate any kind of help.
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#submitBtn').click(function() {
var cmt = $('#cmt').val();
$.ajax({
type : 'POST',
data : {
cmt : cmt,
action : 'EnterMsg'
},
url : 'SubmitComment',
success : function(result) {
$('#view2').text(result);
}
});
});
});
</script>
</head>
<body>
<fieldset>
<legend>Enter Message</legend>
<form>
Ques.<input type="text" id="cmt"> <input type="button"
value="Send" id="submitBtn"><br> <span id="post1"></span>
</form>
</fieldset>
<fieldset>
<legend>View Message</legend>
<form>
<div id='view2'></div>
<br>
</form>
</fieldset>
Try
var html='';
$.ajax({
dataType: "json",
url: "SubmitComment",
error: function () {
alert('error occured');
},
success: function (result) {
for(var key in result) {
var value = result[key];
html+='<div>'+key+':'+value+'</div>'
}
$("#view2").append(html);
}
});
Instead of
success : function(result) {
$('#view2').text(result);
}
Because of you get multiple comments from the ajax respose and you have to iterate each one of them and append to your div tag
is this wrong because i get loadData as undefined?
<head>
<script type="text/javascript">
function loadData() {
var name = $("#surname").val();
$.ajax({
url: 'checkdata.php',
type: 'POST',
data: { 'surname' : surname }
});
alert(data);
//return false;
</script>
</head>
<body onload="loadData()">
<form name="form1" method="post" action="php/save-edit.php" >
i have a form where i want to extract data from a db and display it in the form fields on form load
As per my understand you are using code code.Please try below code.
Just change <body onload="loadData()"> to <body onload="javascript:loadData();"> and try. it may help.
<head>
<script type="text/javascript">
function loadData() {
var name = $("#surname").val();
$.ajax({
url: 'checkdata.php',
type: 'POST',
data: { 'surname' : name }
});
alert(data);
//return false;
}
</script>
</head>
<body onload="javascript:loadData();">
<form name="form1" method="post" action="php/save-edit.php" >
This problem might be happened because of running the onload event before creating the HTML object.
It would be better if you use like as follow :
$( document ).ready(function() { your function });
Hope this help.
i am trying to develop a username validation with jsp and ajax but an error apperar like this
POST ://localhost:8080/web_application/deneme.jsp
Show error : 405 (Method Not Allowed)
this is my jsp page what is wrong here
<html>
<body>
<form name="form" id="form">
<input type="text" name="username" id="username">
<br>
<input
type="submit" name="register" value="Sign up">
</form>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script
src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#form").validate({
rules : {
username : {
required:true,
remote : {
url : "deneme.jsp",
async : false,
type : "post",
data : {
username : function() {
return $("#username").val()
}
}
}
}
},
messages : {
username : {
required:"Require Field" ,remote:"already exist"
}
}
});
});
</script>
</body>
</html>
Instead of:
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>
http is missing in your javascript file link location.
use the following:
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>