0

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.

3
  • I don't understand the question. Are you asking how the Javascript can get the ID from PHP? It can get it from the first <td> element in the <tr> that you clicked on. Commented Aug 29, 2013 at 18:19
  • the question is that i clearly have to redirect using javascript but the id (which is a part of the url) is stored in php Commented Aug 29, 2013 at 18:21
  • But PHP put it into the HTML, where Javascript can read it. Commented Aug 29, 2013 at 18:22

4 Answers 4

2
    <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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
});

1 Comment

This was actually just what i needed however the other answers are perfect aswell!
1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.