Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect on another page and pass parameter in url from table?

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 ?

like image 952
PaolaJ. Avatar asked Dec 31 '12 10:12

PaolaJ.


People also ask

How redirect to another page and pass parameter in URL?

To redirect to another page in JavaScript, you can use window. location. href property, window. location will also work and if you want you can use window.

Can you redirect urls with parameters?

You can use URL Redirect to forward your visitors to a specific page at the destination URL and pass values via query strings to the destination.

How do I redirect a URL to another page?

The simplest way to redirect to another URL is to use an HTML <meta> tag with the http-equiv parameter set to “refresh”. The content attribute sets the delay before the browser redirects the user to the new web page. To redirect immediately, set this parameter to “0” seconds for the content attribute.

How do you pass parameters in hyperlinks?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".


2 Answers

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');"> 
like image 30
Ravinder Singh Avatar answered Sep 22 '22 02:09

Ravinder Singh


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.

like image 89
palaѕн Avatar answered Sep 21 '22 02:09

palaѕн