简体   繁体   English

在新标签页中打开用户个人资料

[英]Opening user profile in new tab

I am displaying proclamations in a table and I want to make each row clickable which I kinda did with js onclick function (i can only click on the name but i want to apply this to whole row). 我在一个表中显示proclamations,我想让每一行都可点击,我用js onclick函数做了一些事情(我只能点击名字,但我想把它应用到整行)。 Now I want to see details about proclamations in new tab. 现在我想在新标签中查看有关公告的详细信息。 My question starts here. 我的问题从这里开始。 I want to get proclamation ID when I click on a row but no idea how to do it. 当我点击一行但我不想知道怎么做时,我想获得公告ID。 Plus i am doing onclick with js and rest of it is php so it's getting kinda messy there. 另外,我正在用js进行onclick,其余的是php,所以它在那里变得有点混乱。 I know it's not the best way to do it so i need your advice about how can I solve this issue. 我知道这不是最好的方法,所以我需要你的建议,我该如何解决这个问题。

PS I am not very familiar with js, those js parts been taken from web tutorials. PS我不是很熟悉js,这些js部分来自网络教程。 And the code below is the section where I display the search results. 以下代码是我显示搜索结果的部分。

<section id="results">
    <div class="container">
        <table>
            <tr>
                <a href="#"><th>Name</th></a>
                <th>Where</th>
                <th>From</th>
                <th>Date</th>
                <th>Price</th>
                <th>Type</th>
                <th>PId</th>
            </tr>

            <?php
                if(isset($_POST['search_btn'])){
                    include_once "db_connect.php";

                    $from = $_POST['from'];
                    $where = $_POST['where'];
                    $date = $_POST['date'];
                    $type = $_POST['type'];
                    $procid = $_POST['proc_id'];

                if(empty($from) || empty($where) || empty($date) || empty($type)){
                    header("Location: index.php?search=empty");
                    exit();
                }else{
                    $sql = "SELECT * FROM proc WHERE p_from = '".$from."' and p_where = '".$where."' and type= '".$type."' ";
                    $query = mysqli_query($conn, $sql);
                    $num_rows = mysqli_num_rows($query);
                        while ($rows = mysqli_fetch_array($query, MYSQLI_ASSOC)) {

                            echo "<tr><td onclick='content(this)'>" . $rows['p_name'] . " " . $rows['p_surname']. " </td><td>" .$rows['p_from']. "</td><td>" .$rows['p_where']. "</td><td>" .$rows['p_date']. "</td><td>" .$rows['price']. "</td><td>" .$rows['type']. "</td></tr>" ;
                        }
                        echo "</table>";
                    }

                }else{
                        echo "Error!";
                    }

            ?>


            <?php
                $fullUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

                if(strpos($fullUrl, "search=empty") == true){
                    echo "<p class='error'>You did not fill in all fields!</p>";
                }

            ?>
        </table>
    </div>
</section>

<script>
        function content(elem){
            <?php 

             ?>
            window.open('procdetail.php');
        }
</script>

If you want to continue to use inline JavaScript, you will first need to change the onClick attribute to be on the <tr> element instead of the <td> element. 如果要继续使用内联JavaScript,首先需要将onClick属性更改为<tr>元素而不是<td>元素。 Once that is done, you can use the same function you have to pass the proc_id to the URL in the window.open(); 完成后,您可以使用相同的函数将proc_id传递给window.open(); method you have set in the function. 您在函数中设置的方法。

HTML/PHP HTML / PHP

echo '<tr onclick="content(\''. $rows['proc_id'] .'\')">';

Javascript 使用Javascript

function content(ID){
            window.open('procdetail.php?proc_id=' + ID, '_blank');
        }

This will make each of the table rows a clickable element that links to procdetail.php?proc_id=<YOUR_ID> 这将使每个表行成为链接到procdetail.php?proc_id=<YOUR_ID>的可点击元素procdetail.php?proc_id=<YOUR_ID>

