How to show an Alert via ajaxSubmit? - php

I'm trying to display an Alert message through ajaxSubmit as successful as in the example code below:
submitHandler: function( form ){
var dados = $( form ).serialize();
$.ajaxSubmit({
type: "POST",
url: "...",
data: dados,
complete: function( data )
{
// $("#cargo").resetForm();
alert("Alert Message");
$("#txtNome").focus();
$("#txtNome")
.hide()
.show("slow");
document.getElementById("cargo").reset();
}
});
return false;
}
But every time the form data is saved, but no Alert message is shown! Can anyone help me?

check this code. It is working.
jQuery('#formId').validate({
submitHandler: function(form) {
jQuery("#formId").ajaxSubmit({
success: function(data) {
alert("hello");
jQuery('#element').html(data);
},
data: {ajax:'true', tab:'true'}
});
}
});
Put your code in this format.

Related

ajax request does not stop page from refreshing

what is wrong with this ajax request? the page is still reloading without giving any popups. if i remove everything after the "event.preventDefault();" it will stop page reload so i figure it must something with how i'm using the ajax method. This is for a php-self validating form
<script type="text/javascript">
//attach submit event handler to the form
$('#rsgform1').submit(function(event) {
//prevent the form from submitting by default
event.preventDefault();
//Clear result div
$("#rsgresult").html('');
//get values from from
var values = $(this).serialize();
// do an ajax request
$.ajax({
url: "contact.php",
type: "post",
data: values,
success: function(){
alert("success");
$("#rsgresult").html('Submitted successfully');
},
error:function(){
alert("failure");
$("#rsgresult").html('There is error while submit');
}
});
</script>
You forgot to close the ajax call...
<script type="text/javascript">
//attach submit event handler to the form
$('#rsgform1').submit(function(event) {
//prevent the form from submitting by default
event.preventDefault();
//Clear result div
$("#rsgresult").html('');
//get values from from
var values = $(this).serialize();
// do an ajax request
$.ajax({
url: "contact.php",
type: "post",
data: values,
success: function(){
alert("success");
$("#rsgresult").html('Submitted successfully');
},
error:function(){
alert("failure");
$("#rsgresult").html('There is error while submit');
}
});
});
</script>
Try return false at the end of the callback function.
And don't forget to balance your braces as others have mentioned.
your script has a missing pair of closing brackets }) at the end
$('#rsgform1').submit(function (event) {
//prevent the form from submitting by default
event.preventDefault();
//Clear result div
$("#rsgresult").html('');
//get values from from
var values = $(this).serialize();
// do an ajax request
$.ajax({
url: "contact.php",
type: "post",
data: values,
success: function () {
alert("success");
$("#rsgresult").html('Submitted successfully');
},
error: function () {
alert("failure");
$("#rsgresult").html('There is error while submit');
}
});
});// <-- missing this closing pair
You have an error in your JS. When an error occurs, the page refreshes.
<script type="text/javascript">
$('#rsgform1').submit(function(event) {
event.preventDefault();
$("#rsgresult").html('');
var values = $(this).serialize();
$.ajax({
url: "contact.php",
type: "post",
data: values,
success: function() {
alert("success");
$("#rsgresult").html('Submitted successfully');
},
error:function() {
alert("failure");
$("#rsgresult").html('There is error while submit');
}
}); // Missing
});
</script>

refuse ajax submit if fields are empty jquery

Let's say I have a simple form. I use this jquery piece of code to get the result using ajax on my form page.
function sendquery()
{
$("#form").submit();
var url = "form.php";
$.ajax({
type: "POST",
url: url,
data: $("#form").serialize(),
success: function(data)
{
$("#output").html(data);
}
});
return false;
}
Quite simple. What I want now is to validate the form by adding a class, let's say "blank" to inputs with empty value, and don't allow the form to be submitted. It shouldn't be too hard, but whatever i've tried will just break my form.
How can I do it?
you need jQuery validate plugin , and ......
$(document).ready(function()
{
$('#formId').validate({
submitHandler:function(form)
{
$(form).ajaxSubmit({
success:function(response)
{
//do stuff on success
},
dataType:'json'
});
},
errorLabelContainer: "#error_message_box",
wrapper: "li",
rules:
{
name:"required",
category:"required"
},
messages:
{
name:"name required",
category:"category required"
}
});
});

AJAX loading message

I want to display a loading message while retrieving results via AJAX, but I can't. Can anybody help please?
<script type="text/javascript">
$(function() {
$(".search_button").click(function() {
// getting the value that user typed
var searchString = $("#search_box").val();
// forming the queryString
var data = 'search='+ searchString;
// if searchString is not empty
if(searchString) {
// ajax call
$.ajax({
type: "POST",
url: "do_search.php",
data: data,
beforeSend: function(html) { // this happens before actual call
$("#results").html('');
$("#search_result_box").show();
$("#searchresults").show();
$(".word").html(searchString);
},
success: function(html){ // this happens after we get results
$("#results").show();
$("#results").append(html);
}
});
}
return false;
});
});
</script>
I would change the message before firing the AJAX request. So, on click or on submit:
<script>
$('form').on('submit', function(e) {
e.preventDefault();
$('#response').html('<p>Loading…</p>');
$.post($(this).attr('action'), $(this).serialize(), function(response) {
// do something here with the response
$('#response').html('<p>Request successful.</p>');
});
});
</script>
Why not using beforeSend and success methods to show/hide a loading message
beforeSend: function(html) { // this happens before actual call
// DO SOMEHTING HERE TO SHOW YOUR LOADING MESSAGE AS $('#loading').show();
$("#results").html('');
$("#search_result_box").show();
$("#searchresults").show();
$(".word").html(searchString);
},
success: function(html){ // this happens after we get results
// DO SOMEHTING HERE TO HIDE YOUR LOADING MESSAGE AS $('#loading').hide();
$("#results").show();
$("#results").append(html);
}
rgds

How to write if else statement AJAX POST JQuery

Im trying to use if else statement in success ajax function, but the value always false and data inserted
I use jQuery v1.7.1, here my code
// Contact Submiting form
function Contact_form(form, options){
$.ajax({
url: 'contact.php',
data: $('#contactform').serialize(),
success: function(data){
if(data.check == '1'){ // if Ajax respone data.check =1 or Complete
$('.notifications').slideDown(function(){
setTimeout("$('.notifications').slideUp();",7000);
}); // Show notifications box
$('#contactform').get(0).reset(); // reset form
}
else if(data.check == '0'){
$('#preloader').fadeOut(400,function(){ $(this).remove(); });
alert("No complete");
return false;
}
},
cache: false,type: 'POST',dataType: 'json'
});
}
if else is bad in success function use
function Contact_form(form, options){
$.ajax({
url: 'contact.php',
data: $('#contactform').serialize(),
success: function(data){
$('.notifications').slideDown(function(){
setTimeout("$('.notifications').slideUp();",7000);
}); // Show notifications box
$('#contactform').get(0).reset(); // reset form
unloading(); // remove loading
},
error: function(data){
$('#preloader').fadeOut(400,function(){ $(this).remove(); });
alert("No complete");
return false;
},
cache: false,type: 'POST',dataType: 'json'
});
}
Can you try to use data.result in logic statement

Form submitted, response via ajax

Currently I have this, which works nicely - it's an email signup list which returns a successful response or error, as appropriate.
$.ajax({
type: "POST",
url: "mailing_list/mailing_list_add2.php",
data: dataString,
success: function(response) {
$('#emailform').html("<div id='display_block'></div>");
$('#display_block')
.hide()
.fadeIn(500, function() {
$('#display_block').html(response)
});
}
});
return false;
});
The form is in a div with ID "emailform" and the "display_block" is the response. What I need is for the response to automatically disappear after a short time and for the form to fade back in. I've tried a few things but nothing that has worked yet.
Any help on what to add to the above code would be appreciated.
Assuming your initial html is like,
<div id="emailform">
<form>
...
</form>
</div>
you can proceed like this,
.ajax({
type: "POST",
url: "mailing_list/mailing_list_add2.php",
data: dataString,
success: function(response) {
var backupHtml = $('#emailform').html();
$('#emailform').html("<div id='display_block'></div>");
$('#display_block')
.hide()
.html(response)
.fadeIn(500, function() {
$(this).fadeOut(5000,function(){
$('#emailform').html(backupHtml);
});
});
}
});
There is nothing inside display_block when you fade it in. Its just empty, I changed the code:
$.ajax({
type: "POST",
url: "mailing_list/mailing_list_add2.php",
data: dataString,
success: function(response) {
var backedup = $('#emailform').html(); // Take a snapshot of whats inside the emailform
$('#emailform').html("<div id='display_block'></div>");
$('#display_block')
.hide()
.html(response) // New line!
.fadeIn(500,function (){ // After we finsish the fadeIn
$('#emailform').hide().html(backedup).fadeIn(500); // Reset the old content and fade it in
});​
}
});
return false;
});
I created a JSFiddle for you http://jsfiddle.net/XHkWr/1/
To do instead of all mumbo jumbo.
$('#emailform').html("<div id='display_block'></div>");
$('#display_block').hide().html(response).stop().fadeIn(500);
I would say, that this would be a correct solution:
$.ajax({
url: 'mailing_list/mailing_list_add2.php',
type: 'post',
data: dataString,
success: function(data, textStatus, jqXHR) {
var $emailForm = $('#emailform').html();
$('#emailform').html('<div id="display_block"></div>');
$('#emailform').hide().html(data).fadeIn(500).delay(3000).fadeOut(500, function() {
$('#emailform').html($emailForm);
});
return false;
},
error: function(jqXHR, textStatus, errorThrown) {
var $emailForm = $('#emailform').html();
$('#emailform').html('<div id="display_block"></div>');
$('#display_block').hide().html(textStatus).fadeIn(500).delay(3000).fadeOut(500, function() {
$('#emailform').html($emailForm);
});
return false;
}
});
Result here: http://jsfiddle.net/p9URt/2/

Categories