I would like to add a constant text string to a form element (a text input line). The usage would be to append something like ", New York, NY" (let's call this variable "city") to a user-submitted string like "123 Jane Street" (let's call this variable "address"). This would produce "123 Jane Street, New York, NY" or something similar (let's call this end-result string "saddr"). I know this is probably possible with JavaScript onSubmit, but that won't work for those users who have JS turned off. Can this be done easily with PHP? I thought perhaps of creating a hidden input for "city" (since it would be constant) and another hidden form input element for "saddr". The idea would then be to make 'saddr' = 'address'+'city'. Address is the only input that would be accessible to the user. What would the syntax be in PHP for something like this?
I am not fully sure to understand your precise need. but IF the invisible appended text always is the same, then a regular HTML form input will do the very same job. within the form configuraiton of your HTML page, you can add to every form several hidden fields - as shown below <input type="hidden" name="city" value="New York, NY"> <input type="hidden" name="address" value="123 Jane Street"> these hidden input fields are off-limit of visitor-interaction, thus permanent values. you can hwoever also replace the permanent fixed value by a variable. for fixed values that above example would be the easiest method to append text. see below tutorial or Google for more details http://www.w3schools.com/tags/tag_input.asp you can of course also adapt above to XHTML
'address' is not fixed - it is input by the user 'city' is fixed 'saddr' is not fixed; it is equivalent to 'address'+'city' (a concatenation or append operation if you will) does that clarify it a little?
ok got it so you can combine the variable user-input PLUS the fixed hidden value in your output just as you would combine 2 user-input values to create your final printout. just try - I am quiet sure it will work as you'll need it.
I'm not exactly sure what you mean... if you want to place that inside a form, you would just put that in the php script, and then do <? $sadr; ?> in the form. If that's not what you had in mind, could you post an example?
//Let say your file name is appendme.php $self = $_SERVER['PHP_SELF']; $form = ' <form name="appendform" method="post" action="'.$self.'"> <input type="text" name="address"> <input type="submit" name="append" value="go"> </form>'; if(isset($_POST['append'])){ $city = 'New York City'; $saddr = $_POST['address'].' '.$city; echo $saddr; }else{ echo $form; } PHP:
Thanks for your solution. It looks exactly like what I need. Sorry for being so dense, but if I put your code in an external PHP file, how can I then: a) reference the external PHP from my HTML page ... would that be a <script> tag? b) change my <form> elements to reflect the php append Here's the code I have so far: <form name="f" action="URL goes here"> <input name="ie" value="UTF8" type="hidden" /> <input name="f" value="d" type="hidden" /> <input name="dirflg" value="r" type="hidden" /> <input name="ttype" value="dep" type="hidden" /> <input name="daddr" value="123 A ave, new york, NY" type="hidden" /> <input name="date" value="6/15/08" type="hidden" /> Leaving from (address):<br/> [B]<input name="saddr" id="saddr" class="tlp-input" maxlength="2048" type="text" /> [/B]<br/> What time are you planning to leave?<br/> <input id="ftime" class="tlp-when" name="time" maxlength="100" type="text" /> <br/> <input value="Get transit directions" type="submit" /> </form> Code (markup): Like I explained, the value of saddr should be appended with the hidden city string (constant) and passed to the form. the first set of input elements (ie through to date)and their values should remain hidden and also passed along when the form is submitted.