Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get id of a clicked hyperlink and post in another page?

I have two pages, index.php and view.php . I take informations from an API. What i need is on clicked <a> tag in index to post the id of that <a> to $a variable in the other page. Should i use jquery?

index.php

<style>
table a{
    text-decoration:none;
}

</style>
<?php
$login = 'djfjshfjshsdhfsjfhdsjhsjfhsjhfdsjhs';

$url = 'https://api.recruiterbox.com/v1/openings';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:");

$results = curl_exec($ch);
curl_close($ch);  




$data = json_decode($results);
$data1 = json_decode($results, true);


/*
var_dump($data);

*/




if (count($data->objects)) {


        // Open the table

        echo "<table>";
        echo "<th>Job Name</th>";
        echo "<th>Date updated</th>";

        // Cycle through the array
        $string = "noportal";$string1 = "noportal";
        foreach ($data->objects as $idx => $objects) {


            // Output a row
            if($objects->tags[0] !== $string) { 

            echo "<tr>";
            echo "<td><a href='view' id='$objects->id'>$objects->title</a></td>";

            echo "<td>".date('m/d/Y',$objects->created_on)."</td>";

            echo "</tr>";
            }
            else { }

        }

        // Close the table
        echo "</table>";
    }

?>

view.php

<style>
table a{
    text-decoration:none;
}

</style>
<?php
$login = '11sfsdfsdsfsfsfs';

$url1 = 'https://api.recruiterbox.com/v1/openings/';
$a ='mp06fne';
$url=$url1.$a;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:");

$results = curl_exec($ch);
curl_close($ch);  
$data = json_decode($results, true);

/*
var_dump($data);

*/

echo "<table>";
echo "<tr>";
echo "<td>Job Position:</td>";
echo "<td>".$data['title']."</td>";
echo "</tr>";
echo "</table>";

1 Answers

There is no need for jQuery. The simplest solution would be to pass the value of the id using the GET.

In index.php, when you generate the link here:

echo "<td><a href='view' id='$objects->id'>$objects->title</a></td>"

Add the id of link in the href too. Something like this:

echo "<td><a href='view?id=$objects->id' id='$objects->id'>$objects->title</a></td>"

Then at the top of view.php, read the value of the parameter by using $_GET:

$link_clicked_id = isset($_GET["id"]) ? $_GET["id"] : null;

now you will have the ID of the link clicked on the variable $link_clicked_id.

like image 118
Alvaro Montoro Avatar answered Dec 15 '25 11:12

Alvaro Montoro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!