It is because Ajax is asynchronous, the success
or the error
function will be called later, when the server answer the client. So, just move parts depending on the result into your success function like that :
jQuery.ajax({ type:"post", dataType:"json", url: myAjax.ajaxurl, data: {action: 'submit_data', info: info}, success: function(data) { successmessage = 'Data was succesfully captured'; $("label#successmessage").text(successmessage); }, error: function(data) { successmessage = 'Error'; $("label#successmessage").text(successmessage); }, }); $(":input").val(''); return false;
Related Posts:
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- AJAX not Working with php as supposed
- Cross-Origin Read Blocking (CORB)
- syntax error: unexpected token <
- Infinite Scrolling in Asp.Net MVC with jQuery / AJAX Issues
- ajax jquery simple get request
- jQuery get textarea text
- jQuery AJAX cross domain
- Set timeout for ajax (jQuery)
- How to generate a simple popup using jQuery
- What does on() in JavaScript do?
- jQuery AJAX submit form
- jQuery Ajax File Upload
- How to use php code in javascript
- How can I upload files asynchronously with jQuery?
- Adding images to a Bootstrap Carousel with AJAX
- AJAX response error: net::ERR_EMPTY_RESPONSE
- Access Control Request Headers, is added to header in AJAX request with jQuery
- Execute PHP function with onclick
- jQuery Ajax POST example with PHP
- Refresh/reload the content in Div using jquery/ajax
- How to manage a redirect request after a jQuery Ajax call
- is there something like isset of php in javascript/jQuery?
- How do I refresh a DIV content?
- Only on Firefox “Loading failed for the script with source”
- FormData append not working
- Uncaught TypeError: Cannot read property ‘toLowerCase’ of undefined
- Download a file by jQuery.Ajax
- AngularJS POST Fails: Response for preflight has invalid HTTP status code 404
- Ajax Load More Posts in Category Page
- WordPress Ajax always returns a 404 error
- Click loads template via ajax
- Display notification only once
- Access/update database with jQuery
- How to display something in a div when the user clicks on a text in another div – no page refresh [closed]
- How to speed up admin-ajax.php in wordpress
- Colorbox ajax loading of outside HTML content works perfect on localhost but not on server
- Sending jsPDF documents to the server
- Ajax return code 400
- How to test nonce with AJAX – Plugin development
- class click counter save number
- AJAX request status 200 but no actual “response”
- Save Search System
- dynamic dependent select dropdown
- How to add a do_action on refreshing of WP customizer?
- Setting value of session with Ajax not working
- How to add a Custom Mailchimp AJAX Newsletter Subscribe Form
- Fancybox type popup window that’s not an iframe
- How do I Import / Upload Files with jQuery AJAX?
- WordPress Ajax Call inserting data but success response false
- wordpress add_action() issue in ajax call
- Why i can’t get custom fields value or post ID via Ajax?
- Accessing an API with jQuery and AJAX
- Change Query Arguments (filter) with jQuery/Ajax or PHP?
- WordPress cascading dropdown selection search based on Parent Page & Child Page
- Tracking Visitor LatLng with WordPress using JS, PHP. How to put data which was extract using JS into DB
- Admin-ajax.php 404 error
- How to pass values from one function to the other using an array variable
- How to use wp_ajax_set_post_thumbnail?
- Update $wpdb query with AJAX
- AJAX to add to cart multiple products woocommerce
- Cannot successfully execute AJAX script to call function.php specific function. Using XAMPP localhost to test
- How to echo a PHP code into WooCommerce variation product?
- How can i get the same ajax result using WP REST API instead of admin-ajax?
- How to call a function from functions.php with ajax?
- number of posts with “Load More”
- Find the method which AJAX GET calls
- Open image gallery on link click
- Why I’m Not Having Access to “$_POST” Data Outside My AJAX Callback?
- 500 (Internal Server Error) of external php file
- Dynamic Gallery
- Pass a PHP variable (loop-audio.php) to jQuery function (js/script.js)
- notify users when changed database
- Uncaught SyntaxError: Invalid or unexpected token
- Uncaught TypeError: Cannot read property ‘msie’ of undefined – jQuery tools
- What does window.jQuery and window.$ mean?
- SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
- JQuery get the title of a button
- How to open a URL using PHP?
- Trying to load jquery into tampermonkey script
- How can I exit from a javascript function? [duplicate]
- Object doesn’t support property or method ‘addEventListener’
- How to fix javascript .toFixed is not a Function error
- Combine CSS and JS files from plugins
- Security – Ajax and Nonce use [closed]
- Nice scroll to wordpress
- Adding wp_ajax to a theme outside functions.php (on specific template page)
- How to prevent those PHP variables from being cached on WordPress?
- Use ajax from function.php
- Ajax loaded form replaces form action with Ajax url
- prevent default not stopping page refresh. Passing form information to and from php with ajax in a wordpress site
- [jQuery]Adding shortcode
- Display parent and child taxonomies in separate drop down select fields
- Output votes to div dynamically in wordpress
- Is there some jQuery conflict here?
- Load more posts using AJAX based on posts inside WP_Query
- Using wp_editor() when not logged stopped working
- Multiple Notifications SetInterval
- Use one javascript variable into another javascript file
- Fatal error: Uncaught Error: Call to undefined function wp_nav_menu()