开发者

Reading id from URL

My question is pertaining to extracting information from the URL using PHP. I've three pages in the website I am building

catalogues.html

form_userdata.php

mail.php

The schema is as follows. The user will choose a file to download from catalogues.html for eg

a href="form_userdata.php?id=1026&name=Vibrating Feeder - Heavy duty -VFH" target="_blank"

This id & name will be passed to form_userdata.php and after entering the details in form_userdata.php page, the page passes control to mail.php, that will check if the fields are all true and valid.

My question is how can I use the ID & nam开发者_开发问答e specified in "a href" in my code? I am passing the ID and name from catalogues->form_userdata and collecting it in mail.php

Thank you for your valuable input


you can use sessions to store it when processing form_userdata.php and then use it in mail.php. That way you don't have to pass it in every URL.


when I add $id=$_POST['id']; at form_userdata, I get an error saying "undefined index: id"

That's quite logical, since you're passing them in the URI, and thus have to read them using $_GET in PHP, ie. do :

$id = isset($_GET['id']) ? (int)$_GET['id'] : null;


I found this solution:

session_start();

$_SESSION['id'] = $id=$_GET['id'];

$_SESSION['name'] = $name=$_GET['name'];

this will pass the value of ID and Name to the next page. Thank you for your help


you may use $_GET to collect and pass variables


You can access the named values in an URL (query string, query-info part) by using the $_GET superglobal. It will contain the value by it's name:

$_GET['id']

More information is in the the PHP manual: $_GET. That page is already pretty specific, I suggest you read this manual page as well: Variables From External Sources it more broadly describes how this works.

As input is very important in programming, it's really recommended to understand how it works so you can use the language for your needs more easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