【问题标题】:PHP GET variable not being set未设置 PHP GET 变量
【发布时间】:2018-05-08 01:15:25
【问题描述】:

我有一个registraion php 类,它显示一个表单,当点击注册按钮时,调用登录javascript 文件中的一个函数。此文件使用ajax 将数据发布到 index.php 文件。我的index.php 文件无法访问此数据,尽管发布成功(在调用警报时,ajax 成功为真)。

Login.js

var loginData, urlPath;

// Allow users to log in or register
function Login() {

    loginData = "username=" + $("#usernameField").val() + "&email=" + $("#emailField").val() + "&password=" + $("#passwordField").val();
    urlPath = "../index.php?action=register";

    // Send the login/registration data to database
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: urlPath,
            data: loginData,
            success: function (result) {
                alert("success");
            }
        })
    })
}

index.php

<?php 

    require_once("Model/model.php");
    require_once("Controller/controller.php");
    require_once("View/view.php");

    $model = new Model();
    $view = new View();
    $controller = new Controller($model, $view);

    $controller->Begin();

    // Client wants to register
    if(isset($_GET['action'])) {
        if($_GET['action'] == "register") {
            echo '<script type="text/javascript">alert("hello")</script>';
        }
    }
?>

【问题讨论】:

  • 您在 Ajax 中使用 post 方法,但在 php 中接收为 get 方法尝试更改一个
  • 使用 GET 的原因是从 url 获取操作 ../index.php?action=register 我使用 post 获取稍后需要的帖子数据
  • 所以当你把 print_r($_POST);就在脚本的开头,你得到了什么?使用 Chrome 的“检查元素 -> 网络”来检查 ajax 请求。此外,您也可以将您的操作在 ajax 中发布。
  • 我检查了网络中的ajax请求,它确实包含“action = registration”数据

标签: javascript php jquery ajax html


【解决方案1】:

您使用了 ajax 的 POST 方法。所以也以 POST 方式发送数据,如下所示:-

// Send the login/registration data to database
$(document).ready(function() {
    var username = $("#usernameField").val();
    var email = $("#emailField").val();
    var password = $("#passwordField").val();
    $.ajax({
        type: "POST",
        url: "../index.php",
        data: {"username":username,"email":email,"password":password,"action":"register"},
        success: function (result) { 
            alert(result);//check the change 
        }
    });
});

然后在php结束时将GET改为POST:-

<?php 

    require_once("Model/model.php");
    require_once("Controller/controller.php");
    require_once("View/view.php");

    $model = new Model();
    $view = new View();
    $controller = new Controller($model, $view);

    $controller->Begin();

    // Client wants to register
    //single condition can do the job and use POST instead of GET
    if(isset($_POST['action']) && $_POST['action'] == "register" ) { 
      echo "hello"; //check the change
    }
?>

注意:- 请注意 cmets。(添加在代码中)

【讨论】:

  • @Jake 很高兴帮助你,伙计:):)
【解决方案2】:
loginData = "username=" + $("#usernameField").val() + "&email=" + $("#emailField").val() + "&password=" + $("#passwordField").val() + "&action=" + "register";
urlPath = "../index.php";

$(document).ready(function() {
        $.ajax({
            type: "POST",
            url: urlPath,
            data: loginData,
            success: function (result) {
                alert("success");
            }
        })
    });

尝试在帖子数据中添加操作并将其作为 $_POST 接收

if($_POST['action']) {
        if($_POST['action'] == "register") {
            echo '<script type="text/javascript">alert("hello")</script>';
        }
}

【讨论】:

  • 似乎不起作用,没有调用 hello 警报。
  • 移除 isset 并重试
  • 没有变化,除了一个 php 通知告诉我“未定义索引:操作”
  • 尝试打印 $_POST 值
  • 我试过但没有任何显示,我想知道你好没有被提醒的原因是因为在registration.php页面上发布我而不是index.php,这是警报被触发了?
猜你喜欢
  • 2012-04-11
  • 2019-07-25
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
相关资源
最近更新 更多