views:

209

answers:

3

Hi,

How can i display data in a textarea after a link has been pressed? This data is saved in a database.

I'm trying to dynamically create a link for the field 'date'. If a user presses the link, the rest of the data for that date should be shown in a textarea.

i'm looping through my data like this:

foreach($data as $key)
{
    echo '<a href="/"' . $key->test . '>' . $key->date. '</a>';
    echo '<br />';
}

I dont know what i should be putting in 'a href'.

So to be clear, on the left side i got a few links which represent date's. On the right side i want to display a textarea which will contain the remaining data.

How can this be done?

A: 

If you can use jQuery you could do something like:

$('a.dates').click(function(event) {
    event.preventDefault(); // prevents link from reloading page
    $('input#date').val($(this).attr('title'));
})

<?php
    foreach($data as $key)
    {
        echo '<a class="dates" title="$key->date" href="">' . $key->date. '</a>';
        echo '<br />';
    }
?>

<input id="date" value="" name="date" />
blcArmadillo
That won't work for dynamic links and he doesn't have title any way :)
Sarfraz
Seeing as he generates the links using PHP .click should work just fine. And what does it matter if he didn't have a title, he can add it just like we both had him add a class to the link.
blcArmadillo
+2  A: 

See the working demo here.

Modify your loop like this:

foreach($data as $key)
{
    echo '<a class="link" href="#"' . $key->test . '>' . $key->date. '</a>';
    echo '<br />';
}

And then you can use this jQuery code:

$(function(){
  $('a.link').live('click', function(){
    $('#textarea_id').val($(this).text());
    return false;
  });
});

Where textarea_id' is the id of your textarea in which you want to update the values of links eg:

<textarea id="textarea_id">............

Update:

To add the above script, put this code between <head></head> tags eg:

<head>
<script type="text/javascrpt" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
<script type="text/javascrpt">
    $(function(){
      $('a.link').live('click', function(){
        $('#textarea_id').val($(this).text());
        return false;
      });
    });
</script>
</head>
Sarfraz
How do i add this script in a php file?
Yustme
@Yustme: See my update please.
Sarfraz
A: 
Ventus
That won't work for dynamic links :)
Sarfraz