0

I have a question. My question title doesn't really describe what I need help with, but it's the same concept. This is what I want to do: I am creating a search function (not open for other suggestions than the one I am trying to accomplish), and it's using query string. If I search for "Hello", I want my website to redirect to website.com?s=hello, but if there already is one, add it to the url with a &, like this: website.com?page=home&s=hello.
I tried to use this:

if(isset($_GET)) { // Add the string with & before
else { // Add the string with a ? before

I am willing to use jQuery if I have to

More brief explanation: I have a form, it has an input and a submit-button.

<form action="" method="post">
    <li><input type="text" placeholder="Username"></li>
    <li><input type="submit" value="Search"></li>
</form>

I am aware how to store everything in variables and such, but I am not sure how to add it as a query string to the url. Thank you!

2
  • Provide more code, particularly the HTML. If I understood correctly, in your form (which I am assuming you are using), store the values of your query string in hidden inputs. Commented May 29, 2018 at 17:15
  • I updated the code, thank you for telling me :) Commented May 29, 2018 at 17:36

1 Answer 1

2

A way to do it is to handle it in php in your form:

<?php

if(isset($_GET['page'])) {
    $url = '?page=' . $_GET['page'];
}else {
    $url = '';
}

?>
<form method="GET" action="<?= $url ?>" >
    Search <input type="text" name="s" />
    <input type="submit" value="Search" />
</form>`

Note that if you want to submit your form AND go to the page ?s=foo as a result of submission, you must have a method GET on your form and have an input with argument name="s".

EDIT: Or a bit more exhaustive, if you have several arg that could be passed in the URL it could be :

<?php
if(!empty($_GET)) {

    $url = '?' . $_SERVER['QUERY_STRING'];
}else {
    $url = '';
}
?>

<form method="GET" action="<?= $url ?>" >
    Search <input type="text" name="s" />
    <input type="submit" value="Search" />
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi! Thank you for your reply! But this isn't what I was looking for :( I want the query string to be relative to whether there already is one! So if there already is one defined, add &s=(string), and if there is not, add ?s=(string), do you understand? Thank you! :)
Yes and that's what it will do ^^. Look, in php you check if the user is on a given page by checking the $_GET['page'], if the user is on yourdomain.com/?page=home the $url var value will be '?page=home' then the action in the form will be action="?page=home" and then, by searching "(string)" then clicking on the submit button, it will redirect you automatically to "yourdomain.com/?page=home&s=(string)
I edited my answer to be more exhaustive in the url possibilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.