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 to change CSS using jQuery?
- How to properly use jsPDF library
- How can I scroll to an element using jQuery?
- jQuery Get Selected Option From Dropdown
- Setting “checked” for a checkbox with jQuery
- Jquery : Refresh/Reload the page on clicking a button
- How to set ‘X-Frame-Options’ on iframe?
- jQuery count child elements
- How do I check if an element is hidden in jQuery?
- SyntaxError: Unexpected token C in JSON at position 0 – Ionic 2 Http GET request
- How to get the children of the $(this) selector?
- window.onload vs $(document).ready()
- jQuery scrollTop() method not working
- How can I know which radio button is selected via jQuery?
- jquery toggleClass not working
- JQuery Number Formatting
- How to add jQuery in JS file
- How to put JQuery/Ajax inside shortcode?
- Ajax request not sending to server and returning – wp-admin/admin-ajax.php 400
- HTML escaping data with ajax requests
- How to display contact form 7 form in vanilla js without jquery in frontend
- WordPress Search Ajax + Isotope + InfiniteScroll
- WordPress ajax filter returning all posts when it should be filtering by category
- WordPress AJAX load post metadata in modal
- Uncaught ReferenceError: tippy is not defined
- Metabox conditionals depending on post format and template in Gutenberg