views:

141

answers:

2

Hi,

I am using Cakephp and JQuery in my application. I have created the form using

<?php
 echo $form->create('Result',array('action'=>"submit/$formid"));

 foreach ($viewfields as $r): 
 echo $form->input($r['Attribute']['label'], array('id'=>$r['Attribute']['id'],'name'=>$r['Attribute']['label'],'type'=>'text'));
  endforeach; 
 echo $form->end('submit');?>

This will creates a Form with id for eg...ResultSubmit1Form where 1 is the formid

When i click on the submit button it submits the data correctly. But i want to add JQuery validations like none of the fields has to be empty .

I tried it with some examples But itt didnt works .How can i do so.....Please suggest me..

Edit:

I tried it with

  $("#ResultSubmit/$formidForm").submit(function(event){
   event.preventDefault();
  /* do validation of fields */
  if($("#"+<?=$r['Attribute']['id'];?>).fieldValue()==""){alert("Fill in  "+"<?=$r['Attribute']['label'];?>");
            $("#"+<?=$r['Attribute']['id'];?>).focus();
     }
  });

But nothing works ,this didnt enters into event at all..

+3  A: 

Updated Response...

You're attempting to mix PHP with Javascript, that won't work. Take this line for instance:

$("#ResultSubmit/$formidForm")

You're printing this out as plain-text js-code, meaning $formidForm is not understood to be a variable containing a value, it's handled as "dollar-sign formidform." If you wanted to print the PHP-value, you'd have to do something like:

$formidForm = "#userDetails";
$("<?php print $formidForm; ?>") /* $("#userDetails") */

Now that it's within <?php and ?>, your server knows to interpret that particular part as PHP, and not plain-text.

As for the rest of your js-code, what does it look like when you copy/paste it from the webpage for the viewer?

Original Response...

You would bind the check to the .submit() event of the form:

$("#myForm").submit(function(event){
  event.preventDefault();
  /* do validation of fields */
});

You could benefit from a utilizing a Validation Plugin too.

Jonathan Sampson
+1  A: 

I am using jquery validation plugin and its documentation is also nice. It works like charm to me

dhaval
thank you karim for the edit
dhaval