Issue
I want to build a marketplace (index.php) website where users can click on items. After that, another of her websites will open with information about this item. This website is the same for all items, only some text and images are different. My idea was to use php. Clicking on an item will call the info.php file and generate the corresponding html. The problem I’m facing is how to click on this item and transfer the id of that product from the main site to info.php.
index.php:
$id=""
How to give a value to var when this item is clicked (and info.php is called)?
info.php:
include 'index.php';
echo $id;
Then use var to retrieve information from the database.
If there’s a more efficient way to do this migration, please let me know!
Solution
Depending on how many items there are, one way to do it is in index.php you have
<?php
..
?>
<a href="/example.com/info?item=1">Item 1</a>
<a href="/example.com/info?item=2">Item 2</a>
Each link is a link to a differrent item.
in info.php
<?php
$id = filter_var ($_GET ['item'] ?? '', FILTER_SANITIZE_STRING);
?>
Because a param is passed from index, you need to use $_GET. But it should be sanitised to ensure that someone does not pass a param thats going to compromise your site. Additionally, the ?? ''
bit ensures that if the param is not present, you get a space in $id instead of a null. You can change this default value to whatever you want.
Answered By – Rohit Gupta
Answer Checked By – Mary Flores (Easybugfix Volunteer)