1

Is it possible to autopopulate a textbox on a page depending on a string passed in with the URL. For example:

Say we have the following form:

<form name="form_reservation" id="form-reservation">
                    <div style="padding:10px 20px 10px 10px;">
                    <label for="reservation-id">Reservation ID</label>
                        <input name="reservation_id" id="reservation-id" class="reservationid" style="width:120px !important"/>
                        <div style="padding:5px 20px 0px 10px;" class="res_message" id="res-message">&nbsp;</div>
                        <input type="submit" class="button" value="Search Reservation" name="search_button" style="width:150px !important; margin:10px 0px 0px 5px;"/>
                        <input type="button" class="button" value="Clear" style="width:150px !important; margin:10px 0px 0px 5px;" onclick="resetForms('reservation')" />
                    </div>
                </form>

And the user received an email with a link in it: www.website.com/formpage.html######

Where ###### was the ID that I wanted to populate in the reservation-id field on the form. Any way to make this happen?

3
  • 1
    Do you mean a URL like this: formpage.html#1234 or like formpage.html?id=1234? Commented Apr 27, 2012 at 13:34
  • And do you want to do it in PHP or in Javascript? Your question has both tags. Commented Apr 27, 2012 at 13:35
  • Sorry about that, I am open to both. I suppose I would see javascript as maybe being easier from my perspective, but am not opposed to either. Commented Apr 27, 2012 at 14:42

2 Answers 2

1

Assuming the URL the user clicks looks like www.website.com/formpage.html?id=12345, you can spit it out into the input "value" field with GET, e.g.:

<input value="<?php echo htmlspecialchars($_GET["id"]); ?>" name="reservation_id" id="reservation-id" class="reservationid" style="width:120px !important" />

This is the way to do it with php.

EDIT:

Per @Esailija's comment, I wrapped it in the htmlspecialchars function, which as far as I understand will help safeguard against XSS attacks.

Sign up to request clarification or add additional context in comments.

2 Comments

Add addslashes($_GET["id"]) so there will be no surprises.
@Dobiatowski what is the point of addslashes? This is vulnerable to XSS which cannot be avoided with addslashes. Html is escaped with &quot; etc. Also, -1.
0

Javascript:

<script>
    var hash = window.location.hash.replace('#', ''); // get hash and replace the '#' (hash) tag
    document.getElementById('reservation-id').value = hash; // add value to input
</script>

I assumed you were talking about hash location url.html#1111, this can be accomplished only in JavaScript.

2 Comments

I do like the idea of accomplishing this with JS... should this function be placed in the Header of the HTML?
if you wish to put that in the header you must execute it after the page load, otherwise put it after your form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.