Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Ajax form submit ?

Featured Replies

Hi guys,

 

I have the following script which basically is "attached" to a contact form, however when i submit the form it refreshes the page. How can i make that it submits the form via ajax showing validation without refreshing the page ?

 

Thank you

 

jQuery(document).ready(function($){	

// contact						
var error = true;	

function addLoading( e )
{
$(e).val( '{wait}'.replace('{wait}', contactForm.wait) ).attr('disabled', true);
}

function removeLoading( e )
{
$(e).val(value_submit).attr('disabled', false);
}

function addError(msg, e, effect)
{
error = true;		
$(e).removeClass('icon success');
$(e).addClass('icon error');
$(e).parents('li').find('.msg-error').text(msg);
if( effect !== undefined && effect == true )
{
$(e).css({position:'relative'}).animate({left:-10}, 100).animate({left:10}, 100).animate({left:-5}, 100).animate({left:5}, 100).animate({left:0}, 100);
}
}				

function addSuccess(e)
{									
$(e).addClass('icon success');
}

function removeError(e)
{
error = false;
$(e).parents('li').find('.msg-error').text('');	
$(e).removeClass('icon error');
 $(e).removeClass( 'formRed')
addSuccess(e);
}		

$('.contact-form .required').blur(function(){			
var name = $(this).attr('name').match( /(.*)\[(.*)\]/ );
var id_form = $(this).parents('.contact-form').find('input[name="id_form"]').val();

jQuery.globalEval( 'var msg = messages_form_'+id_form+'.'+name[2] );

if( $(this).val() == '' )
addError( msg, this );	
else			
removeError(this);
});			

$('.contact-form .email-validate').blur(function(){			
var expr = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
var name = $(this).attr('name').match( /(.*)\[(.*)\]/ );	
var id_form = $(this).parents('.contact-form').find('input[name="id_form"]').val();

jQuery.globalEval( 'var msg = messages_form_'+id_form+'.'+name[2] );

if( ( $(this).val() != '' && !expr.test( $(this).val() ) ) || ( $(this).is('.required') && $(this).val() == '' ) )
addError( msg, this );		
else
removeError(this);
});

$('.contact-form').submit(function(){
addLoading( '.contact-form input:submit' );
});

$('input[placeholder], textarea[placeholder]').placeholder();

});

Change:

 

$('.contact-form').submit(function(){
addLoading( '.contact-form input:submit' );
});

 

To:

 

$('.contact-form').submit(function(e){
e.preventDefault();
addLoading( '.contact-form input:submit' );
});

  • Author

Hi, thanks for your reply i tried your suggestion but unfortunately it doesn't work, the submit buttons changes to "wait..." but nothing happens when i leave fields empty or even when i fill them

 

Here's the code from the source of the page

 

form:

 

<form id="contact-form-contact-form" class="contact-form row" method="post" action="" enctype="multipart/form-data">
<div class="usermessagea"></div>
<fieldset>
 <ul>
  <li class="text-field 3">
   <label for="name-contact-form">
 <span class="mainlabel">Name</span>
   </label>
   <div class="input-prepend"><input type="text" name="bt_contact[name]" id="name-contact-form" class="required" value="" /></div>
   <div class="msg-error"></div><div class="clear"></div>
  </li>
  <li class="text-field 3">
   <label for="email-contact-form">
 <span class="mainlabel">Email</span>
   </label>
   <div class="input-prepend"><input type="text" name="bt_contact[email]" id="email-contact-form" class="required email-validate" value="" /></div>
   <div class="msg-error"></div><div class="clear"></div>
  </li>
  <li class="text-field 3">
   <label for="phone-contact-form">
 <span class="mainlabel">Phone</span>
   </label>
   <div class="input-prepend"><input type="text" name="bt_contact[phone]" id="phone-contact-form" class="" value="" /></div>
   <div class="msg-error"></div><div class="clear"></div>
  </li>
  <li class="textarea-field 9">
   <label for="message-contact-form">
 <span class="mainlabel">Message</span>
   </label>
   <div class="input-prepend"><textarea name="bt_contact[message]" id="message-contact-form" rows="8" cols="30" class="required"></textarea></div>
   <div class="msg-error"></div><div class="clear"></div>
  </li>
  <li class="submit-button span9">
   <input type="text" name="bt_bot" id="bt_bot" />
   <input type="hidden" name="bt_action" value="sendemail" id="bt_action" />
   <input type="hidden" name="bt_referer" value="http://localhost/my-website/?page_id=2" />
   <input type="hidden" name="id_form" value="11" />
   <input type="submit" name="bt_sendemail" value="Send Message" class="sendmail alignleft" />    <div class="clear"></div>   </li>
 </ul>
</fieldset>
</form>

 

Other codes associated to the form

 

<script type="text/javascript">
var messages_form_11 = {
 name: "Insert the name",
 email: "Insert a valid email",
 phone: "",
 message: "Insert a message"
};
</script>

 

<script type='text/javascript'>
/* <![CDATA[ */
var contactForm = {"wait":"wait..."};
/* ]]> */
</script>

 

The page also loads the jquery.placeholder plugin, hope this helps? Any idea why it doesn't work ?

 

Thank you

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.