-1

Is it possible to submit a query without updating the URL?

On a dictionary, I want to do first one simple query: www.example.com/?q=word

On the result page there will be a button with which I want to search for more results with another query.

This second query will be done normally with www.example.com/more.php?q=word.

That code would look as follows:

<button onclick="window.location.href = 'more.php?q=<?php echo "$trimmed"; ?>';">search for more results</button>

However, I want that the URL remains unchanged so that the next dictionary query starts again with a simple query. In other words, I want to hide the part "more.php" from the URL.

user9
  • 145
  • 1
  • 12

1 Answers1

1

Assuming that your www.example.com?q=word points to the index.php.
Assuming also that your more.php contains functions. Assuming as third, that your index.php returns something displayable in the browser even if there is no GET-parameter i.e. the initial page call. Doing so, a really simple solution would be to fire every query against the index.php. There you can handle every query, even different types, based on a new GET-parameter type use use.

#index.php

require 'more.php';

// do something here to validate the parameters you use

switch($_GET('type')) {
    case 'simple':
        return simpleCall();
        break;
    case 'more':
        return additionalInfo();
        break;
}

function simpleCall() {
    // do stuff here
    // access $_GET for other paramters
}

#more.php

function complexCall() {
    //do complex stuff here
}

Finally, your HTML would look something like this

<button onclick="window.location.href = '/?type="more"&q=<?php echo "$trimmed"; ?>';">search for more results</button>

Until you get more than these two types it becomes cluttering at your switch-statement.

For this reason, it would be a good idea to think about different solutions like:

  • having a routing system like this https://medium.com/the-andela-way/how-to-build-a-basic-server-side-routing-system-in-php-e52e613cf241
  • using asynchronous calls from your frontend with the help of JavaScript to call to different PHP files on the server but stay on the same page in the frontend. This will immediately lead to some kind of API. But this is generally not the badest idea.
  • Please validate your parameters regardless if POST or GET before you do anything with them in the rest of your code. Having the example of a dictionary sounds extremely like to query a database where SQL injection is, for example, a big thing if data us used unchecked.

I hope this helps a bit.

Danaq
  • 633
  • 1
  • 6
  • 18
  • Hm. The idea is not so bad. But in my case I would have to change everyting. My code looks as follows: ยดยดยด Couldn't I solve the problem with a goto in front of the second query? โ€“ user9 Apr 09 '20 at 23:10
  • Your mixing frontend and backend. You are including the files with the query logic into the file which is displayed to the user. And your form does not have an action parameter where it is sent to. If you really want to send the form to its own page, then you can wrap your includes into an if-statement or switch-statement as in my example above. โ€“ Danaq Apr 10 '20 at 13:09
  • Indeed, after some testing, the switch solution was the best one, as explained also here: https://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form โ€“ user9 Apr 14 '20 at 16:01