1

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?

3
  • 4
    Short answer: No. CI is one-directional from controller to view Commented 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. Commented 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.. Commented Oct 5, 2011 at 11:21

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help,so if i have lets say a value of 10 in the span. and i want that data in the controller withouth submitting a form. ill do like you said here, but i don't really understand how the data will be passed, what variable will it be stored in the controller?
@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).
I am sending $start value from view to controller but the error is undefined index start for $start = $_POST['start'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.