Вопрос

Im sorry if the title isnt exactly right, but Im not sure what exactly I want (well, how to tell it in english).

Basically I have one input field, where somebody can type some text and push enter/button to search the DB for it. That form redirrects him to site.com/search.php?search=my+dumb+text But I want to have there a selector (it will be drop down select), so they can lookup by Title or User and I want search URL to look like this:

site.com/search.php?user=dumb+name site.com/search.php?title=weird+title

Just for clarification: I DONT want URL to be: site.com/rearch.php?user=dumb+name&type=user Othervise I wouldnt have to ask there.

Is that possible without using JS or sending form as POST and then redirrecting them to search.php file?

Thats how it looks now:

<div class='search'>
    <form method='get' action='search.php'>
    <select name='type'>
        <option value='title'>Title IMG</option>
        <option value='user'>User IMG</option>
    </select>
    <input type='text' name='search' value='$search' placeholder='Search' class='search-field'><input type='submit' value='s' class='search-button'>
    </form>
</div>
Это было полезно?

Решение 3

As others pointed out what I hoped isnt true: Its impossible without redirect so I used the .htaccess to get it done.

Here is the form code:

<div class='search'>
    <form method='get' action='search.php'>
        <select name='type' class='search-select'>
            <option value='replay' ".(isset($_GET['replay']) ? 'selected' : '').">Replays</option>
            <option value='player' ".(isset($_GET['player']) ? 'selected' : '').">Users</option>
        </select>
        <input type='text' name='search' value='$search' placeholder='Search' class='search-field'>
        <input type='submit' value='s' class='search-button'>
    </form>
</div>

.htaccess version

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^type=player&search=(.*)$ [NC]
RewriteRule ^search.php$ /search.php?player=%1 [L,R=301]
RewriteRule ^search.php?player=%1$ /search.php [L]

RewriteCond %{QUERY_STRING} ^type=replay&search=(.*)$ [NC]
RewriteRule ^search.php$ /search.php?replay=%1 [L,R=301]
RewriteRule ^search.php?replay=%1$ /search.php [L]

PHP version

if (isset($_GET['type'])){
    if ($_GET['type'] == "player"){
        header("location:/search.php?player=".$_GET['search']);
        exit();
    }elseif($_GET['type'] == "replay"){
        header("location:/search.php?replay=".$_GET['search']);
        exit();
    }
}

Другие советы

what you want is maybe a radio to let the user select which type of search he wants:

<input type="radio" name="type" value="user">User<br>
<input type="radio" name="type" value="title">Title

this way you would get queries like ?search=A+B+C&type=user

this way its more flexible than changing the "search" parameter to something else

it is not possible to dynamically change the path to submit the form to or the field names in the form without js.

<div class='search'>
<?php echo "<form method='get' action='search.php?title= ".input_filter(INPUT_GET, 'search');." '>" ?>
<select name='type'>
    <option value='title'>Title IMG</option>
    <option value='user'>User IMG</option>
</select>
<input type='text' name='search' value='$search' placeholder='Search' class='search-field'><input type='submit' value='s' class='search-button'>
</form>
</div>

Try this code, let me know if it worked :D.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top