clicking on a link should display the result in same page .BACK and REFRESH button have to work normally

别等时光非礼了梦想. 提交于 2019-12-13 04:27:58

问题


Basically in my webpage (fields.php), i have two text boxes(X and Y), one submit button and 2 DIV tags (DIV id="load" , DIV id="result"). Based upon the values we provide in the text boxes, a query will fetch some values from the database and display it inside the DIV id="load". Also, I have made the results to display as links(I have simply made the results to display in between anchor tags). So here comes my question, once we click any of the links in the DIV id="load", a query should run in the back ground and it should display some kind values(fetched from database) inside the DIV id="result". The most important thing i want is, the refresh/back/forward buttons should work normally. By this i mean, For example, consider we have 4 links like wwww, xxxx, yyyy, zzzz inside DIV id="load" . so if i click on the link wwww, some result (processed by a query) should get displayed inside the DIV id="result". so now, if i click on xxxx, the result should replace the result for wwww. Also, if i press back button, i should see the result for wwww inside the DIV id="result".

can some body help me on this? can any one provide the sample code.

Here is my code:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>history plugin demo</title>
    </head>
    <body>
    <?php
        $x = ( !empty( $_REQUEST['X'] ) ? $_REQUEST['X'] : null );
        $y = ( !empty( $_REQUEST['Y'] ) ? $_REQUEST['Y'] : null );

    ?>
        Ajax load<BR>
            <form id="myForm" action='fields.php' method='GET' rel="history">
                X <BR>
                <input type="text" name="X" id="ix" value="<?=$x;?>"><BR> <BR>
                Y <BR>
                <input type="text" name="Y" id="iy" value="<?=$y;?>"> <BR> <BR>
                <input id="sub" type="submit" value="Search" align="centre"/>
            </form>
        <hr>
            <!-- here i am performing lot of database queries using php, but to show it to you, i have simplified the code!--> 
            <div id="load"><a href= "#")> <?php echo $x; ?> </a></div> <!-- so here whne i click on the link, i should pass the link as a text parameter to a  query  and display the result in div id= result IMPORTANT is : BACK AND REFRESH BUTTON SHOULD WORK AS NORMALLY !-->
            <div id="result"> </div>
        <hr>
    </body>
</html>

回答1:


As you need to keep the back button to work normally, you can use get parameters to display the results even on clicking back button. I suggest you to change the following lines

<div id="load"><a href= "#")> <?php echo $x; ?> </a></div>
<div id="result"> </div>

to

<div id="load"><a href= "?link=<?php echo $x; ?>")> <?php echo $x; ?> </a></div>
<div id="result">
  <?php if (isset($_GET['link'])) {?>
  <!--do all the db related stuff here to fetch the link result-->
    <?php $link_clicked=$_GET['link'];?>
    <!--//this is my stuff-->
    <?php
      if ($link_clicked == 'link1') 
    echo "link1 results";
      else if ($link_clicked == 'link2')    
    echo "link2 results";
      else
    echo "no results";
    ?>
  <?php }?>
</div>

In the result div, the link which was clicked is obtained from the $_GET and the results for the particular link can be fetched from the database. if you want to clear result div on clicking page refresh button, you can rewrite the browser query string using javascript. Hope this helps :)



来源:https://stackoverflow.com/questions/17838588/clicking-on-a-link-should-display-the-result-in-same-page-back-and-refresh-butt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!