Skip to content
Advertisement

html php query without URL update

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement