简体   繁体   English

运行单独的php文件时如何访问我的URL中的$ _GET变量(不使用Session变量)

[英]How to access the $_GET variables in my URL when running a separate php file (without using Session variables)

I am submitting my forms using ajax to a php file that processes the form data on the server. 我正在使用ajax将表单提交到处理服务器上表单数据的php文件。 However I need to access the variables in the URL using $_GET but I'm getting the "Undefined index:" error message. 但是,我需要使用$ _GET访问URL中的变量,但收到“未定义索引:”错误消息。 I think it's because the current URL is not included (or viewed) in the php file because I submitted it separately with AJAX. 我认为这是因为当前URL未包含在php文件中(或未查看),因为我是与AJAX分开提交的。 I would prefer not to use session variables in this case. 在这种情况下,我不希望使用会话变量。 Any help is appreciated. 任何帮助表示赞赏。

URL ../completeAccount.php?acctnum=433462524&keyCode=bdtF5ziKWJ 网址 ../completeAccount.php?acctnum=433462524&keyCode=bdtF5ziKWJ

Javascript Java脚本

formData = $(formID).serializeArray();
$.ajax({
    url  : "process.php",
        type: 'POST',
        data : formData,
        success: function(resp) {
            //success code
        }
});

process.php process.php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$newpass = $_POST['newpass'];
$newpass2 = $_POST['newpass2'];

$acctnum = $_GET['acctnum'];
$keyCode = $_GET['keyCode'];

//use variables to process data etc.

You should get the parameters with your javascript and append them into formData . 您应该使用JavaScript获取参数,并将其附加到formData The code that would grab a URL parameter might look like this: 可以获取URL参数的代码如下所示:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Next, just append it into formData 接下来,只需将其附加到formData

formData = $(formID).serializeArray();
formData.push({name: 'acctnum', value: getParameterByName('acctnum')});

Note, that acctnum will then be in POST , not in GET ! 请注意, acctnum将位于POST ,而不位于GET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM