Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function add_my_css_and_my_js_files(){
- wp_enqueue_script('jquery-validate-min', plugins_url('jquery_validate_min.js', __FILE__ ) );
- wp_enqueue_script( "the-calcs", plugins_url('the_calcs.php', __FILE__ )) ;
- }
- add_action('wp_enqueue_scripts', "add_my_css_and_my_js_files");
- function include_jQuery() {
- if (!is_admin()) {
- wp_enqueue_script('jquery');
- }
- }
- add_action('init', 'include_jQuery');
- add_action( 'init', 'register_shortcodes' );
- function register_shortcodes() {
- add_shortcode( 'displaycalcs', 'mp_calcs_display' );
- }
- function mp_calcs_display() {
- echo '<a class="ajax-link" href="#">click me</a>';
- $output = <<<HTML
- <form action="" method="post" name="formsubmit" id="formsubmit" >
- <h1> Process </h1>
- <p> operation type always robot </p>
- Number of welds: <input type="number" name="numberofwelds" id="numberofwelds" >
- Number of construction welds: <input type="number" name="numberofconwelds" id="numberofconwelds" >
- Total one: <input type="text" name="totalone" id="totalone" disabled>
- <div id="totalfail1"></div>
- Total two: <input type="text" name="totaltwo" id="totaltwo" disabled>
- <div id="totalfail2"></div>
- Total three: <input type="text" name="totalthree" id="totalthree" disabled>
- <div id="totalfail3"></div>
- <input type="submit" value="Calculate" id="submit" name="submit">
- <div id="result"> </div>
- </form>
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- $('#formsubmit').validate({
- rules: {
- numberofwelds: "required",
- numberofconwelds: "required"
- },
- messages: {
- numberofwelds: "Please enter the number of welds",
- numberofconwelds: "Please enter number of con"
- },
- submitHandler: function(form) {
- form.submit();
- }
- });
- $(".ajax-link").click( function() {
- var data = {
- action: 'test_response',
- post_var: 'this will be echoed back'
- };
- $.post(the_ajax_script.ajaxurl, data, function(response) {
- alert(response);
- });
- return false;
- });
- });
- </script>
- HTML;
- return $output; }
- function test_ajax_load_scripts() {
- wp_localize_script( 'activate2', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
- }
- add_action('wp_enqueue_scripts', 'test_ajax_load_scripts');
- function text_ajax_process_request() {
- if ( isset( $_POST["post_var"] ) ) {
- $response = $_POST["post_var"];
- echo $response;
- die();
- }
- }
- add_action('wp_ajax_test_response', 'text_ajax_process_request');
- // enqueue and localise scripts
- wp_enqueue_script( ‘my-ajax-handle’, plugin_dir_url( __FILE__ ) . ‘ajax.js’, array( ‘jquery’ ) );
- wp_localize_script( ‘my-ajax-handle’, ‘the_ajax_script’, array( ‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ) ) );
- // THE AJAX ADD ACTIONS
- add_action( ‘wp_ajax_the_ajax_hook’, ‘the_action_function’ );
- add_action( ‘wp_ajax_nopriv_the_ajax_hook’, ‘the_action_function’ ); // need this to serve non logged in users
- // THE FUNCTION
- function the_action_function(){
- /* 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 */
- $name = $_POST[‘name’];
- echo”Hello World, ” . $name;// this is passed back to the javascript function
- die();// wordpress may print out a spurious zero without this – can be particularly bad if using json
- }
- // ADD EG A FORM TO THE PAGE
- function hello_world_ajax_frontend(){
- $the_form = ‘
- <form id=”theForm”>
- <input id=”name” name=”name” value = “name” type=”text” />
- <input name=”action” type=”hidden” value=”the_ajax_hook” /> <!– this puts the action the_ajax_hook into the serialized form –>
- <input id=”submit_button” value = “Click This” type=”button” onClick=”submit_me();” />
- </form>
- <div id=”response_area”>
- This is where we’ll get the response
- </div>’;
- return $the_form;
- }
- add_shortcode(“hw_ajax_frontend”, “hello_world_ajax_frontend”);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement