Welcome Guest, Not a member yet? Register   Sign In
jquery and CI
#1

[eluser]mehwish[/eluser]
hi, Can anyone tell me where a .js file is placed and how to load a .js file or how to call it from controller and how to return values from any function of jquery to controller like we do with controller and model (as we call a model's function and when that model's function returns a value we place it in a variable)
#2

[eluser]Amitabh Roy[/eluser]
check this thread


http://ellislab.com/forums/viewthread/172363/

this should help you to set up jquery and js files


Passing values from jquery to controller can be done by various methods, it depends in which context you are asking. If you can elaborate more it would be helpful.

I generally pass values from jquery to CI controller's using the $.ajax method

Code:
serverData = some field values in firm of query string;
        }
    
     $.ajax({
           type: "POST",
            url: "<?php echo base_url();?>index.php/auth/authuser",
           data: serverData,
        success: function(msg){alert("Hi");}
              });

http://ellislab.com/forums/viewthread/190618/

You can check this post too

http://ericlbarnes.com/blog/post/codeign..._with_ajax
#3

[eluser]mehwish[/eluser]
I can expalin my context to you please consider it and tell me the solution.

Actually i have a controller "my_controller" and from it i am loading view "my_view" and i have also included some jquery functions in "my_view" like this:

my_view.php

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
[removed]
</head>

<body>
<h2>type your query: </h2>
<div>&lt;textarea cols="50" rows="4" name="query_box" id="textbox"&gt;&lt;/textarea></div><br/>

<div>
&lt;input type='button' value='Add Button' id='addButton'&gt;
&lt;input type='button' value='Remove Button' id='removeButton'&gt;
&lt;input type='button' value='Get TextBox Value' id='getButtonValue'&gt;
&lt;input type='button' value='doneButtonvalue' id='doneButton'&gt;
</div>

[removed]

var counter=2;
var m='';
$(document).ready(function() {
      $("#addButton").click(function(){
          counter++;
          $(this).before(/*'<h4>box'+counter+'</h4>*/'&lt;input type="textbox" name="textbox' + counter + '" id="textbox' + counter + '" value="textbox'+ counter + '" /&gt;&lt;br/>')
          m=$('#textbox' + counter).val();
          alert(m);

});
});
[removed]


now I want to return "m" and "counter" values so that i can access them through my controller or if not from controller atleast i can echo "m" and "counter" outside the script tag in the same view file. I hope you got my point ... please tell me how it can be done ?
#4

[eluser]Amitabh Roy[/eluser]
You are setting the values of counter and m. And you have defined them as javascript variables.

For a more convenient programming I think it would be a better option to define them as hidden fields

Code:
&lt;input type='hidden' value='' id='m'&gt;
&lt;input type='hidden' value='2' id='counter'&gt;

In this way you can access the m and counter values more efficiently

Code:
$(document).ready(function() {
      $("#addButton").click(function(){
        
       // counter++;  
        var tempCounter = $('#counter').val();
       $('#counter').val(tempCounter++);

          ....middle section of the code is bit unclear...........
        
        var tempM =$('#textbox' + tempCounter).val();
       $('#m').val(tempM);

});
});



Now you can access the hidden fields m and counter from anywhere in the view using jquery.

Code:
$('#counter').val();
$('#m').val();
And if you need to send m and counter to the server, call the $.ajax method of jquery and pass the values
of the hidden fields m and counter to the server as parameters in the ajax call.
#5

[eluser]mehwish[/eluser]
but how will i update the hidden text fileds of id "m" and "counter" ? and what is the use of simple this statement:
Code:
$('#counter').val(tempCounter++);
and thanks alot for help Smile
#6

[eluser]mehwish[/eluser]
ohh yeah yeah i think i got from this simple statement hidden fields' id is being updated .. right ?
#7

[eluser]Amitabh Roy[/eluser]
yep, it can be expanded like this


Code:
var tempCounter = $('#counter').val();
  tempCounter = tempCounter + 1;
  $('#counter').val(tempCounter);

it updates the value of the counter field and

Code:
$('#m').val(tempM);

updates the value of the m field
#8

[eluser]mehwish[/eluser]
but one thing more i am updating the hidden fields but there is one other text bar in which i have to take input from user so how do i save that value ? as shown in my code that when a user clicks a button (with id="addButton") that text field is generated with default value i have set but i want user to enter his own value and i need to save that user's value not the one set by me .. how can i do this? Hope you understood what i have said
#9

[eluser]Amitabh Roy[/eluser]
I tried to figure out what you meant , but am not sure I understood you fully, so if you can elaborate more, it would be great.

What I understood is , when one clicks the add button a textfield is generated with a given value.

Code:
$(this).before('&lt;input type="textbox" name="textbox' + tempCounter + '" id="textbox' + tempCounter + '" value="textbox'+  tempCounter + '" /&gt;&lt;br/>')

and you need to correct the html a bit, the input type of textfield is text , so it should be


Code:
$(this).before('&lt;input type="text" name="textbox' + tempCounter + '" id="textbox' + tempCounter + '" value="textbox'+ tempCounter + '" /&gt;&lt;br/>')

Now you can set the value blank when you generate the textfield


Code:
$(this).before('&lt;input type="text" name="textbox' + tempCounter + '" id="textbox' + tempCounter + '" value="" /&gt;&lt;br/>')

User can fill up their own value here, and to save it , you will need to either submit the form via POST or make an ajax POST call to the server and pass the data so that it can be handled by a CI controller at the server end.
#10

[eluser]mehwish[/eluser]
all i have done thans alot for your help Smile one thing more actually i have a value which is generated inside a for loop consider this:

Code:
[removed]
for(i=1; i<=tempCounter1; i++){
        msg= "\n Textbox #" +i+":"+$('#textbox'+i).val();   // this is better
                $('#msg').val(msg);

[removed]

as you can see "msg" values are being updated after every cycle of for loop and using
Code:
$('#msg').val(msg);
i am also updating the hidden field with id "msg". but when a time comes to pass this hideen field value to controller , the last updated value in passed which is obvious.

how to pass the all values to controller ... can I directly pass the "msg" value to database from inside the for loop like is:



Code:
[removed]
for(i=1; i<=tempCounter1; i++){
        msg= "\n Textbox #" +i+":"+$('#textbox'+i).val();   // this is better
                $('#msg').val(msg);
                 DATABSE CODE (insert msg value to table)    

}

[removed]

Or is there any other way to accomplish this?




Theme © iAndrew 2016 - Forum software by © MyBB