Nomis
Nomis

Reputation: 457

Set session when clicking html link?

I have searched and tested different solutions all day without luck. In the below code I want (when clicked) to set a session on the "open" links I echo in the foreach loop. I tried using AJAX but I am new to AJAX and could not make it work. I know how to do it using GET but it is too risky, so i welcome your suggestion and preferably examples.

$task_array = array_combine($task_id_unique, $task_status);
                foreach ($task_array as $card_nr => $card_status) { 
                ?>  
                    <table>
                      <tr>
                        <th>card nr.</th>       
                        <th>Status</th>       
                        </tr>
                        <td><?php 
                                        echo $card_nr;?></td>
                        <td>       
                   <?php 
                            if ($card_status == true) {     
                                    echo "<a href=workcard.php>Open</a>";
                            }
                            else echo "Done ". $card_nr;?></td>
                    </table>

Upvotes: 0

Views: 2473

Answers (3)

alfa6661
alfa6661

Reputation: 205

first, add html class on "Open" link and custom html5 attribute to store your card data. for example data-card.

<a href="#" class="your-class" data-card="card_nr_value">Open</a>

next, create Onclick event for a link to send an ajax request.

$( document ).ready(function() {
    $(".your-class").click(function() {
        var card_nr = $(this).attr('data-card');
        $.ajax({
            url: "workcard.php",
            type: "POST",
            data: "card=" + card_nr
        }).done(function() {
            // do something
        });
        return false;
    });
});

Upvotes: 0

hamed
hamed

Reputation: 8033

You should use ajax on click of link:

$.ajax({
      url: 'test.php',
      dataType: 'json',
      type: 'post',
      data: {name: "value", name2: "value2" /* ... */},
      success: function (data) {

      }

     });

in your test.php, $_POST['name'] is equal to "value". and there you can do everything you want.

Upvotes: 0

Waxi
Waxi

Reputation: 1651

What have you tried and what doesn't work, because this looks like what you need...

HTML:

<a href="home.php?a=register">Register Now!</a>

PHP:

if(isset($_GET['a'])){

    $_SESSION['link']= 'whatever';

 }

And if you need to do it without a page refresh, then use AJAX.

Upvotes: 1

Related Questions