22/**
33 * Doing AJAX the WordPress way.
44 * Use this class in admin or user side
5+ *
6+ * @author Nirjhar Lo
7+ * @version 1.2.1
8+ * @package wp-plugin-framework
59 */
610if ( ! defined ( 'ABSPATH ' ) ) exit ;
711
1115 final class PLUGIN_AJAX {
1216
1317
14- // Add basic actions
18+ /**
19+ * Add basic actions
20+ *
21+ * @return Void
22+ */
1523 public function __construct () {
1624
17- //Adding the AJAX
18- add_action ( 'wp_footer ' , array ( $ this , 'customName_js ' ) );
19- add_action ( 'wp_ajax_customName ' , array ( $ this , 'customName ' ) );
20- add_action ( 'wp_ajax_nopriv_customName ' , array ( $ this , 'customName ' ) );
25+ add_action ( 'wp_footer ' , array ( $ this , 'custom_name_js ' ) );
26+ add_action ( 'wp_ajax_custom_name ' , array ( $ this , 'custom_name ' ) );
27+ add_action ( 'wp_ajax_nopriv_custom_name ' , array ( $ this , 'custom_name ' ) );
2128 }
2229
2330
24-
25- //Output the form
31+ /**
32+ * Output the form
33+ *
34+ * @return Html
35+ */
2636 public function form () { ?>
2737
28- <form id="addByAjax " method="POST" action="">
29- <input type="text" name="textName " placeholder="<?php _e ( 'Text ' , 'textdomain ' ); ?> ">
30- <input id="AjaxSubmit " type="submit" name="submit" value="Submit">
38+ <form id="add_by_ajax " method="POST" action="">
39+ <input type="text" name="text_name " placeholder="<?php _e ( 'Text ' , 'textdomain ' ); ?> ">
40+ <input id="ajax_submit " type="submit" name="submit" value="Submit">
3141 </form>
3242 <?php
3343 }
3444
3545
36-
37- //The javascript
38- public function customName_js () { ?>
46+ /**
47+ * The javascript
48+ *
49+ * @return Html
50+ */
51+ public function custom_name_js () { ?>
3952
4053 <script type="text/javascript">
4154 jQuery(document).ready(function() {
4255
43- jQuery("#addByAjax form").submit(function() {
56+ jQuery("#add_by_ajax form").submit(function() {
4457
4558 event.preventDefault();
4659
47- var val = jQuery("input[name='textName ']").val();
60+ var val = jQuery("input[name='text_name ']").val();
4861
4962 jQuery.post(
50- <?php if (isset ($ _SERVER ['HTTPS ' ]) && $ _SERVER ['HTTPS ' ] == "on " ) { ?>
51- '<?php echo admin_url ("admin-ajax.php " , "https " ); ?> ',
52- <?php } else { ?>
53- '<?php echo admin_url ("admin-ajax.php " ); ?> ',
54- <?php } ?>
55- { 'action': 'customName', 'val': val },
63+ '<?php echo admin_url ("admin-ajax.php " ); ?> ',
64+ { 'action': 'custom_name', 'val': val },
5665 function(response) {
5766 if ( response != '' && response != false && response != undefined ) {
5867
@@ -69,17 +78,20 @@ function(response) {
6978 }
7079
7180
72-
73- //The data processor
74- public function customName () {
81+ /**
82+ * The data processor
83+ *
84+ * @return Json
85+ */
86+ public function custom_name () {
7587
7688 $ val = $ _POST ['val ' ];
7789
7890 // DO some stuff
79-
91+
8092 $ response = array ( 'val ' => $ value );
8193 echo json_encode ( $ response );
8294 wp_die ();
8395 }
8496 }
85- } ?>
97+ } ?>
0 commit comments