简体   繁体   English

html php 查询没有 URL 更新

[英]html php query without URL update

Is it possible to submit a query without updating the URL?是否可以在不更新 URL 的情况下提交查询?

On a dictionary, I want to do first one simple query: www.example.com/?q=word在字典上,我想做第一个简单的查询: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.第二个查询将通过 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.但是,我希望 URL 保持不变,以便下一个字典查询再次以简单查询开始。 In other words, I want to hide the part "more.php" from the URL.换句话说,我想从 URL 中隐藏“more.php”部分。

Assuming that your www.example.com?q=word points to the index.php.假设您的www.example.com?q=word指向 index.php。
Assuming also that your more.php contains functions.还假设您的more.php包含功能。 Assuming as third, that your index.php returns something displayable in the browser even if there is no GET -parameter ie the initial page call.假设第三个,您的index.php即使没有GET参数(即初始页面调用)也会在浏览器中返回可显示的内容。 Doing so, a really simple solution would be to fire every query against the index.php.这样做,一个非常简单的解决方案是针对 index.php 触发每个查询。 There you can handle every query, even different types, based on a new GET-parameter type use use.在那里,您可以根据使用的新 GET 参数type处理每个查询,甚至是不同的类型。

#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最后,您的 HTML 看起来像这样

<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.除非您获得的不仅仅是这两种类型,否则它会在您的switch语句中变得混乱。

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有这样的路由系统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.在 JavaScript 的帮助下使用来自前端的异步调用来调用服务器上的不同 PHP 文件,但保持在前端的同一页面上。 This will immediately lead to some kind of API.这将立即导致某种 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.在您对代码的 rest 执行任何操作之前,请验证您的参数,无论是POST还是GET 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.有一个字典的例子听起来非常像查询一个数据库,其中 SQL 注入是一件大事,例如,如果我们使用未经检查的数据。

I hope this helps a bit.我希望这能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM