add text to input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Hobbs

    add text to input

    Hi there

    I have an input box in a form (very simplified example below so we're
    all on the same page).

    <form method='post' action='http://www.theaddress. com/target.tcl'>
    <input type='text' name='tb1' value=''>Search text
    <input type='submit' name='action' value='Generate '>
    </form>

    When the user submits. I want to concatenate characters to the input
    that the user typed in. Without the user knowing.

    So if the user types "string", I want the form to submit "%string".

    TIA
    Simon
  • Philipp Lenssen

    #2
    Re: add text to input

    Simon Hobbs wrote:
    [color=blue]
    >
    > <form method='post' action='http://www.theaddress. com/target.tcl'>
    > <input type='text' name='tb1' value=''>Search text
    > <input type='submit' name='action' value='Generate '>
    > </form>
    >
    > When the user submits. I want to concatenate characters to the input
    > that the user typed in. Without the user knowing.
    >
    > So if the user types "string", I want the form to submit "%string".
    >[/color]

    That's no problem, though not part of HTML. Instead, that's server-side
    scripting with a language like PHP, ASP (VBScript/ JScript), Python,
    JSP, Perl, and so on. All you would need to do in e.g. PHP is to write
    something like
    if (!isset($tbl) { $tbl = ""; }
    $tbl = "%" . $tbl;

    If you do not have control over the server, or you want to submit to an
    absolute URL of your partner site which is not under your control, you
    can't handle the task foolproof. You might add some JavaScript, but
    it's not reliable.

    Comment

    • Chris Morris

      #3
      Re: add text to input

      get_simon_hobbs @hotmail.com (Simon Hobbs) writes:[color=blue]
      > I have an input box in a form (very simplified example below so we're
      > all on the same page).
      >
      > <form method='post' action='http://www.theaddress. com/target.tcl'>
      > <input type='text' name='tb1' value=''>Search text
      > <input type='submit' name='action' value='Generate '>
      > </form>
      >
      > When the user submits. I want to concatenate characters to the input
      > that the user typed in. Without the user knowing.
      >
      > So if the user types "string", I want the form to submit "%string".[/color]

      You need to do that on the form handler server-side. Otherwise you
      can't be sure that the user won't know or that a user won't
      deliberately submit one without it.

      --
      Chris

      Comment

      • Simon Hobbs

        #4
        Re: add text to input

        > > When the user submits. I want to concatenate characters to the input[color=blue][color=green]
        > > that the user typed in. Without the user knowing.
        > >
        > > So if the user types "string", I want the form to submit "%string".[/color]
        >
        > You need to do that on the form handler server-side. Otherwise you
        > can't be sure that the user won't know or that a user won't
        > deliberately submit one without it.[/color]

        Thanks to both posters.

        I'm hiding it from the users so I don't confuse them. And so I don't
        have to say: "here's what you want, except you have to type a '%' sign
        before the search text". Also, I understand the value of server-side
        scripting, but it is indeed a case of control, and the developers are
        working on other stuff.

        So it sounds like JavaScript is the go. I guess I get JS to build the
        address string with all the necessary parameters and then point the
        browser there? If someone can point me to an example then I should be
        able to work from it. That would be cool.

        S.

        Comment

        • Simon Hobbs

          #5
          Re: add text to input

          Oh, that was painless. I used 'onSubmit' to add the desired character
          to the string and put it in a hidden textbox.

          For schmucks like me, the complete html is below. Ironically, the
          example uses Google and Google doesn't support wildcards.

          Thanks to the poster for nudging me in the right direction.
          S.

          -------html below--------
          <html>
          <head>
          <script language='JavaS cript'>
          <!--
          function addwildcard()
          {
          document.f1.as_ q.value = '%' + document.f1.myi nput.value
          }
          // -->
          </script>
          </head>
          <body>

          <form method='get' action='http://www.google.com. au/search'
          name='f1' onSubmit='addwi ldcard()'>
          Keyword
          <input type='text' value='' name='myinput' size='10'>
          <input type='hidden' name='as_q'>
          <input type='submit' name='btnG' value='Search'>
          </form>

          </body>
          </html>

          Comment

          • Owen Jacobson

            #6
            Re: add text to input

            Simon Hobbs wrote:
            [color=blue]
            > I'm hiding it from the users so I don't confuse them. And so I don't
            > have to say: "here's what you want, except you have to type a '%' sign
            > before the search text". Also, I understand the value of server-side
            > scripting, but it is indeed a case of control, and the developers are
            > working on other stuff.[/color]

            I'm guessing that you're dropping the text into a WHERE foo LIKE clause
            in a SQL statement as your search mechanism. If so you may want to
            either document that % and _ are wildcards or escape any user-submitted
            % and _ characters before you add your own (check your DB docs for
            appropriate escaping). The usual caveats about sanity-checking user
            input also apply (making sure that any string-terminating characters
            are escaped, etc).

            Comment

            • Owen Jacobson

              #7
              Re: add text to input

              Simon Hobbs wrote:
              [color=blue]
              > Oh, that was painless. I used 'onSubmit' to add the desired character
              > to the string and put it in a hidden textbox.[/color]

              This will fail on browsers that do not support javascript or that have
              it disabled. Doing it on the server is a much smarter plan. At least
              make a note that this is going on for those users, so that searching
              doesn't mysteriously Not Work for them.

              Comment

              • Simon Hobbs

                #8
                Re: add text to input

                Simon Wrote:[color=blue][color=green]
                > > Oh, that was painless. I used 'onSubmit' to add the desired character
                > > to the string and put it in a hidden textbox.[/color]
                >[/color]
                Owen Wrote:[color=blue]
                > This will fail on browsers that do not support javascript or that have
                > it disabled. Doing it on the server is a much smarter plan. At least
                > make a note that this is going on for those users, so that searching
                > doesn't mysteriously Not Work for them.[/color]

                Fair concern, but it's quite OK in this case. I have 50 users who use
                whatever is imaged onto their laptops.

                S.

                Comment

                Working...