dtah63820 2017-04-25 15:23
浏览 31
已采纳

将数据发布到同一页面(而不是通过URL)

Am a newbie, a hardcore one.

I am in a need of reloading page with passing variables; so when the page is reloaded it would already have those variables before initializing some of the elements (as u can guess, those variables will be used there).

I need this because I am working with an old videoplayer, which can't be reset on-the-fly, the whole page has to be reloaded to re-initialize it with new params.

Also these variables cannot be passed through url, because they are too long.

I would be glad to see any help and to answer clarifying questions as well. Thanks!

UPD1: There's no real need to delve into specifics of my code thus not posting it; Just imagine a simple page with a textfield and a button, where user enters a string, presses the button, then the page reloads getting that string into a global variable.

UPD2: Found this code on a related page:

$.ajax({
    type: "POST",
    url: "packtypeAdd.php",
    data: infoPO,
    success: function() {   
        location.reload();  
    }
});

That is how I post the data and reload. But how do I get it after reload then? How is this data accessible?

  • 写回答

1条回答 默认 最新

  • douzhao5656 2017-04-25 15:34
    关注

    What about making a simple HTML form with attributes action="" and method="post" like:

    <form action="" method="post"> <!-- in action specify where you want the content to be sent, the method speaks about his self -->
    <input type="[type]" name="[name]"> <!-- in the name attribute define the name with which you can call the data in PHP via $_POST[name] -->
    ...some input fields...
    ...submit btn probably...
    </form
    

    Note: clicking the submit button automatically send the information to the page and redirects you to it. If you leave action empty like above it will redirect you to the same page (refresh it) and receive the data from the form.

    Also if you don't want to use a form you can simply set cookies with PHP. For example:

    setcookie($name, $value, $expireDate, $path(e.g "/");
    

    And then just take information from the cookie with:

    $_COOKIE[$name]
    

    Note: in order to make cookies work, you need to refresh the page, so they can be set!

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?