dongyi2083 2019-01-07 14:14
浏览 71
已采纳

如何使用php / javascript仅在url中显示特定参数?

I have several parameters which I pass to pages, such as unique id's from my database, error messages and form data. I would like to hide all of these parameters, to keep the url clean, except for the id. I tried looking this up for both PHP and JavaScript, but didn't find a solution, so I started fiddling around.

If I refresh the page when I have hidden the id, the route cannot find the id anymore, which generates a bunch of errors on the page. I want to hide all parameters except for the id so I don't get these errors. I managed to remove all parameters using this bit of Javascript:

let clean_url = window.location['href'].split('?')[0];
window.history.pushState(null, null, clean_url);

I tried to only show the id (which is always a positive integer) if it is set, but when changing my JS to the next block of code, it shows all parameters again.

let url_string = window.location['href'];
let url = new URL(url_string);
let id = url.searchParams.get("id");
if (typeof id !== 'undefined') {
    let clean_url = url_string.split('?')[0].concat("?id={0}".format(id));
} else {
    let clean_url = window.location['href'].split('?')[0];
}
window.history.pushState(null, null, clean_url);

Can anyone tell me what's the problem with my code, or if it is solvable in php? Thanks in advance.

EDIT: due to my lack of time, I won't be able to set my feedback messages to the $_SESSION. I have found a way to fix my javascript so all parameters except for the id parameter are hidden:

let url_string = window.location['href'];
let short_url = window.location['href'].split('?')[0];
let url = new URL(url_string);
let id = url.searchParams.get("id");
if (id == null) {
    window.history.pushState(null, null, short_url);
} else {
    let clean_url = short_url.concat(`?id=${id}`);
    window.history.pushState(null, null, clean_url);
}
  • 写回答

1条回答 默认 最新

  • dongwei7913 2019-01-07 15:03
    关注

    Use the good medium for your data.

    That's not always true but a good rule of thumb is that error messages goes in $_SESSION and forms data goes in $_POST.

    For example, your form could look like this :

    <form method="post" action="myurl1.php?id=123">
      <!--you could also use a hidden field to send the id with post : <input type="hidden" name="id" />-->
      <input name="test" />
    </form>
    

    And the processing of your form could give this :

    if(empty($_POST[test])){
      $_SESSION['error']='Empty test field';
    }
    header('Location: myurl2.php?id='.$_GET['id']); //or $_POST['id'] if you used a hidden field
    

    Point is you decide how you send the data, and you should use the url only when it have value for your users.

    (More on php sessions here)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题