Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. function add_my_css_and_my_js_files(){
  2. wp_enqueue_script('jquery-validate-min', plugins_url('jquery_validate_min.js', __FILE__ ) );
  3. wp_enqueue_script( "the-calcs", plugins_url('the_calcs.php', __FILE__ )) ;
  4. }
  5. add_action('wp_enqueue_scripts', "add_my_css_and_my_js_files");
  6.  
  7. function include_jQuery() {
  8. if (!is_admin()) {
  9. wp_enqueue_script('jquery');
  10. }
  11. }
  12. add_action('init', 'include_jQuery');
  13.  
  14. add_action( 'init', 'register_shortcodes' );
  15. function register_shortcodes() {
  16. add_shortcode( 'displaycalcs', 'mp_calcs_display' );
  17. }
  18.  
  19. function mp_calcs_display() {
  20. echo '<a class="ajax-link" href="#">click me</a>';
  21.  
  22. $output = <<<HTML
  23. <form action="" method="post" name="formsubmit" id="formsubmit" >
  24. <h1> Process </h1>
  25. <p> operation type always robot </p>
  26. Number of welds: <input type="number" name="numberofwelds" id="numberofwelds" >
  27. Number of construction welds: <input type="number" name="numberofconwelds" id="numberofconwelds" >
  28. Total one: <input type="text" name="totalone" id="totalone" disabled>
  29. <div id="totalfail1"></div>
  30. Total two: <input type="text" name="totaltwo" id="totaltwo" disabled>
  31. <div id="totalfail2"></div>
  32. Total three: <input type="text" name="totalthree" id="totalthree" disabled>
  33. <div id="totalfail3"></div>
  34. <input type="submit" value="Calculate" id="submit" name="submit">
  35. <div id="result"> </div>
  36. </form>
  37. <script type="text/javascript">
  38. jQuery(document).ready(function($) {
  39. $('#formsubmit').validate({
  40. rules: {
  41.  
  42. numberofwelds: "required",
  43. numberofconwelds: "required"
  44. },
  45. messages: {
  46. numberofwelds: "Please enter the number of welds",
  47. numberofconwelds: "Please enter number of con"
  48. },
  49.  
  50. submitHandler: function(form) {
  51. form.submit();
  52. }
  53. });
  54. $(".ajax-link").click( function() {
  55. var data = {
  56. action: 'test_response',
  57. post_var: 'this will be echoed back'
  58. };
  59. $.post(the_ajax_script.ajaxurl, data, function(response) {
  60. alert(response);
  61. });
  62. return false;
  63. });
  64. });
  65. </script>
  66. HTML;
  67. return $output; }
  68.  
  69. function test_ajax_load_scripts() {
  70. wp_localize_script( 'activate2', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
  71. }
  72. add_action('wp_enqueue_scripts', 'test_ajax_load_scripts');
  73. function text_ajax_process_request() {
  74. if ( isset( $_POST["post_var"] ) ) {
  75. $response = $_POST["post_var"];
  76. echo $response;
  77. die();
  78. }
  79. }
  80. add_action('wp_ajax_test_response', 'text_ajax_process_request');
  81.  
  82. // enqueue and localise scripts
  83. wp_enqueue_script( ‘my-ajax-handle’, plugin_dir_url( __FILE__ ) . ‘ajax.js’, array( ‘jquery’ ) );
  84. wp_localize_script( ‘my-ajax-handle’, ‘the_ajax_script’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ) ) );
  85. // THE AJAX ADD ACTIONS
  86. add_action( ‘wp_ajax_the_ajax_hook’, ‘the_action_function’ );
  87. add_action( ‘wp_ajax_nopriv_the_ajax_hook’, ‘the_action_function’ ); // need this to serve non logged in users
  88. // THE FUNCTION
  89. function the_action_function(){
  90. /* this area is very simple but being serverside it affords the possibility of retreiving data from the server and passing it back to the javascript function */
  91. $name = $_POST[‘name’];
  92. echo”Hello World, ” . $name;// this is passed back to the javascript function
  93. die();// wordpress may print out a spurious zero without this – can be particularly bad if using json
  94. }
  95. // ADD EG A FORM TO THE PAGE
  96. function hello_world_ajax_frontend(){
  97. $the_form = ‘
  98. <form id=”theForm”>
  99. <input id=”name” name=”name” value = “name” type=”text” />
  100. <input name=”action” type=”hidden” value=”the_ajax_hook” />&nbsp; <!– this puts the action the_ajax_hook into the serialized form –>
  101. <input id=”submit_button” value = “Click This” type=”button” onClick=”submit_me();” />
  102. </form>
  103. <div id=”response_area”>
  104. This is where we’ll get the response
  105. </div>’;
  106. return $the_form;
  107. }
  108. add_shortcode(“hw_ajax_frontend”, “hello_world_ajax_frontend”);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement