简体   繁体   中英

how can I get a url segment?

I have the last url segment of the following url

http://something.com/somefunc/10

that is the number 10;

My real problem is that I have a form that I need to post an action of the url

<form action="../something/somefunc/<?php echo $id>">
   //some code
</form>

after receiving the posted stuff, I would like to still be able to get the id of the product under consideration. I think now that form's action url is the only way I know that can help retain such an id. Any help is appreciated, thank you.

[ UPDATE ]

Uhmmm, all the answers below are correct, don't know which one to choose as the best reply.

Something along these lines may work...

$url = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$path = $parts['path'];
$keys = explode('/', $path);
echo $keys[4]; // `corresponding to the 4th url word`

Hope this helps!

Sorry if I'm misunderstanding, but can't you get the ID of your posted form via the $_POST array? http://www.w3schools.com/php/php_post.asp

Alternately, if the ID is in a URL parameter, you can access it via the $_GET variable: http://www.w3schools.com/php/php_get.asp

您可以使用ID输入隐藏的输入

<input type="hidden" name="id" value="<?php echo $id; ?>">

1.You could pass the product id using POST as a hidden input in your form:

<input name="productid" id="productid" type="hidden" value="<?=$id ?>">

and in the page you post to:

<? if (isset($_POST['productid'])) $id = $_POST['productid'] ?>

2.Alternatively use GET & pass it on the querystring

action="../something/somefunc/[processingpage]?productid=<?=$id ?>"

and in [processingpage]:

<? if (isset($_GET['productid'])) $id = $_GET['productid'] ?>

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