Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AJAX将变量传递给PHP_Php_Jquery - Fatal编程技术网

使用AJAX将变量传递给PHP

使用AJAX将变量传递给PHP,php,jquery,Php,Jquery,我试图通过Ajax将值从html表传递到PHP控制器。我认为问题在于我的MVC结构,但不确定。我以前使用过Ajax,在不同的结构中没有任何问题,所以我很困惑问题到底出在哪里 我从.js文件中的HTML表中获取值,如下所示,并尝试通过ajax传递该值: $(".courtBook").click(function() { var $row = $(this).closest("tr"); // Find the row var $time = $row.find(".cour

我试图通过Ajax将值从html表传递到PHP控制器。我认为问题在于我的MVC结构,但不确定。我以前使用过Ajax,在不同的结构中没有任何问题,所以我很困惑问题到底出在哪里

我从.js文件中的HTML表中获取值,如下所示,并尝试通过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');
        }
    });
});
它获取正确的值,因此该部分是正确的。正如你所看到的,我已经尝试了一些方法。 目前,我只是尝试在PHP中回显该值,以确保正确传递时间变量,但没有显示任何内容。php代码如下所示:

if(isset($_POST['time'])){
    echo "So far, so good";
    $time = $_POST['time'];
    echo $time;
}
    // 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;
这是同一个文件,其中包含包含表的html的视图。我的控制器结构如下:

if(isset($_POST['time'])){
    echo "So far, so good";
    $time = $_POST['time'];
    echo $time;
}
    // 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;
视图是返回语句中带有html的php文件。 我的索引结构如下:

    //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;
我之所以包括这一点,是因为我认为问题在于Ajax调用中的URL。我在其他页面上有HTML表单,它们与.js中的URL结构配合得很好。出于某种原因,它似乎没有在Ajax中触及页面

脚本、控制器、视图等都位于根目录中自己的文件夹中


有人能告诉我哪里出了问题,或者提供一种替代方案,将值从HTML表传递到PHP控制器吗?

我认为您需要将数据变量用引号括起来:

data: {'time': $time},

在控制台中检查请求的状态,看看是否有错误。我在控制台或PHP错误日志中都没有错误。如果我将URL更改为“controllers/booking.php”或类似的内容,我会得到404,但即使“index.php?page=booking”没有给出错误,控制器也不会注册Ajax调用。我不认为这是一个语法问题,因为我在其他项目中使用了相同的代码,没有任何问题,但可能因为结构不同,我需要做不同的事情?您是否尝试在控制器中执行print\u r$\u POST?它返回空数组。大堆所以它没有通过。在控制台中,我可以看到值被抓取,但由于某些原因,它没有被传递。在Ajax调用中尝试常规URL结构会得到404,但该URL也没有达到目标。还有其他方法可以从表中获取我不知道的值吗?另外,说真的,停止使用面向包含的编程。你说得很对,先生。在尝试时,我身边的马虎。但问题依然存在。ifisset$\u POST['time']从未被触发…您可以尝试以下操作并查看实际设置的内容:print\u r$\u REQUEST