【问题标题】:How to display recent queries in get method如何在get方法中显示最近的查询
【发布时间】:2015-12-18 12:53:00
【问题描述】:

我有这个表格

<form action="" method="GET">
<label>NAME:</label>
<input type="text" name="name" id="name" required/>
<button>GET</button>
</form>

<?php

if (isset($_GET['name'])){

$name = $_GET['name'];

echo "hello".$name;}
?>

如果输入一个名称“google” 之后我再次输入“示例”

当我键入另一个名称时,有什么方法可以显示最近输入的名称?

【问题讨论】:

  • 你能再解释一下吗?
  • 将它们保存在会话中的数组中:php.net/manual/en/book.session.php
  • 我想显示在表单中输入的姓氏,以便显示最近搜索过的名字
  • 将它们保存在会话或数据库中。此外,&lt;button&gt;GET&lt;/button&gt; 不会发送表单。您需要添加type="submit" 属性。
  • 如何将每个查询保存到数据库中?

标签: php getmethod


【解决方案1】:

我认为足以解决问题的代码是

<?php
  if(!isset($_GET["name"]){
    print "the name parameter was not supplied";
    die();
  }
  session_start();
  $name = $_GET["name"];
  $_SESSION["list"][sizeof($_SESSION["list"])] = $name;
  var_dump($_SESSION["list"]);
  //I used var_dump here only to show the output you are free to use any other
  //way to display the data as you like

【讨论】:

  • 这并不重要,只需使用 GET 更改 POST 即可完成工作。等一下,我会为你编辑代码
  • 我也在这里得到顶部的 url 是 example.com/?name=google 并且我想要自定义 url 作为 example.com/name/google
  • @AishwaryaTaneja 这应该可以解决这个问题,但我必须警告您,这些更改不是持久的,一旦用户关闭浏览器就会被删除。如果您希望更改持久化,您应该考虑使用数据库来存储所有条目
  • @AishwaryaTaneja 对于自定义路由,您应该查看 URL 路由,因为您需要在 apache 中配置您的 htaccess 文件,而不是从 PHP 中的 GET 请求获取完整请求的 URL 并解析获取请求参数。这是我不久前写的一个脚本,它也实现了 url 路由 github.com/georoot/finna-ninja/blob/master/index.php
  • 是的,你是对的..我必须只将它们保存在数据库中
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 2020-11-04
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
相关资源
最近更新 更多