Shy
Shy

Reputation: 536

Why is this $_GET empty after form is submitted by GET, and why is a "?" appended to URL on form submission

Here is an SSCCE demonstrating my problem. At the top of the page, the $_GET is printed. When the page is first loaded, it is understandable that an empty array Array() will be printed.

But the form's action attribute has an empty value, which means the page is submitted to itself. The value of method is GET. So the form is submitted by GET.

So when the form is submitted to the page itself by GET, now the $_GET array printed at the top of the page must NOT be empty. Oh, but it is! And that is the question.

When the form is submitted,

  1. the $_GET array printed at the top of the page is empty. Array ()

  2. A ? is appended to the URL. For example, before the form submission, the URL of the page was http://localhost/Test/index.php. After the form is submitted, the page reloads and the URL becomes http://localhost/Test/index.php?

The question is why? and how do I fix this.

<?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>

Upvotes: 1

Views: 100

Answers (3)

Ilove112
Ilove112

Reputation: 94

Is there some kind of syntax error of your code?

<script src="<script
src="http://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>"></script>

Please check you syntax first.

Upvotes: -1

Adri
Adri

Reputation: 365

The $_GET is empty because your input have no name. The "?" is put by default when you send a form by GET : http://url.com?param1=value1&param2=value2&...

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

The $_GET array printed at the top of the page is empty.

This is because you need to provide a name attribute on the input for it to be added to the key/value pair data sent in the request:

<input type="text" id="nameInput" class="validate" name="foo" />

A ? is appended to the URL. For example, before the form submission, the URL of the page was http://localhost/Test/index.php. After the form is submitted, the page reloads and the URL becomes http://localhost/Test/index.php?

That's because that is how a GET request works; the information is appended to the URL as application/x-www-form-urlencoded data.

Once you send the request with the name attribute added to the input, the URL will become:

http://localhost/Test/index.php?foo=[value]

Upvotes: 5

Related Questions