Redirect to URL with PHP

I have a form at index.php that takes in user input, it the includes and sends the user input to another php file for processing.

Here is the code of index.php:

<?php
if(isset($_GET['q'])){
    include_once "form.php";
    exit(0);
}
?>
<!Doctype HTML>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Search</title>
    </head>
    <body>
        <form method="get">
         <input type="text" name="q" />
    </form>
    </body>
 </html>

When one submits the form it goes to http://mysite.com/?q=textUserEntered (if just the domain was visited before) or http://mysite.com/index.php?q=textUserEntered (if index.php is visited before)

How can I get it to go to http://mysite.com/form?q=textUserEntered or http://mysite.com/index.php/form?q=textUserEntered while still passing the form data to form.php

I tried this in the beginning index.php and form.php, it navigates to the URL but doesn’t pass the data to form.php and instead goes to a 404 error page.

if(!empty($_GET['q']))
{
    header("Location: form?q=".rawurlencode($_GET['q']));
    exit;
}

Update:

I can’t use the action attribute because adding form.php to the value of the action attribute would make the URL http://mysite.com/form.php?q=userEnteredText not http://mysite.com/form?q=userEnteredText

remove the exit() call from your if, and make your form’s Action point to form. (If you’re trying to hide the search after, use an ELSE)

@StarLion So, I remove the exit(); and then change the form’s action like so

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

I don’t understand what you mean by

(If you’re trying to hide the search after, use an ELSE)

I think what @StarLion; means is this:



<?php
if( isset( $_GET['q'] ) )
{
    include_once "form.php";
    // exit(0);
}else{ ?>
<!Doctype HTML>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Search</title>
    </head>
    <body>
        <form method="get">
         <input type="text" name="q" />
    </form>
    </body>
 </html>
<?php } ?>


Thats the right way to use your php code so as to include the “included page” and prevent the main page from showing.

Ah, okay. Thank you guys for your help! I will try this later today and let you all know!