There is a way to perform this in HTML as well, but you want to modify the CSS of the table elements so that there are not non-clickable dead zones in the table row. 有一种方法可以在HTML中执行此操作,但是您希望修改表元素的CSS,以便表行中没有不可单击的死区。

Using your current method of the single line echo, you could use something similar to the code below. 使用单线回波的当前方法,您可以使用类似于下面的代码。 This will append the proc_id data as a URL parameter for procdetail.php to parse. 这将追加proc_id数据作为一个URL参数procdetail.php解析。

Notice that the code only includes p_name and p_surname . 请注意,代码仅包含p_namep_surname You would have to add each definition with the same method. 您必须使用相同的方法添加每个定义。

PHP PHP

echo "<tr><td><a href='procdetail.php?proc_id=". $rows['proc_id'] ."' target='_blank'>". $rows['p_name'] . "</a></td><td><a href='procdetail.php?proc_id=". $rows['proc_id'] ."' target='_blank'>". $rows['p_surname'] ."</a></td></tr>";

CSS CSS

table tr td a {
    display:block;
    height:100%;
    width:100%;
}

table tr td {
    padding-left: 0;
    padding-right: 0;
}

Check out this thread on the issue. 在这个问题上查看这个帖子 It includes a usable demo that is hosted in JSFiddle. 它包含一个托管在JSFiddle中的可用演示

In your JavaScript function that calls window.open , you need to call the second parameter which will be the target to open the window, adding _blank will open it on a new tab instead of the same window: 在调用window.open JavaScript函数中,需要调用第二个参数作为打开窗口的目标,添加_blank将在新选项卡而不是同一个窗口中打开它:

window.open('procdetail.php', '_blank');

Give JavaScript the data needed to handle your information. 为JavaScript提供处理信息所需的数据。

Also, if your idea is to open the tab with JavaScript with the content specially for the row, then you should try returning the necessary information to it, for example, if you want to open a new window with the content of a user, then you must let know JavaScript which user id should open. 此外,如果您的想法是使用JavaScript打开带有专门针对行的内容的选项卡,那么您应该尝试向其返回必要的信息,例如,如果您要打开包含用户内容的新窗口,那么你必须知道JavaScript应该打开哪个用户ID。

This is as easy as returning in an attribute for your row element the value of the user id which data is being outputted by PHP. 这就像在您的行元素的属性中返回PHP输出数据的用户ID的值一样简单。

Do something like this in your inline PHP script: 在您的内联PHP脚本中执行以下操作:

echo "
<tr class='table-item' data-userid='USER_ID_VARIABLE_HERE'>
    <td>" . $rows['p_name'] . " " . $rows['p_surname']. " </td>
    <td>" .$rows['p_from']. "</td>
    <td>" .$rows['p_where']. "</td>
    <td>" .$rows['p_date']. "</td>
    <td>" .$rows['price']. "</td>
    <td>" .$rows['type']. "</td>
</tr>
";

And then in your JavaScript code you could use an event listener for every element in your DOM with class="table-item" : 然后在您的JavaScript代码中,您可以使用事件监听器为DOM中的每个元素使用class="table-item"

window.onload = () => {
    let tableItems = document.querySelectorAll(".table-item");
    tableItems.forEach((element) => {
        element.addEventListener("click", (event) => {
            //Handle your click event here
            let userId = event.target.dataset.userid; //Your user id
            window.open('procdetail.php?id=' + userId, '_blank');
        });
    })
}

This will open in a new tab the contents of the url given which is procdetail.php?id=userId . 这将在新标签中打开给出的url的内容,即procdetail.php?id=userId So then, in your procdetail.php file you must handle the $_GET["id"] variable to show the respective data. 那么,在procdetail.php文件中,您必须处理$_GET["id"]变量以显示相应的数据。

Additional resources: 其他资源:

You can read more of the window.open method here . 您可以在此处阅读更多window.open方法。

尝试这个

<a href="your url address" target = "_blank"> link name </a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM