title: Call a php function in MVC using AJAX?
body: <p>I am currently migrating an already built web application to MVC, and I'm figuring out that I'm too newbie to do some kind of changes.  There are some ajax calls that are freaking me out.  I'll try to be as clear as possible, but due to my inexperience I'm not sure if I won't let some important information by the way.</p>
  
  <p>The point is in the old application, things go this way:</p>
  
  <p>In the php code:</p>
  
  <pre><code>    if ($action_user == 'show_alerts') {
          $list = array();
          $query = "SELECT alert_type FROM alert_contact WHERE NOT 
          deleted AND user_email=" . typeFormat($email);
          $result = mysqli_query($db, $query) or die('Error in query "'.$query . '": ' . mysqli_error($db));
          while ($db_field = mysqli_fetch_assoc($result)) {
          $list[] = $db_field['alert_type'];
          }
          echo json_encode($list);
  </code></pre>
  
  <p>In the jquery code:</p>
  
  <pre><code>    $.ajax({
              type: 'POST',
              url: 'userpost.php',
              data: $('#userForm').serialize(),
              cache: false,
              dataType: 'json'
  </code></pre>
  
  <p>Here comes my problem, and since I don't have an userpost.php file anymore, I have to send it to the index.php and call my users component by a get petition, which I don't like, but I coudn't find another way to do it. And, what is even worse, I don't know at all how ajax is getting the variables that it needs.  It must be a pretty basic mistake, but I recognize my skills at this point are't so good. That's what I'm doing in my version:</p>
  
  <p>In the php code:</p>
  
  <pre><code>    if ($action_user == 'show_alerts') {
              $list = ModelUser::getAlertContact($act_email);
              echo json_encode($list);//I predict that ajax don't reach this line, but not sure
      }
  </code></pre>
  
  <p>In the jquery code:</p>
  
  <pre><code>$.ajax({
          type: 'POST',
          url: 'index.php?option=users',
          data: $('#userForm').serialize(),
          cache: false,
          dataType: 'json',
      success: function(data) {
          alert ('gotcha');
          $.each(alertsarray, function(index, value) {
              if ($.inArray(value, data) === -1) {
                  $("#sub" + value).prop("checked", false);
                  $('#alert' + value).removeClass("list_alert_sub");
              }
              else {
                  $("#sub" + value).prop("checked", true);
                  $('#alert' + value).addClass("list_alert_sub");
              }
          });
      },
      error: function(data) {
          alert("¡Error (ajax)!");
      }
  });
  </code></pre>
  
  <p>Any help would be appreciated, and if there's some more information I've missed, please let me know. Thanks in advance.</p>
  
  <p>UPDATE:</p>
  
  <p>I've been making some progress but don't seem to find a real solution.  Now I know that the url has to be the controller, so I'm using 'components/userpost/controller.php' as it, and it reaches the ajax call, cause the success alert is showing up. The problem is the MVC way, because I send ajax to the controller, but since I don't have a reload in the page, all the includes are failing so they are obviously not being loaded, and I'm getting errors like this:</p>
  
  <blockquote>
    <p>PHP Warning:  include(components/userpost/model.php): failed to open
    stream: No such file or directory in
    /var/www/html/viewer_mvc/components/userpost/controller.php on line 3,
    referer: <a href="http://localhost/viewer_mvc/index.php" rel="nofollow">http://localhost/viewer_mvc/index.php</a></p>
  </blockquote>
  
  <p>Really hope you guys can show me where am I failing, and if there's a special way to do these thing in MVC.</p>