Good day.

Faced a problem, but not quite understand why this is happening and how to get around.

There are 2 forms on the page, in each form there are 2 fields and 1 button.

<form action="" name="frm1" method="post"> <input type="text" name="fn" id="fn"/> <input type="text" name="ln" id="ln"/> <input type="submit" name='submitlogin1'/> </form> 

and

 <form action="" name="frm2" method="post"> <input type="text" name="fn" id="fn"/> <input type="text" name="ln" id="ln"/> <input type="submit" name='submitlogin2'/> </form> 

Next is the handler:

 if(isset($_POST['submitlogin1'])){ $name = $_POST["fn"]; header("Location: /list.php?name=$name"); } 

and

 if(isset($_POST['submitlogin2'])){ $name = $_POST["ln"]; header("Location: /list.php?name=$name"); } 

Conditions are processed - but no forwarding occurs.

If I remove 1 form from page and leave 1 handler, everything works, redirection happens.

Why it does not want to work with two forms and how to get around this? Thank!

  • And after header("Location:... also exit(); you need to do it. - Visman
  • @Visman yes, but alas, it will not make the code work (checked) - Vladimir V.

1 answer 1

I will assume that one of these PHP handlers is located after the HTML code and, as a result, the header does not work, but all because

Remember that the header () function can only be called if data has not yet been transferred to the client. That is, it should go first in the output; there should be no HTML tags, empty lines, etc. before calling it. Quite often, an error occurs when reading code through file functions like include or require, in this code there are gaps or blank lines that are output before the header () call. The same problems can occur when using a single PHP / HTML file.

 <html> <?php /* Этот пример приведет к ошибке. Обратите внимание * на тэг вверху, который будет выведен до вызова header() */ header('Location: http://www.example.com/'); exit; ?> 

quote from the documentation: http://php.net/manual/ru/function.header.php


so the code should be at the top

  • I do not agree. When there is one form on a page, it doesn't matter how much text is in front of header (), it will still work out at the end and pass the $ _GET request - Vladimir V.
  • one
    @ VladimirV. Seriously? Well, you need to give a nobel for breaking the system - Alexey Shimansky
  • It would be nice, but not the problem. I removed everything before the header, but the redirection still does not happen - Vladimir V.
  • @ VladimirV. OK. let's go 1) at the very beginning of the file write <?php error_reporting(E_ALL); ?> <?php error_reporting(E_ALL); ?> and see what errors it produces .. 2) show the whole page completely as it is now with all the extra garbage removed before the header (add to the question for example) 3) the page should be utf-8 encoded without BOM - Alexey Shimansky