简体   繁体   中英

PHP+HTML: List -> Details flow

I have a list of items in a table and next to the items I have a "show details" element. This element has:

onClick="drilldown(this.id)";

function drilldown(aClickedId) {
    // do magic, redirect to showDetails.html?id=aClickedId
}

Then I want to pass the ID as a parameter to a HTML page and redirect the user to it. The HTML page will call a php script that populates the view with data from the DB in the appropriate section:

showDetails.html :

<?php 
    $_GET['id'] = getIdFromURL();
    include "showDetails.php"; ?>

Is this the right approach? if so, how to send a parameter to "showDetails.php" when I'm "including" it?

I didn't understand what is the exact problem. In my view you can pass the id parameter via a get method since you are redirecting the user to showDetails.php

$id = ""; // store the value of id

<a href="showDetails.php?id=$id">Show Details</a>

You can redirect in Javascript:

function drilldown(aClickedId) {
    window.location = "showDetails.html?id=" + aClickedId;
}

By default your showDetails.html will not execute the PHP code within it because only .php files will be parsed and executed as PHP. It is possible to configure your web server to parse and execute .html files but it won't by default.

You don't need this:

$_GET['id'] = getIdFromURL();

All you need to do is, within showDetails.php, use $_GET['id'] . All includes will have access to the $_GET array because it is a superglobal. When a script is included, it is simply inserted into that point and is executed as if it is part of the parent and so it will have access to all variables and functions that are available at the point of inclusion.

It's hard to comment on whether this approach is best practice because I don't think there is enough info about what you're doing. For example, if the element that takes the click is an anchor ( <a></a> ) then you don't need the Javascript redirect at all, just put the ID into the href.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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