D
D
DvorkinFromChaos2021-11-18 16:53:18
PHP
DvorkinFromChaos, 2021-11-18 16:53:18

Difficulty when working with forms - why are parameters passed via GET lost?

The bottom line is this - I need to do this with the help of a GET request

<html>
<head>
<meta charset="utf-8">
</head>
<body>
<br/>
<form action="" method="GET">
  Имя: <input type="text" name="username"><br><br>
  <button name="submit">Отправить</button><br><br>	
</form>
</body>
</html>

The form is followed by this code.
<?php
  if (isset($_GET['submit'])) {
    echo "Получили GET запрос от " .$_GET['username'];
  }
  else{
    echo "Введите своё имя <br>";
  }
?>

The problem is that the parameter is lost. And the first if is not executed. At the same time, it throws me to index.php (I work with Openserver)
What could be the problem? I will be glad for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2021-11-18
@DvorkinFromChaos

And so?

if (array_key_exists('submit', $_GET)) {
...

G
grek_cheburek, 2021-11-18
@grek_cheburek

You need to add type='submit' to the button
Remove action="" Or enter the page to which the submission is being made.
I advise you to put exit(); before the first if and look in the address bar to see if there are get parameters there. If so, then the problem is in your script. Never trust an empty name in a button. You should have at least that. name=submit' value='ok' Then the first if will work. The isset function checks whether the variable is empty, including the get parameter you specified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question