为什么这个$ _GET在GET提交表单后为空,为什么是"?" 附加到表单提交的URL

Shy*_*Shy 1 html javascript php forms jquery

这是SSCCE展示我的问题.在页面顶部,$_GET打印出来.首次加载页面时,可以理解Array()将打印一个空数组.

但是formaction属性有一个空值,这意味着在页面提交到自身.价值methodGET.所以表格由GET提交.

因此,当表单通过GET提交给页面本身时,现在$_GET打印在页面顶部的数组不能为空.哦,但是!这就是问题所在.

提交表单时

  1. $_GET打印在页面顶部的数组为空.Array ()

  2. A ?附加到URL.例如,在表单提交之前,页面的URL是http://localhost/Test/index.php.提交表单后,页面重新加载并且URL变为http://localhost/Test/index.php?

问题是为什么?以及如何解决这个问题.

<?php 
print_r($_GET);//check
?>

<!DOCTYPE html>

<html>

<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/css/materialize.min.css"/>
    <script src="<script
  src="http://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/js/materialize.min.js"></script>
</head>

<body>
    <form action="" method="get">

        <div class="input-field">
            <input type="text" id="nameInput" class="validate" />
            <label for="nameInput">Name: </label>
        </div>


        <button type="submit" class="btn waves-light waves-effect">Submit Form</button>

    </form>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Ror*_*san 5

打印在页面顶部的$ _GET数组为空.

这是因为您需要提供一个name属性,input以便将其添加到请求中发送的键/值对数据中:

<input type="text" id="nameInput" class="validate" name="foo" />
Run Code Online (Sandbox Code Playgroud)

A ?附加到URL.例如,在表单提交之前,页面的URL是http://localhost/Test/index.php.提交表单后,页面重新加载并且URL变为http://localhost/Test/index.php?

那是因为这就是GET请求的工作方式; 信息作为application/x-www-form-urlencoded数据附加到URL .

一旦发送name添加了属性的请求input,URL将变为:

http://localhost/Test/index.php?foo=[value]
Run Code Online (Sandbox Code Playgroud)