2

I'm new in PHP MVC, I have a question about how javascript works with php mvc

If I have a page with a button, when user click the button It will send to next page and update data in database

$(document).ready(function(){
    $("#btn").click(function(){
        $.load(){...   }    
        or $.post(){.....}//post data to another page
    });
});

//next page
if(isset($_POST[])){
  //update data
}

My question is

Should I send this data to controller than pass to model and output in view(if we need respond something)

Button --javascript--> controller -> model(update data) --send data back--> view

or

I can just send data to page and update without mvc

5
  • checkout this cs.colorado.edu/~kena/classes/7818/f06/lectures/19/arch.png Commented Mar 15, 2016 at 12:19
  • you can use javascript to send data to an API (view in MVC )and it will return back the result. you can't communicate with controllers or models using the javascript. Commented Mar 15, 2016 at 12:21
  • so you mean i use second approach, send data to page and update it without mvc? Commented Mar 15, 2016 at 12:24
  • PHP MVC is not going to affect the javascript. Javascript is completely independent from your php code. if you are using php you can respond with a json data to javascript and update your client view. Commented Mar 15, 2016 at 12:32
  • you can create your HTML output in your php Controller after you have received your required data from repository->Model , and by using ob_start() you can buffer your output and then use ob_get_contents() to get full output in a variable so you can echo it . in the javascript you can take it and put in in document using .innerHTML of your output container Commented Feb 7, 2020 at 9:17

1 Answer 1

2

Sorry, i can't just comment your question yet

Your first approach is correct. Is recommended that you update data in models. Meanwhile, all SQL statements or ORM handles should be on it.

In your case, you have two options to show the data in view: Return a JSON in your php handle it with javascript, and load your view directly after update data. It depends how all your project is builded.

I can write some exemples, but you will need to give some peace of code.

// In your controller
if(isset($_POST)){

    $obj = new MyObject();
    $obj->name = $_POST['name'];
    $obj->date = date("Y-m-d");
    $obj->validatePost();
    $obj->update();

    $result = $obj->getData();
    return $result;

}


// Your model
class MyObject {

    public $name;
    public $date;

    public function validatePost(){
        if($this->name == null){
            // print error
        }
    }

    public function update(){
        // database cheets
    }

    public function getData(){
        return $json;
    }

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

6 Comments

so should I put update method in same page controller? when I send data, create a new object and use method update?
thx a lot! I got it now, but sometime I felt MVC make app lots complex
See my edit. I assume that your controller is already handling the post
I can tell for sure. But generali, js, css and images keeps on a "public" folder.
OK thx a lots!, I just did some testing, I put in app folder but it wont work if my htaccss deny from all, so keeps on public folder
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.