【问题标题】:Pass variable to PHP with AJAX使用 AJAX 将变量传递给 PHP
【发布时间】:2015-12-21 13:35:37
【问题描述】:

我正在尝试通过 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;
}

这是包含包含表格 html 的视图的同一个文件。我的控制器都是这样的结构:

    // 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;

视图是 php 文件,在 return 语句中带有 html。 我的索引结构如下:

    //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 控制器?

【问题讨论】:

  • 在控制台中检查请求的状态,看看是否有错误。
  • 我在控制台或 PHP 错误日志中都没有收到任何错误。如果我将 URL 更改为 'controllers/booking.php' 或类似的东西,我会得到 404,但即使 'index.php?page=booking' 没有给出错误,控制器也不会注册 Ajax称呼。我不认为这是语法问题,因为我在其他项目中使用相同的代码没有问题,但可能由于结构不同,我需要做不同的事情?
  • 你试过在你的控制器中做一个print_r($_POST)吗?
  • 它返回一个空数组。大批 ()。所以它没有通过。在控制台中,我可以看到该值正在被抓取,但由于某种原因它没有通过。在 Ajax 调用中尝试“常规”URL 结构会产生 404,但此 URL 也没有达到目标。还有其他方法可以从我不知道的表中获取值吗?
  • 另外,说真的,停止使用“面向包含的编程”

标签: php jquery


【解决方案1】:

我认为您需要将数据变量放在引号中:

data: {'time': $time},

【讨论】:

  • 您说的非常对,先生。尝试事情时,我身边的马虎。问题仍然存在。 if(isset($_POST['time']) 永远不会触发...
  • 你可以试试下面的,看看实际设置了什么:print_r($_REQUEST)
猜你喜欢
  • 2014-04-23
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多