Print out parts of a URL, then enter it in a form field
Moderator: General Moderators
Print out parts of a URL, then enter it in a form field
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?
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
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
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
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
$item = $_GET['item'];
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
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
No parse_url.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?
Use this:
Code: Select all
<?php
$item = $_GET['item'];
echo $item;
?>
Re: Print out parts of a URL, then enter it in a form field
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?
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
Aaah. Sorry for the inconvenience, you should've said so in the first place.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?
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"]);
?> Re: Print out parts of a URL, then enter it in a form field
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...
$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
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.
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
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.
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
Umm, no, that's not the proper syntax.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.
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
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?
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?
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?
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?tonchily wrote:Code: Select all
<?php $url = parse_url("http://www.url.com/test.html?item=28444"); echo ereg_replace("item=", "", $url["query"]); ?>
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?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Print out parts of a URL, then enter it in a form field
Put this in your page and click the link on the other site. Try it. It works.
Code: Select all
<?php
echo $_GET['item'];
?>Re: Print out parts of a URL, then enter it in a form field
Forget what you think you know...
I'm just going to, in my own words, review what has already been said.
PHP Manual: Variables From External SourcesPHP Manual wrote:When a form is submitted to a PHP script, the information from that form is automatically made available to the script.
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")
- $_GET['name'] = "value"
- $_GET['other'] = "123"
In your script, to display the "123" from "http://localhost/script.php?number=123", put this in script.php:
Code: Select all
echo $_GET['number'];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
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!