www.webdeveloper.com
Results 1 to 2 of 2

Thread: PHP MVC Ajax issue

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    8

    PHP MVC Ajax issue

    Hi all, wonder if anyone has a moment, trying to create a dynamic dropbox that is populated from the selection of an original dropbox using ajax ( i know this part is working because the secondary dropbox alters with no data as soon as the original "customerID" box is altered, this seems to be very problematic because my "bootstrapper" which inspects the URL for the MVC models/functions is picking up the ajax request and basically saying it doesnt exist, if anyone could point me in the rite direction that would be great.

    the contactSelect.php page that the ajax calls -

    PHP Code:
    <?php

        $customerID 
    $_POST['customerID'];
        
    $conn2 = new mysqli(DB_HOST,DB_USERDB_PASSDB_NAME)
                            or die (
    'Cannot connect to db');
        
    $result2 $conn2->query("SELECT * FROM contacts where CustomerID=".$customerID);
          while (
    $row $result2->fetch_assoc()) {

          
    ?>
    <option value="<?php echo $row['ContactID']; ?>"><?php echo $row['ContactName']; ?></option>
    <?php
        
    }

    ?>
    the ajax request that index.tpl calls when the customerID dropbox selection is altered

    Code:
      <script type="text/javascript">
                                    $(document).ready(function(){
                                    $("#customerID").change(function(){
                                     var customerID = $("#customerID").val();
                                     $.ajax({
                                        type:"post",
                                        url:"contactSelect.php",
                                        data:"customerID="+customerID,
                                        success: function(data) {
                                          $("#contactDrop").html(data);
                                        }
                                     });
                                });
                           });
                            </script>
    The original selection drop down also in index.tpl

    PHP Code:
          <select name="customerID" id="customerID" >
                            <?php
                            $conn 
    = new mysqli(DB_HOST,DB_USERDB_PASSDB_NAME)
                            or die (
    'Cannot connect to db');
                            
    $result $conn->query("SELECT * FROM customer");
                               
                            while (
    $row $result->fetch_assoc()) {

                            echo 
    '<option value="'.$row['customerID'].'">' $row['customerName'] . '</option>';
                             }

                            
    ?>
                            </select>
    The bootstrapper page which controlls the MVC flow

    PHP Code:
    <?php
    $controller 
    "contact";
    $action "index";
    $query null;

    if (isset(
    $_GET['load']))
    {
        
    $params = array();
        
    $params explode("/"$_GET['load']);

        
    $controller ucwords($params[0]);
        
        if (isset(
    $params[1]) && !empty($params[1]))
        {
            
    $action $params[1];
        }
        
        if (isset(
    $params[2]) && !empty($params[2]))
        {
            
    $query $params[2];
        }
    }

    $modelName $controller;
    $controller .= 'Controller';
    $load = new $controller($modelName$action);

    if(!empty(
    $_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
       
    //some sort of direct call to the php page should be created here because its an ajax request  }

    else
    {

    if (
    method_exists($load$action))
    {
        
    $load->{$action}($query);
    }
    else 
    {
        
    //die('Invalid method. Please check the URL. Action is: '.$action.' and query is '.$query);
           
    $load->{$action}($query);
    }}
    IE9 dev tools states that: <br />
    <b>Fatal error</b>: Call to undefined method ticketcontroller::contactSelect.php() in <b>C:\inetpub\mvc\utilities\bootstrap.php</b> on line <b>39</b><br />

    so my understanding is that the MVC is trying to pickup the ajax request, i put it a condition which checks if its an ajax but I dont know how best to handle the action, sorry this is so long winded, thanks alot in advance.

  2. #2
    Join Date
    Oct 2012
    Posts
    8
    resolved all, basically rather than mess around trying to exclude ajax calls from the mvc logic just included the method in the appropriate controller as below

    public function getContact()
    {
    $customerID = $_POST['customerID'];

    $conn = new mysqli(DB_HOST,DB_USER, DB_PASS, DB_NAME)
    or die ('Cannot connect to db');

    $result = $conn->query("SELECT * FROM contact where CustomerID=".$customerID);
    while ($row = $result->fetch_assoc()) {

    ?>
    <option value="<?php echo $row['ContactID']; ?>"><?php echo $row['ContactName']; ?></option>
    <?php
    }

    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles