Indicating success with an Ajax script from a form
I have a form that manipulates the size of an image that is posted via this ajax script. The script and my controller work fine. My question is, how do I append a success message to this jquery script? If I used the regular $.ajax() script it would be easy.
I obtained this script through using form torch for codeigniter and would like to learn how to indicate success with it. The "function(data)" appears to me to be there to indicate failure although I dont understand how that is triggered. It doesnt work anyways if the form is submitted empty.
$(document).ready(function() {
$("#form").submit(function() {
var image = $("#image").val();
$.post("/post/process", { image:image },
function(data){
$("#image_error").html(data.image);
},'json');
});
});//this comes from form torch
The controller is the standard stuff for image manipulation The form
<?=form_open("/post/process", 'onsubmit="return false;" id="form"')?>
<p>
<la开发者_JAVA百科bel for="image">Image</label>
<span id="image_error" class="error"></span>
<input type="text" name="image" id="image" />
I would like to add something like this at the bottom of the form
<div id="success"></div>
Thanks for reading
Personally, I prefer to you the $.ajax
function because it gives you just one function you can use for all AJAX calls. In addition, it allows you to provide two functions: one to be executed if the request succeeds and the other to be executed if the request fails.
According to the documentation (http://api.jquery.com/jQuery.post/), the $.post
method only calls the function you specify if the operation succeeds.
Also, if you need to make the script wait for an indication of success or failure before the user does anything else, consider using the $.ajax
method with the async:false
parameter.
There are several ways to accomplish what you described: one way would be to write something like this for the function you pass to $.post
:
function(data)
{
$(document).append('<div id="succeed"></div>');
}
If i'm not mistaken, you can do something similar to this:
response = $.post(...);
And response can hold success and error messages. Than, u can check it using if-condition and write to your div:
$("#success").html('your html')
精彩评论