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
- I got this error POST https://localhost/meraboilerwp/[object%20Object] 404 (Not Found)
- How do I ensure that the URL parameters are updated correctly when multiple filters are applied
- Getting a variable inside foreach from PHP to JS after localization
- Custom AJAX form not working async
- JS file is not loading when i put wp_head() and wp_footer
- Live search from database table
- Add a counter for mouseovers (custom field)
- AJAX Success Function using JS substr() fails in WP 4.1 vs 4.0
- Ajaxurl suddenly not defined
- Execute Jquery when a specific page in my plugin is loading
- Replacing WordPress Icons (menu,icons32, etc)?
- Covert WordPress Blogname into JQuery
- Is there some jQuery conflict here?
- Ajax URLs without #!, how to prevent falling into single.php on load or reload?
- Using jQuery .after inside loop
- what does $options = get_option(‘test_theme’); do?
- Ajax page load in wordpress the right way?
- How to load previous or next attachment with jquery ?
- How to set variables with AJAX request to use in another function in WordPress
- Search Ajax Call – Use Form Data in Response
- How to display contact form 7 form in vanilla js without jquery in frontend
- Validating an email input from form field before submit using JQuery, AJAX, and PHP
- Change each text after 4 second and each image after 2 second
- Why does PHP called with Jquery AJAX not allow additional php code to be added?
- Ajax call not working in wordpress through a plugin
- Send Array from JS to php file then insert it in wp database through a plugin
- How to use wp_send_json() and output the value with ajax?