duanlu0386 2017-01-10 12:59
浏览 69

使用.innerHTML重新加载后,在页面上保留表单ID的输出值

I have a search form where onclick of the submit button it displays the form value underneath. This is the code:

 <input type="text" name="searchfield" id="searchfield" value="" />
<input type="submit" name="submit" id="submitSearch" value="Submit" onclick="document.getElementById('output').innerHTML = document.getElementById('searchfield').value" />
<div id="output"></div>

It works fine, but once submitted (the page reloads rather than going to another page) the value in the output div disappears.

Is there a way to make the value remain, even after page reload?

I'm trying to find the simplest way to achieve this.

</div>
  • 写回答

1条回答 默认 最新

  • duanliujie8639 2017-01-10 13:20
    关注

    You can do this in severals ways for example using get values, sessions, cookies, localstorage, ajax,...

    Since you are trying to build a search form GET values seems the most appropriate (or ajax too).

    An example of code:

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
      <input type="text" name="searchfield" id="searchfield" value="" />
      <input type="submit" id="submitSearch" value="Submit"/>
    </form>
    
    <div id="output">
    
    <?php
    
    if(isset($_GET['searchfield'])) {
      echo $_GET["searchfield"];
     }
    
    ?>
    
    </div>
    

    Check AJAX if you don't want a reload.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法