Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When a user clicks on the table row, I want to:

  • save a value in the user profile
  • go to a page

I am within this loop:

<?php $query = new WP_Query(array('post_type' => 'aanvraag', 'posts_per_page' =>'-1', 'post_status' => array('publish', 'pending', 'draft', 'private'/* , 'trash' */) ) ); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<?php endwhile; endif; ?>

This is not working:

<tr onclick="window.location.href='http://www.areal-agro.nl/<?php 
    $user = get_the_title();
    update_usermeta( $current_user->id, 'current_user', $user );        
?>'">

If I echo the $user value, it outputs the right (current) value.
If I use the update_usermeta as written, it uses the last value.
If I use update_usermeta( $current_user->id, 'current_user', 'test'); it works.

How can I export the current value? Why is onclick not behaving like it's in the loop, and how can I improve this?

share|improve this question
    
Hi Lars, are you familiar with the general concept of AJAX and the WordPress AJAX API? –  fischi Mar 19 '13 at 9:35
    
I've looked into AJAX but it seems to difficult for me as of yet. Are you saying that the onclick doesn't get the php values realtime? –  Lars van Peij Mar 19 '13 at 9:57
    
The only way to execute some PHP using Javascript is by AJAX. This link on Ajax in Plugins will give you some insight on how to do it. –  RRikesh Mar 19 '13 at 10:02
add comment

1 Answer

up vote 0 down vote accepted

In simple terms, window.location.href is javascript, and will run in the browser, but PHP runs on the server. In order to execute update_usermeta() you need to open a page with that block of code on it. The best way to do this from a click is to use AJAX (javascript) to visit the URL on which you have this code. Most likely you'll need to create a new PHP file with your code in it and write some AJAX to visit the page.

share|improve this answer
    
Thanks, any hints on how to get started? Or are there any other possibilities of retrieving that value? –  Lars van Peij Mar 19 '13 at 11:01
    
Maybe it's easier to make a static php link with some kind of variable in the url and pass it through to the next page? Such as http:/www.areal-agro.nl/?page-id=740?aanvraag=15?aanvrager=6 –  Lars van Peij Mar 19 '13 at 11:13
    
Lars: your static approach will certainly work. Ajax would just mean that the user would not be taken to a separate page - the action would happen in the background. If navigating to a page solves your problem, do that instead. You can always edit it later to run via AJAX with minimal changes. Just a small note, the URL you posted is not legal, it should only have one question mark. Additional parameters must use & instead: http:/www.areal-agro.nl/?page-id=740&aanvraag=15&aanvrager=6 –  roryok Mar 19 '13 at 15:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.