Welcome Guest, Not a member yet? Register   Sign In
JQuery Validation
#1

[eluser]RobbieL[/eluser]
Sorry to post another JQuery/Validation post, but I had a hunt and couldn't see a thread looking at my problem. I've managed to get JQuery to use the CI validation, but I can only get it working to validate one input field. My form field has multiple input fields, but I'm unsure how to post the value of them all to the CI controller that does the validation.

I'm currently at work so can't post my code at the moment, but any feedback would be greatly appreciated.

Cheers.
#2

[eluser]hostingx[/eluser]
use action="Path to your controller i.e. http://site.ext/controller/function"

<form method="POST" action="Path to your controller i.e. http://site.ext/controller/function">

</form>
#3

[eluser]Phil Sturgeon[/eluser]
Post your code and I will help you out on this one, this is something I never got round to working on.
#4

[eluser]xwero[/eluser]
To get all the values you would have to do something like
Code:
var post = '';
$('#formid input,select,textarea').each(function(){
   var name = $(this).attr('name');
   switch($(this).attr('type'))
   {
      case 'checkbox':
      case 'radio':
        post += name+'="'+$(':checked',this).val()+'"&';
        break;
      default:
        post += name+'="'+$(this).val()+'"&';
        break;
   }
  
});
post = post.substring(0,post.length-1);
// ajax
This is just off the top of my head but it should give you some idea on how to gather fields. I think you should take a look at the validation plugin code to see how they gather the field values.
#5

[eluser]RobbieL[/eluser]
Cheers for the responses. I'm still at work, but can provide some code off the top of my head.

Code:
$(function(){
      $("#registration_form").submit(function(){
           $.ajax({
                   url: "http://localhost:8888/project/index.php/registration/validate",
               data: "id="+$('#username').val(),
               success: function(){
                    alert('Success');
               }
            });

            return false;
       });
});

It's just from memory, so it might not be 100% correct.
#6

[eluser]bijon[/eluser]
Quote:var post = '';
$('#formid input,select,textarea').each(function(){
var name = $(this).attr('name');
switch($(this).attr('type'))
{
case 'checkbox':
case 'radio':
post += name+'="'+$(':checked',this).val()+'"&';
break;
default:
post += name+'="'+$(this).val()+'"&';
break;
}

});
post = post.substring(0,post.length-1);

YOu can do all the above things in just single line
Quote:var str = $("form").serialize();
Quote:you can check the Jquery serialize

However i have done exactly what you have done. If you nedd the solve then you can again request ..
Now you can play with that.. Smile

Enjoy
you can check the Jquery serialize
#7

[eluser]xwero[/eluser]
Nothing beats jQuery power (I even have a tattoo of the logo Smile )
#8

[eluser]syntax error[/eluser]
I use XML response from codeigniter validation to validate form data. The XML response is rendered simply using a view. Validation errors are stored within XML response.

Perhaps this will help?
Code:
function ajaxSubmit(form) {
      var params = {};
      // build param list from form field values
      $(form).find("input[@checked], input[@type='text'], input[@type='file'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
      .filter(":enabled")
      .each(function() {
        params[this.name] = this.value;
      });
      // post form to server
      $.post(form.getAttribute("action"), params, function(xml){
        strError = "Unable to submit form. Please try again later.";
        // process xml response
        $("AjaxResponse", xml).each(function() {
          strError = $('error', this).text();
          strRedirect = $('strRedirect', this).text();
        });
        // success!
        if (strError.length == 0) {
          [removed] = strRedirect;
        }
        // errors!
        else {
          alert("The following errors were encountered:\n\n" + strError);
        }
      });
    return false;
}

This line: [removed] = strRedirect; should be: [removed] = strRedirect;
Lol hmm this forum does not like ( window . location )
#9

[eluser]xwero[/eluser]
@richard : if you want to show javascript some parts will be removed because of the filtering on the forum so to show your code properly you can use a html entities where needed.

Maybe there should be a sticky about this.
#10

[eluser]nmweb[/eluser]
On a side note, the next version of the validation plugin for jQuery will have Ajax checking integrated. It'll be much easier then Smile




Theme © iAndrew 2016 - Forum software by © MyBB