如何获取单击的超链接的id并在另一个页面中发布


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

我有两个页面,index.php和view.php。我从API获取信息。我需要的是点击索引中的<a>标记,将该<a>的id发布到另一页中的$a变量。我应该使用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>";

不需要jQuery。最简单的解决方案是使用GET传递id的值。

在index.php中,当您在此处生成链接时:

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

href中也添加了链接的id。类似这样的东西:

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

然后在view.php的顶部,使用$_GET:读取参数值

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

现在您将在变量$link_clicked_id上单击链接的ID。