passing params to html-form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Holger Butschek

    #1

    passing params to html-form

    Hi folks,

    first of all, I'm absolutly new to javascript. I have designed a html-form
    and am trying to path parameters with the suffix of i.e.
    ?Seminartitel=h allowelt in the url.

    In the head of the called page I put the following script (I found it some
    where, many thanks to him):

    <script language="JavaS cript">

    function GetUrlValue(Ent ry)
    {
    urlText=locatio n.search;
    fc=urlText.slic e(0,1);
    if(fc=="?")
    {
    l=urlText.lengt h;
    urlText=urlText .slice(1,l);
    ArgSet=urlText. split("&");
    for(i=0;i<ArgSe t.length;i++)
    {
    as=ArgSet[i].split("=");
    if(as[0]==Entry)
    {
    return (as[1]);
    }
    }
    return ("");
    }
    }
    </script>

    Now, I would like to preset the field Seminar of the form with the value
    Seminartitel.

    <input type="text" size="40" name="Seminar" value=" ... xxxxxxxx ......">

    Many thanks,
    Holger Butschek

    --
    Erstellt mit M2, Operas revolutionärem E-Mail-Modul:
    Opera is a secure, innovative browser used by millions around the world with a built-in ad blocker, free VPN, units converter, social messengers, battery saver and much more - all for your best browsing experience. Download Opera browser now and enjoy the Internet once again. Want to know more? Visit opera.com and discover yourself.

  • Robert

    #2
    Re: passing params to html-form

    In article <opsdz7rxd4ofq5 zp@news.butsche k.com>,
    Holger Butschek <holger@butsche k.com> wrote:
    [color=blue]
    > I have designed a html-form
    > and am trying to path parameters with the suffix of i.e.
    > ?Seminartitel=h allowelt in the url.[/color]
    ....[color=blue]
    > Now, I would like to preset the field Seminar of the form with the value
    > Seminartitel.
    >[/color]


    Try invoking with formfillin.html ?Seminartitel=p ass

    file: formfillin.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Form</title>
    <SCRIPT type="text/javascript">
    /*

    Most folks lower case the first word in the name of
    functions and variables.

    Using an upper case letter
    in the first word is reserved for functions which
    initialize class members:
    var myCar = new Car("VW");

    Anyway, be consistant.

    */
    function getUrlValue(ent ry)
    {
    /* Without the var, you are declaring this
    variable a global variable. */
    var urlText=locatio n.search;
    var fc=urlText.slic e(0,1);
    if (fc=="?")
    {
    var l=urlText.lengt h;
    urlText=urlText .slice(1,l);
    var argSet=urlText. split("&");
    for (var i=0;i<argSet.le ngth;i++)
    {
    var as=argSet[i].split("=");
    if (as[0]==entry)
    {
    return (as[1]);
    }
    }

    }
    /*
    Moved null return in case nothing was passed
    */
    return ("");
    }

    /*
    Although not part of your question, I have
    included a short validation routine.
    */
    function validate()
    {

    var x = document.forms["myForm"];
    var submitOK = true;
    var messageText =
    "What a suprise! Please correct the error in your ways." +
    " Please remove the asterisk ( * ) in the field.";


    if (x.seminar.valu e.indexOf("*") >= 0)
    {
    alert(messageTe xt);
    submitOK = false;
    }

    return submitOK;

    }
    </script>
    </head>
    <body>
    <p>Retrieve data passed as search value.</p>
    <form name="myForm"
    action="http://www.natAValidWe bAddress.com"
    method="POST"
    onsubmit="retur n validate();">

    <SCRIPT type="text/javascript">
    var input = getUrlValue("Se minartitel");
    /*
    document.write will paste data into the document
    as the document is loaded.
    */
    document.write(
    "<input type=\"text\" size=\"40\" name=\"seminar\ "" +
    "value=\"" + input + "\">");
    </script>
    <p><input type="submit"></p>
    </form>
    </body>
    </html>




    This line will show you the generated html plus everything else:
    javascript:'<ht ml><head><title >SourceFileList ing<\/title><\/head><body><c
    ode><ol><li>'+( document.docume ntElement||docu ment.body).inne rHTML.replace
    (/&/g,"&amp;").repl ace(/</g,"&lt;").repla ce(/%20%20/g,"&nbsp;%20"). replac
    e(/\r\n/g,"<li>").repla ce(/\n/g,"<li>").repla ce(/\r/g,"<li>")+'<\/ol><\/c
    ode><\/body><\/html>';

    It wrapped! In IE, you can change innerHTML to outerHTML.

    Robert

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: passing params to html-form

      Robert wrote:
      [color=blue]
      > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">[/color]

      The system identifier (the URL of the DTD) is missing in this declaration.
      It should be

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      [color=blue]
      > [...]
      > <SCRIPT type="text/javascript">
      > /*
      >
      > Most folks lower case the first word in the name of
      > functions and variables.[/color]

      True.
      [color=blue]
      > Using an upper case letter
      > in the first word is reserved for functions which
      > initialize class members:
      > var myCar = new Car("VW");[/color]

      That is a *constructor*. Using the "new" keyword when calling it, returns
      a reference to an object, not to a "class member". There are no classes
      in ECMAScript < 4, JavaScript < 2 and JScript < 6, those are languages
      implementing only prototype-based inheritance.
      [color=blue]
      > Anyway, be consistant.[/color]

      ACK
      [color=blue]
      > <form name="myForm"[/color]

      A "name" attribute is not required here ...
      [color=blue]
      > action="http://www.natAValidWe bAddress.com"
      > method="POST"
      > onsubmit="retur n validate();">[/color]

      .... if you pass a reference to the calling object:

      function validate(x)
      {
      // This is no longer required then:
       // var x = document.forms["myForm"];
      ...
      }
      ....
      onsubmit="retur n validate(this); ">
      [color=blue]
      > [...]
      > document.write(
      > "<input type=\"text\" size=\"40\" name=\"seminar\ "" +
      > "value=\"" + input + "\">");[/color]

      Early JavaScript versions, and ECMAScript implementations allow for using
      both the " and the ' character to be used as string literal delimiter. To
      make use of that makes especially DHTML easier to read and to maintain.
      More, some HTML attributes have default values and thus can be omitted:

      document.write( '<input name="seminar" value="' + input + '" size="40">');
      [color=blue]
      > [...]
      > <p><input type="submit"></p>[/color]

      This is not a paragraph, use the "div" element instead of the "p" element.


      PointedEars

      Comment

      Working...