37

How to redirect on another page and pass parameter in url from table ? I've created in tornato template something like this

<table data-role="table" id="my-table" data-mode="reflow">
    <thead>
        <tr>
            <th>Username</th>
            <th>Nation</th>
            <th>Rank</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        {% for result  in players %}
        <tr>
            <td>{{result['username']}}</td>
            <td>{{result['nation']}}</td>
            <td>{{result['rank']}}</td>
            <td><input type="button" name="theButton" value="Detail"
                       ></td>
        </tr>
    </tbody>
    {% end %}
</table>  

and I would like when I press detail to be redirect on /player_detail?username=username and show all detail about that player. I tried with href="javascript:window.location.replace('./player_info');" inside input tag but don't know how to put result['username'] in. How to do this ?

PaolaJ.
  • 10,872
  • 22
  • 73
  • 111

5 Answers5

58

Set the user name as data-username attribute to the button and also a class:

HTML

<input type="button" name="theButton" value="Detail" class="btn" data-username="{{result['username']}}" />

JS

$(document).on('click', '.btn', function() {

    var name = $(this).data('username');        
    if (name != undefined && name != null) {
        window.location = '/player_detail?username=' + name;
    }
});​

EDIT:

Also, you can simply check for undefined && null using:

$(document).on('click', '.btn', function() {

    var name = $(this).data('username');        
    if (name) {
        window.location = '/player_detail?username=' + name;
    }
});​

As, mentioned in this answer

if (name) {            
}

will evaluate to true if value is not:

  • null
  • undefined
  • NaN
  • empty string ("")
  • 0
  • false

The above list represents all possible falsy values in ECMA/Javascript.

Community
  • 1
  • 1
palaѕн
  • 72,112
  • 17
  • 116
  • 136
8

Do this :

<script type="text/javascript">
function showDetails(username)
{
   window.location = '/player_detail?username='+username;
}
</script>

<input type="button" name="theButton" value="Detail" onclick="showDetails('username');">
Ravinder Singh
  • 3,113
  • 6
  • 30
  • 46
7

Bind the button, this is done with jQuery:

$("#my-table input[type='button']").click(function(){
    var parameter = $(this).val();
    window.location = "http://yoursite.com/page?variable=" + parameter;
});
Atticus
  • 6,585
  • 10
  • 35
  • 57
6

Here is a general solution that doesn't rely on JQuery. Simply modify the definition of window.location.

<html>
   <head>
      <script>
         function loadNewDoc(){ 
            var loc = window.location;
            window.location = loc.hostname + loc.port + loc.pathname + loc.search; 
         };
      </script>
   </head>
   <body onLoad="loadNewDoc()">
   </body>  
</html>
TennisVisuals
  • 427
  • 5
  • 8
0

HTML - set an id attribute

<input type="button" id="go-another-page" name="theButton" value="Detail">Go to another page with parameters</td>

JS - Create an action listener for redirecting

  const anotherPackage = document.getElementById('go-another-page');

   anotherPackage.addEventListener('click', (event) => {
    event.preventDefault();
   // After ? mark set your key and variable eg: payment=order-consulting
   // For multiple parameters you can use & eg: payment=order-consulting&amount=20
    window.location.replace('/payment.html?payment=order-consulting');
  });

Retrieve parameters from another page (In this case, payment.html)

// payment.js - this is javascript of your another page
document.addEventListener('DOMContentLoaded', (event) => {
  const parameters = new URLSearchParams(window.location.search);
  const payment = parameters.get('payment');
  console.log(payment);
  event.preventDefault();
});
MD SHAYON
  • 7,001
  • 45
  • 38