gpt4 book ai didi

javascript - 未设置 PHP GET 变量

转载 作者:行者123 更新时间:2023-11-30 14:54:50 24 4
gpt4 key购买 nike

我有一个显示表单的 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 方法。所以也像下面这样以 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
}
?>

注意:- 请也注意注释。(添加到代码中)

关于javascript - 未设置 PHP GET 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47466219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com