Andrea Diggelmann Andrea Diggelmann - 28 days ago 5
Ajax Question

How to set a variable by clicking to a link of an auto generated list?

I have created a list, generated from a query, that works (no problem here).

What i would like to do is to trigger an event (probably with jQuery, that I know little about) that would set a php variable.

Let's say I have a table with two columns, '

column 1
' and '
column 2
'.
The list has various links with the valor of 'column 2' displayed on them.
I want that, as you click to one of those links, you could set a valor to a variable that equals to the valor in 'column 1' of that row.

This is what I wrote in my index page:

<div class="example">

<?php
$test = new Test();


$list = $test->getValors($user_id);

echo '<div id="mylist">';
foreach ($list as $element) {
echo '<a href="#">' . $element->COLUMN2 . '</a>';
}

echo '</div>';

$variable = ?;

?>
</div>


I have nothing in my JavaScript file about it because I don't know how to begin.

Can you give me some directions?

EDIT

I resolved partially, even if this is not complete, i include my new code:
index.php



<?php
$test = new Test();


$list = $test->getValors($user_id);

echo '<div id="mylist">';
foreach ($list as $element) {
echo '<a href="#" class="pointername" id="' . $element->COLUMN1 . '">' . $element->COLUMN2 . '</a>';
}

echo '</div>';

$variable = ?;

?>




Javascript

$(document).ready(function () {
$("a.pointer").click(function() {
var variablename = $(this).attr("id");
$.post("ajax/filewithajax.php", {"postname": variablename});
$("#content").load("ajax/filewithajax.php");


});

});

Answer

You could put a event onclick on <a> and call a php function in a other page with jQuery (search about ajax). The php function could return a value that you can use in your main page.

Comments