Page 1 of 2
Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 4:09 pm
by Cre8tor
I will have a page with a url of:
http://www.url.com/test.html?item=28444 The number 28444 will change depending on what was selected in the previous page, so the real underlying url would be:
http://www.url.com/test.html?item={%NUMBER}
Using PHP, how would I isolate the number 28444 (or any other number that will appear there), and then print that number into a text field of a form?
I've been told that the parse_url command may be involved, but can someone actually write out the code for my example above?
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 5:16 pm
by tonchily
Uhm, you have to be more specific, but this is how I'll try to help you and hope to get you on your way.
Lets say there's a textfield with the attribute
name="item" in the index.php file
Just do the following code in the very start of the index.php code so you make that number appear in that textfield
Code: Select all
<?php
$item = $_GET['item'];
?>
<html>
/** now here's your form with textfield **/
<form>
Item: <input type="text" name="item" value="<?php echo $item; ?>" />
</form>
</html>
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 5:19 pm
by TipPro
PHP made this task pretty simple...
To avoid an injection attack on your Web site you are going to want to run some checks on the input because malicious code can be passed through a GET variable. So for example, if you know item will always be numerical you can check like this...
Code: Select all
if(!is_int($_GET['item'])) die("invalid input");
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 5:30 pm
by Cre8tor
Thanks for the responses so far but I really just want a section of the url printed, that's all (the number part). And then later add it in the text field of a form. So starting with the first part (print out a section of the url) I've been told I can do this with the parse_url command. How would I use that command to just print out the number part of my url example above?
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 5:37 pm
by tonchily
Cre8tor wrote:Thanks for the responses so far but I really just want a section of the url printed, that's all (the number part). And then later add it in the text field of a form. So starting with the first part (print out a section of the url) I've been told I can do this with the parse_url command. How would I use that command to just print out the number part of my url example above?
No parse_url.
Use this:
Code: Select all
<?php
$item = $_GET['item'];
echo $item;
?>
Remember, you gotta save it as .php file, not as .html (as you showed us in an example)
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 5:56 pm
by Cre8tor
In my effort to keep this simple I managed to make it more complex. The variable "item" is not defined anywhere in my page, it's coming from another place. I wanted to use parse_url to keep this as simple as possible. Can you forget everything I said previously and just let me know how to print out a section of any url using the parse_url command? In my example,
http://www.url.com/test.html?item=28444, shows up on my end. The site from where this was generated sent it over using:
http://www.url.com/test.html?item={%NUMBER}
So how would I use the parse_url command to print out just the numbers 28444 or any other number generated by the previous page?
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 6:27 pm
by tonchily
Cre8tor wrote:In my effort to keep this simple I managed to make it more complex. The variable "item" is not defined anywhere in my page, it's coming from another place. I wanted to use parse_url to keep this as simple as possible. Can you forget everything I said previously and just let me know how to print out a section of any url using the parse_url command? In my example,
http://www.url.com/test.html?item=28444, shows up on my end. The site from where this was generated sent it over using:
http://www.url.com/test.html?item={%NUMBER}
So how would I use the parse_url command to print out just the numbers 28444 or any other number generated by the previous page?
Aaah. Sorry for the inconvenience, you should've said so in the first place.
Try this out, think it should work.
Code: Select all
<?php
$url = parse_url("http://www.url.com/test.html?item=28444");
echo ereg_replace("item=", "", $url["query"]);
?>
If you want me to explain it to you just say so, but it's more or less self-explanatory if you know how the parse_url works.
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 7:04 pm
by Cre8tor
In the line: $url = parse_url("
http://www.url.com/test.php?item=28444"); would I actually put the number 28444 in the code? There will be times when the number is different. Or should I put this instead:
$url = parse_url("
http://www.url.com/test.php?item={%NUMBER}");
Yes, if you could explain what's going on with the ereg_replace command that would be great...
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 7:27 pm
by tonchily
Uhm, didn't you say the item id is previously generated by the user? So yeah, you would use a variable to put it there, not the 28444 number since there are gonna be different numbers.
Yes, I can explain you what ereg_replace does, but first I gotta explain you what parse_url does then;
When you use parse_url function, you can't just define that you want 'the thing after "item="'. There are various components you can choose from and get the return values from:
scheme - return value: e.g. http
host
port
user
pass
path
query - return value: after the question mark ?
fragment - return value: after the hashmark #
The bold ones are the names of those components. As you may follow, after I parse that url you gave me, I'll get the query component value of item=28444 (since it's what's saying after the question mark, right?), and you said you only wanted a number to appear, so I used ereg_replace to replace "item=" with "" (which is nothing, because there's no space or anything in "", those are just two double-quotation marks) which basically say delete string item= so only what you got left is that number.
I don't really understand how you mean to generate that item number, so I can't help you more. Maybe I could help you out if you would be more specific.
Re: Print out parts of a URL, then enter it in a form field
Posted: Thu Sep 02, 2010 7:59 pm
by Cre8tor
To be more specific, the previous page will be a page where a visitor selects a certain item (numbered 1 to 28444). After he clicks any item number, it brings him to my page where the item number is attached to the url. In my page I want to display something like "You have chosen item number:" and then display the item number. To bring this a step further, I want that item number to be entered in a text field in a form which I will be creating.
So does the first line: $url = parse_url("
http://www.url.com/test.php?item={%NUMBER}"); have the proper syntax? Ie/ with curly brackets around the variable and the % symbol in front of the variable? That by the way is how it was given to me by the person who has control over the previous page containing the items.
Re: Print out parts of a URL, then enter it in a form field
Posted: Fri Sep 03, 2010 5:27 am
by tonchily
Cre8tor wrote:To be more specific, the previous page will be a page where a visitor selects a certain item (numbered 1 to 28444). After he clicks any item number, it brings him to my page where the item number is attached to the url. In my page I want to display something like "You have chosen item number:" and then display the item number. To bring this a step further, I want that item number to be entered in a text field in a form which I will be creating.
So does the first line: $url = parse_url("
http://www.url.com/test.php?item={%NUMBER}"); have the proper syntax? Ie/ with curly brackets around the variable and the % symbol in front of the variable? That by the way is how it was given to me by the person who has control over the previous page containing the items.
Umm, no, that's not the proper syntax.
Listen, I still don't get exactly what you're saying. I got this - you want an user to click on an itemid on your website that will take him to other persons website to the address "
http://www.url.com/test.php?item=itemid"?
I'ma show you an example so maybe it helps you somehow.
Lets say there is a page called item.php where people enter item id into textbox and after they submit the form they're being redirected to
http://www.url.com/test.php?item=itemid
Code: Select all
<?php
$itemid = $_POST['itemid'];
if($itemid != ""){
header("location:http://www.url.com/test.php?item=$itemid");
}
?>
<html>
<form method="post" action="item.php">
item id: <input type="text" name="itemid" /> <br />
<input type="submit" value="Submit" />
</form>
</html>
Remember it doesn't have to be a textfield where they will put their item id, it can be a picture after which they click on they're being redirected to there etc. I've just shown you how it's being done.
Re: Print out parts of a URL, then enter it in a form field
Posted: Fri Sep 03, 2010 8:16 pm
by Cre8tor
You're making this way too complicated than it is. Forget about the form part for now. I still can't get the number to display.
Someone else owns a page where there is a list of item numbers to choose from. After a visitor chooses an item, it hyperlinks to my page. The code for the hyperlink to my page is:
http://www.url.com/test.php?item={%NUMBER} So the person who coded the previous page is obviously using a variable called %NUMBER to represent the item number chosen, and then forwarding the visitor to my page with the item number attached at the end of the url.
So I'm trying to get that item number printed out when they get to my page: "You have chosen Item Number:" and then print the item number based on what it says at the end of the url. Do you see what I'm trying to do?
tonchily wrote:
Code: Select all
<?php
$url = parse_url("http://www.url.com/test.html?item=28444");
echo ereg_replace("item=", "", $url["query"]);
?>
I tested this and it does print out the number 28444. I then changed the $url line to: $url = parse_url("
http://www.url.com/test.html?item={%NUMBER}"); because that is how the number is generated and passed on to my page, through a variable called NUMBER. But after making this change, it simply prints out {%NUMBER}. I would like it to print out the actual number chosen by the visitor and which is showing up in the url. Any idea how?
I think the following would do it: Instead of the line: $url = parse_url("
http://www.url.com/test.html?item=28444"); Simply change into php code: $url = parse_url(
refer to the url being displayed right now); Obviously, the tough part would be how would you put that into php code?
Here is another way to put it: Look at the url of this thread right now. You should see something like:
viewtopic.php?f=1&t=120660&p=622754#p622754 Now how do you print out the last part? Say I want to print out p622754, how do you code that in php to separate the url and print that part only?
Re: Print out parts of a URL, then enter it in a form field
Posted: Fri Sep 03, 2010 9:15 pm
by Jonah Bron
Put this in your page and click the link on the other site. Try it. It works.
Re: Print out parts of a URL, then enter it in a form field
Posted: Fri Sep 03, 2010 9:55 pm
by McInfo

This is hilarious!
Forget what you think you know...
I'm just going to, in my own words, review what has already been said.
PHP Manual wrote:When a form is submitted to a PHP script, the information from that form is automatically made available to the script.
PHP Manual:
Variables From External Sources
A GET request is made when
- you submit an HTML form that uses the GET method or
- you enter a URL that contains a query string (example: "http://localhost/script.php?name=value&other=123")
When a GET request is made to your PHP script, the name/value pairs in the query string are split up and put into a superglobal variable named $_GET. In the case of the example above, the $_GET array would contain two keys:
- $_GET['name'] = "value"
- $_GET['other'] = "123"
PHP does this automatically, and the $_GET array is always available to your script, regardless of whether a GET request was submitted. However, if nothing is submitted, $_GET will be empty.
In your script, to display the "123" from "http://localhost/script.php?number=123", put this in script.php:
It's as simple as that.
Some other things to consider:
- Your script must be located in the document root or another directory that is accessible to your Web server.
- Your script must have the correct file extension. Usually, PHP files end in ".php".
Re: Print out parts of a URL, then enter it in a form field
Posted: Fri Sep 03, 2010 10:04 pm
by Cre8tor
Yes you are correct. Finally got it. I was incorrectly advised to use the parse_url command. Amazing how much you can learn from these forums!