-3

I am trying to pass a value from an html table to a PHP controller through Ajax. I think the problem lies in my MVC structure, but not sure. I have previously used Ajax with no problems within a different structure, so I am pretty puzzled as to where the problem is exactly.

I am getting the value from the HTML table in my .js file like this and trying to pass it through ajax:

$(".courtBook").click(function() {
    var $row = $(this).closest("tr");    // Find the row
    var $time = $row.find(".courtTime").text(); // Find the text

    console.log($time);

    // var sLink = "index.php?page=booking&time="+$time;

    // $.get(sLink, function(jData){
    //  alert($time);
    // });

    //$.post( "index.php?page=booking", { time: $time } );

    $.ajax({
        url: 'index.php?page=booking', //This is the current doc
        type: 'POST',
        data: {time: $time},
        success: function(){
           alert('Much success, such wow');
        },
        error: function(){
           alert('Much wrong, such sad');
        }
    });
});

It grabs the right value, so that part is right. As you can see I have tried a couple of things. For now I am just trying to echo the value in the PHP to make sure the time variable is passed correctly, but nothing is showing. The php code looks like this:

if(isset($_POST['time'])){
    echo "So far, so good";
    $time = $_POST['time'];
    echo $time;
}

This is the same file which include the view containing the html of the table. My controllers are all structured like this:

    // including the model with the code for the database
    include_once 'models/Booking_Table.class.php';

    /* Passing information to and from the view */
    // Include the view depending on the results from the database and return the view
    $view = include_once 'views/booking-html.php';
    return $view;

The views are php files with html in a return statement. My index is structured like this:

    //load page model class definition
    include_once "models/Page_Data.class.php";

    //database connection
    include "models/dbcon.php";

    //create a new object
    $pageData = new Page_Data();

    //change default content - include navigation
    $pageData->content = include_once"views/navigation.php";

     //main navigation
    $navIsClicked = isset( $_GET['page'] );
    if ( $navIsClicked ) {
      $controller = $_GET['page'];
      } else {
        //default controller
           $controller = "home";
      }

    $pageData->content .= include_once "controllers/$controller.php"; 

    $pageData->content .= include_once "views/footer.php";

    //load the view
    $pageAsHTML = include_once "views/page.php";

    echo $pageAsHTML;

The reason I am including this is because I think the problem lies in the URL in the Ajax call. I have HTML forms on other pages that work just fine with the URL structure in the .js. For some reason it doesn't seem to be hitting the page in the Ajax.

Scripts, controllers, views etc. are all in their own folder in the root directory.

Can anyone tell me where I am going wrong or provide an alternative solution for passing a value from an HTML table to a PHP controller?

tereško
  • 58,060
  • 25
  • 98
  • 150
MaDaHoPe
  • 133
  • 10
  • Check the state of the request in the console to see if you're getting an error. – Rory McCrossan Dec 21 '15 at 13:38
  • I get no errors in neither the console or the PHP error log. If I change the URL to to 'controllers/booking.php' or something like that I do get a 404, but even though 'index.php?page=booking' doesn't give an error the controller doesn't register the Ajax call. I don't think it's a syntax problem as I have used the same code in other projects with no problems, but maybe I need to do it differently because of the different structure? – MaDaHoPe Dec 21 '15 at 14:01
  • have you tried doing a `print_r($_POST)` in your controller? – bIgBoY Dec 21 '15 at 14:05
  • It returns an empty array. Array (). So it is not getting passed. In the console I can see the value is being grabbed, but for some reason it is not passed. Trying a "regular" URL structure in the Ajax call gives a 404, but this URL is not hitting the target either. Is there another way to get the value from table that I am just not aware of? – MaDaHoPe Dec 21 '15 at 14:13
  • you told jquery to expect json back, but look like you're passing back html. that's never going to work. if jquery expects json, then the **ONLY** output permitted from the script is json. anything else will be a parse error. – Marc B Dec 21 '15 at 15:41
  • My bad, sorry. I changed it ( see the edited code ) and it alerts the success message. However, the $time variable still never gets to the php file. – MaDaHoPe Dec 21 '15 at 15:50
  • Please provide the context for where `$_POST['time']` is used. Those 5 lines including `echo "So far, so good";` do not seem to be related to the rest of the code. Where are you actually trying to access it? – tereško Jan 02 '16 at 08:10
  • 2
    Also, seriously, stop using **"include oriented programming"**. – tereško Jan 02 '16 at 08:11
  • @tereško Why? Not a very helpful comment when there are no arguments for saying this. I have never come across any comments or articles saying it's a bad idea and been taught this by 4 different teachers, so please elaborate. If I "seriously" need to stop doing this, please let me know why. – MaDaHoPe Jan 02 '16 at 09:53
  • @tereško Oh, and I did get the problem solved. I will provide the solution as an answer as soon as I have the time and my computer fixed. As I recall I solved it by telling the ajax call to expect JSON back and echo JSON encoded data in the PHP. And, of course, exit; after echo. Thank you for your interest. – MaDaHoPe Jan 02 '16 at 09:57
  • @MaDaHoPe that makes me question the competency of your 4 "teachers". The term "include oriented programming" is a tongue in cheek description of [non-structured programming](https://en.wikipedia.org/wiki/Non-structured_programming) style that you showed in your code example. The major drawback would be complete lack of variable scope, [spaghetti](https://en.wikipedia.org/wiki/Spaghetti_code) level of execution path and difficulty to debug. It's how people write when PHP4 was the *new thing* (you are most likely to see examples of this style in legacy codebases). – tereško Jan 02 '16 at 11:21
  • @tereško Seems like you know what you are talking about. I always been taught MVC like this: The model handles the queries and connection to the database, controllers are the connection between the view and the model (controlling the interaction) and the view, well, displays the results. Is this correct and just the way of doing it which is wrong or did I totally miss the point? I still pretty new to programming and I don't want to start off on the wrong path, so if there is a better way to do it, I would appreciate it a link or what to google :) – MaDaHoPe Jan 02 '16 at 12:14
  • @tereško I believe there is a better way of structuring this, so no point in getting bad habits, might as well get it right as soon as possible. Thank you for the heads up and your time. – MaDaHoPe Jan 02 '16 at 12:16
  • IMHO, you are just going at it the wrong way. You first need to get solid grasp on OOP, various best practices. Then learn how to do routing and autoloading. And only then start tackling MVC. You might find [this list](http://stackoverflow.com/a/16356866/727208) useful in your studies. As for explaining various parts of MVC, I will just link you to relevant post (because I am a lazy asshole): [controller](http://stackoverflow.com/a/25709651/727208), [model](http://stackoverflow.com/a/5864000/727208), [view](http://stackoverflow.com/a/16596704/727208). – tereško Jan 02 '16 at 12:42
  • @tereško I'm fine with being lazy, thanks for the links, much appreciated! – MaDaHoPe Jan 02 '16 at 13:17

1 Answers1

0

I think you need to your data variable(s) in quotation marks:

data: {'time': $time},
Harvinder
  • 21
  • 1
  • 7
  • You are very right sir. Sloppiness from my side when trying things out. The problem persists though. The if(isset($_POST['time']) is never triggered... – MaDaHoPe Dec 21 '15 at 15:55
  • can you try the following and see what is actually set: `print_r($_REQUEST)` – Harvinder Dec 22 '15 at 10:04