0

I have a WordPress Frontend Form, that uses AJAX to save data. The form is processed in backend obviously using PHP post, I did a simple solution to inform form submission errors to users if the form could not be submitted.

But My forms are specific to Logged In Users, I want my form to show an error message if WordPress user is logged out.

<form action="https://milyin.com/my/" method="post" name="milyin-add-edit" id="Add-Edit" enctype="multipart/form-data"> <input type="hidden" name="action" value="wpfrtp_save_post"> 

This is the start of the form, I believe that the rest of the form HTML is genuinely irrelevant to this question, so I shall not include that, incase still needed just inform me in comments, and it won't be a problem either.

here's the JS used to Submit Form Via AJAX, and then also to show some errors.

jQuery('#Add-Edit').on('submit', function(e) { e.preventDefault(); jQuery('.action-save').prop('disabled', true); jQuery.ajax({ url : jQuery(this).attr('action') || window.location.pathname, type: "POST", data: jQuery(this).serialize(), error: function(jqXHR) { jQuery(".action-save").html("Error"); jQuery(".action-save").css("background-color", "red"); jQuery("#user_post_publish").html("Error"); jQuery(" #user_post_publish").css("background-color", "red"); msg = ''; if (jqXHR.status === 0) { msg = 'No Internet Connection Available. Turns out your Internet has gone to a random hibernation'; } else if (jqXHR.status == 404) { msg = 'Requested page not found. [404]'; } else if (jqXHR.status == 500) { msg = 'Internal Server Error [500]. Hold Tight, Our Server turned Evil, we will solve it soon. Please try again after some time.'; } else if (exception === 'parsererror') { msg = 'Requested JSON parse failed.'; } else if (exception === 'timeout') { msg = 'Time out error. It took too long to transfer the data, please save again.'; } else if (exception === 'abort') { msg = 'Ajax request aborted.'; } else { msg = 'Uncaught Error.\n' + jqXHR.responseText; } jQuery('.Error').html(msg); jQuery('.Error').show(); } }); }); 

As you can see my in my code, in case we incur any kind of form submission error, we turn the buttons to red and change their text to "Error" then we check the error code, and show a message based on the error code.

Now, if the user logs out, and tries to submit the form, it never says an "Error" because form submitted successfully, the AJAX seeks the success function rather than the error function.

What I am trying to do:

So, first of all, I want to ensure that if the user is logged out AJAX knows its an Error in form submit, and the success() should not run, I did not include the code of success() because its lengthy and seems irrelevant.

Secondly, I want to tell the error function that the error was due to the user being logged out, so that I can inform user of it being logged out by jQuery('.Error').html(msg);

if(isset($_POST['action'])&& $_POST['action']=="wpfrtp_save_post"){ if ( !wp_verify_nonce( $_POST['frontier_add_edit_post_'.$_POST['postid']], 'frontier_add_edit_post' ) ){ [...] global $current_user; if(!is_user_logged_in()){ //This is the problem region // I don't understand What Should come here } 

    1 Answer 1

    1

    A response is needed from php side after sending an AJAX call.

    // ... rest of the code if ( is_user_logged_in() ) { wp_send_json_success(); } else { wp_send_json_error( null, 403 ); } // ... rest of the code 

    And then in your JS code add a new check for jqXHR.status == 403 error.

    Documentations:

    wp_send_json_success

    wp_send_json_error

    3
    • Hey there, sorry for late reply, 2 questions, first of all, I am not using wp-admin/admin-ajax.php so will wp_send_json_error() work with a custom php file? Also how can I distinguish, like if I want to check if the wp_nonce is invalid and send error for it, with 403 but distinguish between the 2, like how do I know which condition threw 403 error for frontend.CommentedAug 31, 2020 at 16:38
    • 1. Yes, it will work. It is just sending needed headers and data in json format and does what is needed to respond to an AJAX request. 2. As the documentation says, you can use custom status code in wp_send_json... functions. Also you can pass custom data and check it in JS code like jqXHR.data.customDataName='something'
      – Hector
      CommentedAug 31, 2020 at 16:59
    • Oh wow, Thank You, I shall test this on my production site, and let you know in an hour or 2CommentedSep 1, 2020 at 4:39

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.