如何获取点击的超链接的ID并在另一个页面中发布?

时间:2015-11-30 00:50:16

标签: javascript php jquery api

我有两个页面,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>";

1 个答案:

答案 0 :(得分:1)

不需要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。