0

I understand the code you use to generate the page would be put in the view section of the MVC pattern of CI.

I am trying to understand the logic flow of the function when 1. user provide input 2. input sent to database 3. add another DOM element to show the input.

For example, just like Facebook, when you leave a message on someone's status, your message will be 'added' to that chain of reply to the status.

So in the view.php, I would write such that when you try to reply, an Ajax call would be made to the controller.php, and routed to model.php (which would 'add' the new msg to the status in the DB), after this, where do I go from here?

William Sham
  • 12,849
  • 11
  • 50
  • 67
  • @usoban, yes I understand that part, I assume you would need both Javasccript and PHP to achieve this. But what's the logic flow. – William Sham Oct 02 '11 at 04:52

1 Answers1

1

At client side, when lets say 'add' button is pressed, message is sent back to the server via Ajax. Server processes the message, and returns result back to the client. Now you have two options:

  • Server responds only with success or failure. In case of failure, your JavaScript displays error message, in case of success, you append a message to the list (using values you sent to server)
  • Server responds with all data that should be appended (lets say message, author, date, ...). Server can returns such data as JSON, and since JSON=JavaScript Object Notation, JavaScript can easily parse it. Using that data, you construct new node and append it.

JSON response can look something like this:

{
 message: 'I like the new picture!',
 author : 'William',
 date : '2011-10-9' 
}

and you parse it with JavaScript. Reference this question for example of parsing: Parse JSON in JavaScript?

I'd suggest you to use the second option, since server side may do some message filtering/cleaning and final result displayed to user may not be the same as he entered it.

I'm not using CodeIgniter, but Zend, and it has some nice logic to change response type from HTML to JSON or XML. This question may help you a bit: codeigniter JSON

Community
  • 1
  • 1
usoban
  • 5,428
  • 28
  • 42
  • Thanks for the detail response. However, I understand this part you are explaining. What I wanna know is how this 'server responds' play into the MVC structure of Codeigniter or any server-side MVC implementation. Because MVC means you have the view.php part generates the view for you, but 'echo-ing' out the JSON and have Javascript handle the actual display of the response is a different thing – William Sham Oct 03 '11 at 15:12
  • Aha, so, then please update your question with a little more info about JS library you're using and part of DOM you'd like to append the new node to. – usoban Oct 04 '11 at 09:21