Auto-append to html form input values?

Discussion in 'PHP' started by dubesor, Jun 11, 2008.

  1. #1
    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?
     
    dubesor, Jun 11, 2008 IP
  2. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #2
    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
     
    hans, Jun 11, 2008 IP
  3. dubesor

    dubesor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    '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?
     
    dubesor, Jun 11, 2008 IP
  4. coffeesonnow

    coffeesonnow Peon

    Messages:
    34
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    so like:

    $sadr = echo $address."New York City";

    ?
     
    coffeesonnow, Jun 11, 2008 IP
  5. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #5
    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.
     
    hans, Jun 11, 2008 IP
  6. dubesor

    dubesor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    how do i put that into a <form> context? i don't know much about php syntax.
     
    dubesor, Jun 12, 2008 IP
  7. coffeesonnow

    coffeesonnow Peon

    Messages:
    34
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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?
     
    coffeesonnow, Jun 12, 2008 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    //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:
     
    php-lover, Jun 12, 2008 IP
  9. dubesor

    dubesor Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    dubesor, Jun 13, 2008 IP