访问<a> href variable on same page using $_GET method

时间:2018-08-19 06:18:17

标签: php html ajax

I have a table which will open when a button clicked as a response of ajax call, everything in the website based on ajax call. Now the Problem is the table has lot of rows for that as a solution i opted to go for pagination.But the problem is i want to access the href variable on the same page without refreshing the pabe(as i'm already in the page requested by ajax). How can i access that variable on the same without refreshing the page?

///////  Pagination //////////////////////////////////
    $page = $perpage = $limit = $total = $pages = 0;

    echo $page = (isset($_GET['page'])) && $_GET['page'] > 0 ? ($_GET['page']) : 1;
    $perpage = 20;

    $limit = ($page > 1) ? ($page * $perpage) - $page : 0;

    //////////////////////////////////////////////////////
    //echo the report in table from tmptable
    $sqlqr = "";
    $sqlqry = "SELECT * FROM tmpreport WHERE ACCD = $cmb  LIMIT $limit,$perpage";
    $connect = mysqli_query($rptcon2,$sqlqry);
    echo '<div id="sugg"> Rotate Screen For Better View </div>';
    echo '<div id="sugg2" style = "font-size:10px;text-align:center"> Click On First Account Name For Narration </div>';
    ///////  Pagination //////////////////////////////////
    $total = mysqli_num_rows($connect);
    $pages = ceil($total/$perpage);
    //////////////////////////////////////////////////////
    echo '<div class = pagination>';
        echo '<a href ="?page=2">Prev</a>';
        echo '<a href ="?page=3">Next</a>';
    echo '<div>';
    echo '<div class = "table-responsive">';
        echo '<table class = table table-striped border = 1  cellspacing = 1 cellpadding = 3 id = "table" >';
        echo '<tr style = "background-color:#f0f8ff " >';
        echo '<th style = "display : none" >Id</th>';
        echo '<th>Date</th>';
        echo '<th>Account</th>';
        echo '<th>Account</th>';
        echo '<th align = "RIGHT" >Debit</th>';
        echo '<th align = "RIGHT" >Credit</th>';
        echo '<th>Balance</th>';
        echo '</tr>';
        while ( $rows = mysqli_fetch_array($connect,MYSQLI_ASSOC) ){
            echo '<tr  style = "background-color :'.$rows['COLOR'].'">' ;
                echo '<td style = "display : none" >'.$rows['ID'] .'</td>' ;
                echo '<td>'.( $rows['DATE'] == '1900-01-01'? "-" :$rows['DATE'] ) .'</td>' ;
                echo '<td data-toggle="modal" data-target="#myModal">'.$rows['ACCOUNT'].'</td>' ;
                echo '<td>'.( $rows['SUBACCOUNT'] == "0" ? "": $rows['SUBACCOUNT'] ).'</td>'; 
                echo '<td align = "RIGHT" >'.( $rows['DEBIT'] == 0 ? '-' :$rows['DEBIT'] ).'</td>'; 
                echo '<td align = "RIGHT" >'.( $rows['CREDIT'] == 0 ? '-' :$rows['CREDIT'] ).'</td>'; 
                echo '<td align = "RIGHT" '.( $rows['BALANCE'] >= 0 ? 'style = "color:green "': 'style = "color:red ".' ).' >'.( $rows['BALANCE'] >= 0 ? $rows['BALANCE'].' Dr': $rows['BALANCE'].' Cr').'</td>' ;
            echo '</tr>';
        }
        echo '</table >';
    echo '</div >';
//}

?>
<script>
$(document).ready(function(){
    $("a").click(function(e){
        e.preventDefault();
    })
})

this is the page from where the table will popup as a response

i'm using

echo '<a href ="?page=2">Prev</a>';
echo '<a href ="?page=3">Next</a>';

but i want to access the $page variable as

echo '<a href ="?page=$page-1">Prev</a>';
echo '<a href ="?page=$page+1">Next</a>';

any Help?

1 个答案:

答案 0 :(得分:0)

首先更改此代码:

echo '<a href ="?page=$page-1">Prev</a>';
echo '<a href ="?page=$page+1">Next</a>';

echo "<a href ='?page={$page-1}'>Prev</a>";
echo "<a href ='?page={$page+1}'>Next</a>";

它将在您的代码中显示页面值
但是,您需要使用页面参数分别为每个元素定义click事件
像这样

echo "<a href ='?page={$page-1}' onclick='myFunc({$page-1})'>Prev</a>";
echo "<a href ='?page={$page+1}' onclick='myFunc({$page+1})'>Next</a>";

或者,向每个元素添加另一个属性,并在点击事件中使用它

echo "<a href ='?page={$page-1}' page='{$page-1}'>Prev</a>";
echo "<a href ='?page={$page+1}' page='{$page+1}'>Next</a>";

<script>
$(document).ready(function(){
  $("a").click(function(e){
     // use this.page or this.getAttribute('page') for ajax call
     e.preventDefault();
  })
})
</script>