Marc Rasmussen
Marc Rasmussen

Reputation: 20545

onclick redirect to url stored in php variable

I have the following table:

foreach ($forwards as $data):
    ?>

    <tr style="cursor: pointer" class="main_row">

        <td><?php echo $data['Offer']['id']; ?> </td>
        <td><?php echo $data['Offer']['name'];?> </td>
        <td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
        <td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
        <td><?php echo $data['Category']['name']?></td>
        <td><?php echo $data['Country']['name'] ?></td>
    </tr>
    <?php
     endforeach; ?>

now the idea is that when ever you click one of the main_row it should redirect to a new url. the problem is that the url contains an id for instance the url could look like www.example.com/details/2 where 2 is the id.

This id is like all of the other data stored in a php variable: $data['Offer']['id'];

Now my question how can i redirect using both php and javascript? is there a work around? .

Please do note that i am fully aware that php i server side and javascript is not. it is because of this i am asking this question.

Upvotes: 0

Views: 1379

Answers (4)

wesleywmd
wesleywmd

Reputation: 605

If I am understanding you question correctly, your not looking for a redirect. To redirect in php, it must be done before you echo anything to the browser.

Because you want to "redirect" after your display is rendered, you should be using anchors.

<td><a href="www.example.com/details/<?php echo $data['Offer']['id']; ?>">
    <?php echo $data['Offer']['id']; ?> 
</a></td>

The anchor will direct the browser to the href url.

Upvotes: 1

Barmar
Barmar

Reputation: 780723

You can get the ID from the first column in the row:

$(".main_row").click(function() {
    var id = $(this).find("td:first-child").text();
    window.location.href = 'www.example.com/details/' + id;
});

Upvotes: 1

Gabriel C. Troia
Gabriel C. Troia

Reputation: 3330

If you use jQuery do this:

$('.main_row').click(function(){
   window.location.href = "YOUR_URL"+$(this).attr('id');
});

And modify the html for the row to look like:

<tr style="cursor: pointer" class="main_row" id="<?php echo $id ?>">

This way - any time you click on the row it appends the row ID to the url and redirect you there.

Upvotes: 1

Frankey
Frankey

Reputation: 3757

    <table>     
       <?php  foreach ($forwards as $data): ?>                        
                <tr data-link="http://www.example.com/details/<?php echo $data['Offer']['id']; ?>">    
                    <td><?php echo $data['Offer']['id']; ?> </td>
                    <td><?php echo $data['Offer']['name'];?> </td>
                    <td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
                    <td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
                    <td><?php echo $data['Category']['name']?></td>
                    <td><?php echo $data['Country']['name'] ?></td>
                </tr>
         <?php  endforeach; ?>
    </table>

   <script>
    $('table tr').click(function(){
         document.location.href = $(this).data('link');
    });
   </script>

if you use jQuery though.

Upvotes: 2

Related Questions