0

I'm trying to add a value to the end of a url that's already preset.

So for example a user would type "hello world" into an input box, press submit and they would be taken to:

http://google.com/?q=hello%20world

Can I do this in HTML or will I need to include some PHP?

Any help would be fantastic. Thanks

Oldskool
  • 34,211
  • 7
  • 53
  • 66
Samuelofc
  • 15
  • 1
  • 3
  • Okay PHP it is. Any ideas on how I do this in PHP? – Samuelofc Feb 12 '12 at 21:31
  • I like the answer by @Vigrond, but if you need something to modify the URL as a string, [try this php function](http://stackoverflow.com/questions/7356555/better-way-to-replace-query-string-value-in-a-given-url/7356682#7356682) that I wrote. – Scuzzy Feb 12 '12 at 21:36

2 Answers2

3

This would be considered a GET request and is natively possible with HTML forms.

Example:

<form action="http://google.com" method="GET">

  <input type="text" name="q"/><br/>

  <input type="submit" value="Submit"/>

</form>

Once the submit button is clicked, the browser will make a GET request and direct the user to http://google.com/?q=value

Vigrond
  • 8,148
  • 4
  • 28
  • 46
  • 1
    Is there a way to add it onto more values? Such as if my initial URL is "http://google.com/?q="hello world"" then typing "test" into the input box would turn it into "http://google.com/?q="hello world"&w="test"? – Samuelofc Feb 12 '12 at 21:42
  • If you're just adding more input fields onto the page, then this will be fine, otherwise if you're using a singular input field, client side javascript would be required, unless you're willing to process the query string data in code and re-direct the user to your newly formed URL. – Scuzzy Feb 12 '12 at 21:48
  • You can add onto it by adding more text inputs. If you are saying can you add onto it post-submit, then typically you would save the data 'q' somewhere, and present a new input 'w' to allow the user to make a 2nd GET request. It's difficult to describe to you the best way to go about this as you have not described your project. But I would recommend studying the basics of GET & POST requests in HTML, as well as client-side and server-side concepts. – Vigrond Feb 12 '12 at 21:49
  • I'm not bothered, it's for personal use rather than distribution. I've never really touched JavaScript so the simpler the better really! Appreciate the help – Samuelofc Feb 12 '12 at 21:51
  • @Vigrond - The actual project is something for myself. I'm trying to edit the URL to this link "map.sysomos.com/…; so I can enter my own value to Q in a form box and it'll just edit that part of the link. Not sure if I'm explaining it very well. – Samuelofc Feb 12 '12 at 22:12
  • So you can create a form just like above, with the action your link *without* GET variables. In the form you will include the GET variables via input elements with the name attribute corresponding. If the name and value of the GET variable will always be the same, you can just make a element and set its name and value attributes accordingly. btw Please mark this as your answer if you are satisfied. – Vigrond Feb 12 '12 at 22:19
0

add a string to a string

if $str is "A" and $str2 is "B"

$str .=$str2;

results in $str being set to "AB"