One is When I use the shortcode of the form in the page or post,
showing the form in the WP backend 5.2.3 version.
That happens because your shortcode is echoing the output. A shortcode should always return the output, but if you need to echo it, then you should use output buffering:
public function ajaxrf_shortcode(){
// Turn on output buffering.
ob_start();
self::AjaxdataProcess();
self::ajax_reservation_form(); // echo the form
// Turn off output buffering and then return the output echoed via the above functions.
return ob_get_clean();
}
Another one is when I check the nonce field using this function
check_ajax_referer(). The form does not appear in the view page.
Only showing -1 in the view page.
First off, why are you calling AjaxdataProcess() in the shortcode function (ajaxrf_shortcode())? Is it because you are allowing non-AJAX form submissions?
But that “-1” could happen if the (AJAX) request verification failed (e.g. due a missing or an expired nonce) and by default, check_ajax_referer() will call either wp_die() if doing an AJAX request or die() otherwise.
And to prevent WordPress/PHP execution from being halted upon failure of the request verification, set the third parameter to false when you call check_ajax_referer():
check_ajax_referer( 'rsf_nonce_action', 'snonce', false )
If you want to allow non-AJAX form submissions…
-
Make sure the relevant form fields are assigned the proper
name; e.g.:<input type="text" class="form-control" id="RFname" name="RFname"> <input type="email" class="form-control" id="RFemail" name="RFemail"> -
You should also set the form’s
methodtopost(becauseAjaxdataProcess()is using$_POST):<form action="<?php the_permalink(); ?>" id="arform" method="post"> -
Although your form does include the proper nonce, WordPress/PHP doesn’t recognize it because you’re not using the proper name — you used
snoncewithcheck_ajax_referer()(and in your JS script), but in the form, the name isrsf_nonce_field:-
In
ajax_reservation_form()— the value of the second parameter forwp_nonce_field()should match the value of the second parameter forcheck_ajax_referer()(and in fact, same goes to the first parameter..):<?php wp_nonce_field( 'rsf_nonce_action', 'rsf_nonce_field');?>
So that
rsf_nonce_fieldshould be changed tosnonce. -