admin管理员组文章数量:1126116
I am new to UI. I have created a REST API and a form in my Wordpress website. Could you please suggest how to make POST call to my REST endpoint on-click event of the form submit button?
- How to open /wp-admin/admin-ajax.php in IDE? I am hosting the website in Godaddy.
- How to get the value of each field of the form?
- How to make POST call to my REST endpoint on-click event and get the POST API response?
Thank you for your help! Nav
I am new to UI. I have created a REST API and a form in my Wordpress website. Could you please suggest how to make POST call to my REST endpoint on-click event of the form submit button?
- How to open /wp-admin/admin-ajax.php in IDE? I am hosting the website in Godaddy.
- How to get the value of each field of the form?
- How to make POST call to my REST endpoint on-click event and get the POST API response?
Thank you for your help! Nav
Share Improve this question asked Feb 11, 2024 at 20:17 NavNav 11 bronze badge 1- With the help of AJAX. – Aftab Commented Feb 12, 2024 at 4:00
2 Answers
Reset to default 0You donot need to edit or modify the wp-admin/admin-ajax.php as this is wp core file. If you want to use ajax you can check here
For getting the values of form you can use serialize() function of jQuery.
Please check the below code for calling REST API on click event.
$('button').click(function(){ $.ajax({ type : "POST", dataType : "JSON", url : "/wp-json/v2/......", data : $(form#formID).serialize(), success: function(response) { alert(response); } });
});
HTML :
<div id="border">
<form action="/" id="registerSubmit">
<div id="userError"></div>
Username: <input type="text" name="username" id="username" size="10"/><br>
<div id="emailError" ></div>
Email: <input type="text" name="email" size="10" id="email"/><br>
<div id="passError" ></div>
Password: <input type="password" name="password" size="10" id="password"/><br>
<div id="passConfError" ></div>
Confirm Password: <input type="password" name="passconf" size="10" id="passconf"/><br>
<input type="submit" name="submit" value="Register" />
</form>
</div>
jQuery:
$('form').on('submit', function(e) {
e.preventDefault();
var form_data = $("#registerSubmit").serialize() // returns all the data in your form
$.ajax({
type: "POST",
url : "/wp-admin/admin-ajax.php",
data: {action: "save_form_data" , "data": form_data},
success: function() {
//success message mybe...
}
});
});
PHP :
add_action( 'wp_ajax_nopriv_save_form_data', 'my_ajax_save_form_data' );
add_action( 'wp_ajax_save_form_data', 'my_ajax_save_form_data' );
function my_ajax_save_form_data() {
$form_data = $_POST['data'];
/* here you can use CURL for post data to other your rest API */
}
本文标签: From Wordpress form submit onclick eventhow to call an external REST POST API
版权声明:本文标题:From Wordpress form submit on-click event, how to call an external REST POST API? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736653152a1946191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论