Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>  ';
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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>';
}
share|improve this answer
    
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 –  mattnewbie Jan 28 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 Jan 28 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. –  mattnewbie Jan 28 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 Jan 28 at 17:38
1  
That's okay this way, just be sure your Plugin does not output anything directly. –  fischi Jan 28 at 17:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.