Welcome Guest, Not a member yet? Register   Sign In
jquery (newbie )
#1

[eluser]marjune[/eluser]
ei guys, please help i'm a newbie in jquery, inside my dialog i had a two input text and a
submit button, and every time the user click the submit button it will check the the inputs
for validation, the problem is it will validate the input once.

my code:

$(function() {
var error = $(".err"),
topic = $("#topic"),
thread = $("#thread"),
allFields = $([]).add(error);
$dialog = $('.dialog')
.dialog({
autoOpen: false,
title: 'Add New Topic',
height: 290,
width: 600
});

$('.topic').click(function() {
$dialog.dialog('open');

});

function ferror(value, txt){
if(value.val() == ""){
error.addClass('ui-state-error');
displayerror(value + " is required.");
return false;
}
else
return true;
}

function displayerror(e){
error.html(e);
error.fadeOut(1500);
}

$("#post").click(function(){
var inputcheck = true;
allFields.removeClass('ui-state-error');
inputcheck = inputcheck && ferror(topic,"Topic"),
inputcheck = inputcheck && ferror(thread,"Thread");
if(inputcheck){
alert('input ok');
$dialog.dialog('close');
return false;
}
return false;
});
});
#2

[eluser]jdfwarrior[/eluser]
Why don't you just use jquery to post the data to a codeigniter function, run the validation there, and then return the result, let jquery do whatever after the input has been validated.
#3

[eluser]Samuurai[/eluser]
That sounds interesting, jdfwarrior! Can you give me a hint on what to search for on how to do this? I take it you mean, using ajax?
#4

[eluser]jdfwarrior[/eluser]
[quote author="Samuurai" date="1255554354"]That sounds interesting, jdfwarrior! Can you give me a hint on what to search for on how to do this? I take it you mean, using ajax?[/quote]

The link in my sig will give you some sample code and kinda walk you through it. I plan on getting a screencast done on this topic sometime soon.

Yeah, using ajax. You can use jQuery's AJAX functionality (I chose $.post) to send the data to a CodeIgniter controller/function. Use the CodeIgniter form validation library to validate the inputs, and just pass a result back to jQuery using a php echo. You can grab that result using the jQuery callback, and then move on accordingly.
#5

[eluser]marjune[/eluser]
what i want is before passing to the ci controller function i want the data is ok so that i make a validation function using jquery in the page!!!

i try also validating my inputs to controller function by

$.post('index.php/controller/controller_function', {value: topic}, function(data){

//code here

});
but my problem is i cant get the value of topic = $("#topic"); its undefined.... please help!!!!1111 htanks
#6

[eluser]jdfwarrior[/eluser]
You say "before passing to the ci controller" like once thats done, nothing else can happen. The validation in CI will be a lot easier than in jQuery/Javascript due to the existence of the form validation library. In the example I told you about, its done asynchronously. It submits to a controller/function, but if you pass the event in, and use preventDefault(), you can stop it from submitting. For instance..

Code:
$(document).ready(function() {

  $("#mybutton").click(function(e) {
    
    e.preventDefault();

    $.post('controller/function', { data }, function(data) {
      
       //here, take the data variable and do whatever with it.
       //it can be the form validation error message, just a state
       //telling whether or not it was valid, etc.
       //do something where, if (data == "failed") { show error message }
       // else { post to the submit controller/function }  

    });

  });

});
#7

[eluser]marjune[/eluser]
ok thanks i'm aware of that, but my problem is the data that i'm going to post to the controller/samplefunction is nothing even thought i input something on the input text, i know this is very
simple but its really twist.

it goes this way

$(document).ready(function() {

$("#post").click(function(e) {
var value = $("#topic").val();
e.preventDefault();
$.post('controller/samplefunction', {value: value}, function(data) {
alert(data);
});
});
});

my html sample input data

Topic:<input type="text" id="topic"><br>
<input type="submit" value="Post" id="post">

then my controller/samplefunction

function samplefunction(){
$this->load->helper('url');
$value = $this->input->post('value');
echo $value;
}




Theme © iAndrew 2016 - Forum software by © MyBB