0

I am trying to use a Jquery validation plugin called bassistance. I have wp_enqueue_script the script. I have loaded Jquery as I understand it's built into wordpress. I have included the <script> tag so that I can .validate() it . I have added the rules by adding class to each field. This is all in one .php script, as I'm just learning. It doesn't seem to work. Please can someone advise? Has the <style> tag been used wrongly?

add_action( 'wp_enqueue_script', 'load_jquery2' );
    function load_jquery2() {
        wp_enqueue_script( 'jquery' );       }      

    function add_my_css_and_my_js_files(){
            wp_enqueue_script('jquery-validate-min', plugins_url('activate/jquery_validate_min.js', __FILE__ ) );
     }
      add_action('wp_enqueue_scripts', "add_my_css_and_my_js_files");

      <script type="text/javascript">
     jQuery(document).ready(function($) {
     $('#calsinput').validate();
    });  
    </script>

function mp_calcs_display() {
return '
<form name="calsinput" action="" method="post" id="calsinput" >
<h1> Process </h1>
<p> operation type always robot </p>
<br> <br>
Number of welds: <input type="number" name="numberofwelds" class="required digits" title="This field is required and must be a no."  >
<br> <br>
Number of construction welds: <input type="number" name="numberofconwelds" class="required digits"  title="This field is required and must be a no.">
<input type="submit" value="Calculate cycle time ">
</form>  ';
}

1 Answer 1

1

You are mixing PHP and Javascript.

The part between <script> must not be in the PHP code, but you could put it in the variable just after </form>.

Another way of doing it would be to create a Javascript file myfunctions.js, putting the code into it and enqueueing it.

The code would be:

add_action( 'wp_enqueue_script', 'load_jquery2' );
function load_jquery2() {
    wp_enqueue_script( 'jquery' );
}      

function add_my_css_and_my_js_files(){
    wp_enqueue_script('jquery-validate-min', plugins_url('activate/jquery_validate_min.js', __FILE__ ) );
}
add_action('wp_enqueue_scripts', "add_my_css_and_my_js_files");


function mp_calcs_display() {
return '
<form name="calsinput" action="" method="post" id="calsinput" >
<h1> Process </h1>
<p> operation type always robot </p>
<br> <br>
Number of welds: <input type="number" name="numberofwelds" class="required digits" title="This field is required and must be a no."  >
<br> <br>
Number of construction welds: <input type="number" name="numberofconwelds" class="required digits"  title="This field is required and must be a no.">
<input type="submit" value="Calculate cycle time ">
</form>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        $("#calsinput").validate();
    });  
</script>';
}
8
  • Thanks, but I'm confused. You haven't used wp_enqueue_script on an extra file in your example? You just moved <script type="text/javascript"> further down the page
    – user28566
    Commented Jan 28, 2014 at 17:17
  • 1
    No, I went with the simple solution of just outputting it after the form, as it is not the best thing to do to include a scriptfile containing just 5 lines of code.
    – fischi
    Commented Jan 28, 2014 at 17:20
  • Thanks, that makes sense. The Jquery plugin doesn't seem to be working still though. I get the The plugin generated 655 characters of unexpected output during activation. message on the plugin activation page. Is there a way to get a more detailed explanation of errors like this, that you know of? Thank-you.
    – user28566
    Commented Jan 28, 2014 at 17:32
  • 1
    Oh sorry, I did read that wrong. You have to check that the plugin does not output anything directly (i suppose you call echo mp_calcs_display();), but to make it available with a shortcode or a filter on the content.
    – fischi
    Commented Jan 28, 2014 at 17:38
  • 1
    That's okay this way, just be sure your Plugin does not output anything directly.
    – fischi
    Commented Jan 28, 2014 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.