I have a value price in a span in my view file and i want to pass the value of that span to my controller without submitting a form, is there a way to do this?
-
4Short answer: No. CI is one-directional from controller to viewdanneth– danneth2011-10-04 12:43:37 +00:00Commented Oct 4, 2011 at 12:43
-
This begs the question: "how are you getting that value into your view?" - You're probably not using MVC the way it is designed. You should have all data in (or accessible from) your model, pass that data to your controller (to calculate, format, etc.), then pass the final variables to your view for display. I can't really see why you would have a value in your view without it first passing through your controller.swatkins– swatkins2011-10-04 14:12:02 +00:00Commented Oct 4, 2011 at 14:12
-
The value could only be from the view, it's a span field that displays the current x y offest of a box using javascript, how would you do that from a controller lol btw values can be passed from view to controller using a form and post method but this time i'm not using a form..user951042– user9510422011-10-05 11:21:16 +00:00Commented Oct 5, 2011 at 11:21
Add a comment
|
1 Answer
Yes you can use something like AJAX. However you are thinking about this all wrong, you are not passing from a VIEW => CONTROLLER, you are still doing user submission, and just managing that input.
So you would do a controller LIKE a standard form submit, you could use jQuery to get you there by using its .ajax() function:
$.ajax({
type: "POST",
url: "/mycontroller/function/",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
Reference: http://docs.jquery.com/API/1.1/AJAX
3 Comments
Jakub
@TommyD.Adey, you are still confused. Think of this as a FORM being submitted, except as you request, without a page refresh. The
$.ajax fn is used to seamlessly pass that data (POST) to your backend (controller). In the controller you would catch/validate these values as if it was a standard $_POST['name'] or $_POST['location'] (as in the example I used). So to end it, your view would house the Javascript ajax code, and your controller class (make a new one) would catch the submission. The success location handles your response to the user (notify on success or failure).Ramaraju.d
I am sending $start value from view to controller but the error is undefined index start for $start = $_POST['start'];