-3

I have a search form and if I use it like this:

<form  method="post" action="search.php?go"> 
<input  type="text" name="name"> 
<input  type="submit" name="search" value="Search">
</form>

It works.

If I try to use it like this, then it doesn't:

<?php
    if ($_POST['submit'] == 'search') {
        header("Location:search.php?go");

      }

    ?>

    <!DOCTYPE html>
    <html>
        <head>
    </head>
    <body>

    <form  method="post" action=""> 
        <input  type="text" name="name"> 
        <input  type="submit" name="search" value="Search">
        </form>

I need two forms (the search and a dropdown menu) on my site and if I use simple "action" with the pages to call, then it doesn't work, so I try to do it with PHP.

I find only this "Header:location" method on the net, but it doesn't work for me.

EDIT: I solved this in another way without redirect, because I couldn't get it work even with session variables.

Hunnenkoenig
  • 193
  • 1
  • 2
  • 14
  • 1
    Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Epodax Aug 03 '16 at 07:38
  • The important thing is that header location should be placed **at the beginning** of your file (before HTML). – Martin Aug 03 '16 at 07:39
  • I don't get a header already sent message, so no. – Hunnenkoenig Aug 03 '16 at 07:39
  • @forexking: I have it at the top. I have put that under the form here for better understanding. You don't really believe that this is all my code, do you? ;-P – Hunnenkoenig Aug 03 '16 at 07:40
  • 1
    Redirects lose their POST data. You'd need to store it in the session then grab the posted data on the other side through the session after the redirect completes. We do it all the time with the PRG (Post/Redirect/Get) design pattern: https://en.wikipedia.org/wiki/Post/Redirect/Get – Ultimater Aug 03 '16 at 07:42
  • 1
    @Hunnenkoenig it's a good idea to show us the real code that's not working, instead of something else. Otherwise, you will get many people coming to the wrong conclusions; which is what's happening now. – Ben Hillier Aug 03 '16 at 07:44
  • check my post.. it works – Hardik Aug 03 '16 at 07:48

3 Answers3

1
<?php
if (isset($_POST["search"])) 
{
    header("Location: search.php?go");
}
?>

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form  method="post" action=""> 
            <input  type="text" name="name"> 
            <input  type="submit" name="search" value="Search">
        </form>
    </body>
</html>

Kindly use this code. This works..

Hardik
  • 448
  • 2
  • 9
  • The problem is not putting the redirect at the top. I tried different methods. The problem is that it seems that the post data doesn't get stored when redirecting. – Hunnenkoenig Aug 03 '16 at 08:05
  • 2
    the problem is in your if condition. i tried with `if ($_POST['submit'] == 'search')` condition, that not working. – Hardik Aug 03 '16 at 10:12
0

Your form is wrong, it should be like this:

<form  method="post" action=""> 
  <input type="text" name="name"> 
  <input type="submit" name="submit" value="search">
</form>
Jade Kallo
  • 110
  • 1
  • 10
  • 1
    This is a valid answer as the OP's condition is `if ($_POST['submit'] == 'search')` when his HTML shows `value="Search"`, a difference in case. – Ultimater Aug 03 '16 at 07:49
  • You're right. I had seen it myself and deleted my comment just before you posted you posted yours. – Ben Hillier Aug 03 '16 at 07:50
-1

There are 2 things wrong here:

  1. You are outputting HTML before you send the header. That won't work. Once anything has been output, it's too late to redirect the browser.
  2. You are not embedding the PHP in <?php tags, so you would just see it output as text.

I would try this:

<?php 
    if ($_POST['submit'] == 'search') {
        header("Location:search.php?go");
        exit;
    }
?>
<form  method="post" action=""> 
    <input  type="text" name="name"> 
    <input  type="submit" name="search" value="Search">
</form>
Ben Hillier
  • 2,126
  • 1
  • 10
  • 15
  • Thanks, but that'S not it. In my real code it's all right, but I will edit my original post. – Hunnenkoenig Aug 03 '16 at 07:42
  • @Hunnenkoenig **Do not modify your question** if it invalidates existing answers. Instead, ask a new question. – Kyll Aug 03 '16 at 07:53
  • The if statement would remains false as `search` != `Search`. – Ultimater Aug 03 '16 at 07:57
  • @Ultimater. I know; but in the meantime 1) The OP had changed his question and 2) JadeKallo has answered it. – Ben Hillier Aug 03 '16 at 07:59
  • So edit your answer and include a 3rd point while fixing your own code. Your answer includes 2 other points his doesn't. – Ultimater Aug 03 '16 at 08:00
  • @Kyll I modified it, because that's what people tried to tell as a solution was not the problem. Before even more people coming and telling me to put it on the top, I rather edit it, because those answers were invalid in the first place. – Hunnenkoenig Aug 03 '16 at 08:01