5

I´m building a small application with Jquery and PHP. Jquery (index.html) adds Form Fields for the User and sends those to a PHP Script (pdo.php). The PHP Script fetches Values from the Database and does some calculations with the User given Values and the Values from the DB. The sum is returned to the Form Page.

index.html <-> pdo.php

Thus I am trying to understand the PHP MVC pattern my question is if

a.) this would make sense in this case.

b.) if so, which part would be what. index.html --> view; pdo.php --> model; controller --> ?

thanks for your help,

tony

Cut-out

jquery ... index.html

$(document).ready(function(){
    $("#buttonAjax").click(function(){
        var name = encodeURI($("#name").val());

            $.ajax({
                type: "POST",
                url: "pdo.php",
                data: "name="+name,
                success: function(data){
                    var json = $.parseJSON(data);
                    $("#output").html(json.summe);
                    talk(json.say);
                }
            });

    });

    function talk (say){
        jQuery.noticeAdd({text: say,stay: false});
    }

     });

pdo.php

/* DB Connection */
    $strDbLocation = 'mysql:dbname=test;host=localhost';
    $strDbUser = 'root';
    $strDbPassword = 'root';

    try{
        $objDb = new PDO($strDbLocation, $strDbUser, $strDbPassword);
    }
    catch (PDOException $e){
        echo 'Failure: ' . $e->getMessage();
    }


/* Fetch POST Data */
    $id = $_POST['name'];


/* Build query */
    $dbSelect =  $objDb->prepare("SELECT Age,Name FROM Benutzer WHERE id = :id");
    $dbSelect -> setFetchMode(PDO::FETCH_ASSOC);  
    $dbSelect -> bindParam('id', $id);
    $dbSelect -> execute();


/* Output + Calculate */    
    while($row = $dbSelect->fetch()) {  
        $total =  $row['Age'] / 100 . "<br />";
    }  


/* Return to User */
    if(!empty($total)){ 
        $ret = Array("summe" => "Summe: " . $total, "say" => "all right");
        echo json_encode($ret); }
    else{
        $ret = Array("summe" => "Nothing for you",  "say" => "nothing for you");
        echo json_encode($ret); 
    }
Anatol
  • 1,923
  • 6
  • 26
  • 55
  • http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller – PeeHaa Jul 03 '12 at 07:25
  • 2
    http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000 – PeeHaa Jul 03 '12 at 07:25
  • P.S. You should look into real prepared statements instead of emulated ones. [Best way to prevent SQL Injection](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php). – PeeHaa Jul 03 '12 at 07:28
  • PeeHaa, thanks for the injection tip. This was new to me. – Anatol Jul 03 '12 at 09:35

2 Answers2

3

In a standard MVC webapp, routes are matched to controller actions. These controller actions may interface with the Model [which in turn interfaced with the database] or performs some other Model-agnostic calculations, and renders a view. It is obvious that the index.html is the view. And I think you have the controller and the model bundled up in pdo.php.

I really recommend PeeHaa's link in the comments. That answer is well written.

That been said, there are many architectural patterns to making a webapp. Yours may not be MVC. Some prominent frameworks that aren't MVC are SproutCore (View-based controllers) and JSP pages (single controller).

Igbanam
  • 5,904
  • 5
  • 44
  • 68
  • 1
    @tschonbuchner: Also note that even though most frameworks state they are doing the MVC pattern. They are in reality doing MVCi ™ (MVC inspired) patterns. – PeeHaa Jul 03 '12 at 07:56
0

Use of controller is to controll the form elements in html using php . For eg:

/ * Fetch POST Data */
    $id = $_POST['name'];

the use of model is only for db use (insert,select,..)

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • Hi together thank you for your reply. I think the part that confused me is the combination of MVC and ajax use and the question if mvc makes sense in this case. but as Yasky writes there are more architectural patterns. I will read the article maybe I find an better fitting one . – – Anatol Jul 03 '12 at 10:10