putting variables into "value" of "INPUT"

  • Carpdung
  • Newbie
  • Newbie
  • Carpdung
  • Posts: 11

Post 3+ Months Ago

Hi all,

Using HTML, I have a form that has autofilled entries from a link:
(ie. http://some.url.com/somefile.html?x=1)

The way I output the variables (Thanks Leo!) is like:
Code: [ Select ]
<tr> <td ALIGN="left">x: </td>
    <td><script>
       var args = parseQueryString ();
       document.write(args['x']);
        </script>
    </td>
</tr>
  1. <tr> <td ALIGN="left">x: </td>
  2.     <td><script>
  3.        var args = parseQueryString ();
  4.        document.write(args['x']);
  5.         </script>
  6.     </td>
  7. </tr>


The autofilled entries are Fields within a form and "x=1" outputs nicely.

However, I really need to capture the value from the

"document.write(args['x']);"

line. How can I do this?

If I could just find some way to capture the "document.write" output into the "value" of the INPUT Tag, all my problems will be solved.
So basically,
Code: [ Select ]
<input type="hidden" name="x" value=???>

I don't know how to place that into the value??
Can someone help??

Thanks!
-carpdung
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Ok, here you have your form :)

http://www.drk.com.ar/spider/show_autof ... Leo&age=27
http://www.drk.com.ar/spider/show_autof ... ohn&age=12
http://www.drk.com.ar/spider/show_autof ... l?name=Ted
http://www.drk.com.ar/spider/show_autof ... ?age=99999

Code: [ Select ]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Autofill demo by _Leo_</title>
    <link href="/base.css" rel="stylesheet" type="text/css">
    <script language="JavaScript" type="text/javascript">
    
    // This function will parse HTTP GET string for variable/value pairs
    function parseQueryString(str) {
     str = str ? str : location.search;
     var query = str.charAt(0) == '?' ? str.substring(1) : str;
     var args = new Object();
     if (query) {
      var fields = query.split('&');
      for (var f = 0; f < fields.length; f++) {
       var field = fields[f].split('=');
       args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
      }
     }
     return args;
    }
    
    // Ok, let's make args array globally availabe
    var args = parseQueryString();
    
    // This function will autofill form fields values
    function autofill() {
        if (args['name'])
            document.forms.namedItem('my_form').elements.namedItem('name').value = args['name'];
        if (args['age'])
            document.forms.namedItem('my_form').elements.namedItem('age').value = args['age'];
    }
    
    </script>
</head>

<!-- Call autofill() function once document is loaded -->
<body onLoad="autofill();">
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="height: 100%;">
<tr>
    <td height="100%" align="center" valign="middle">

        <form action="show_autofill.html" name="my_form" id="my_form">
        <table cellspacing="2" cellpadding="2" border="0">
        <tr>
            <td>Name:</td>
            <td><input type="text" name="name" size="25"></td>
        </tr>
        <tr>
            <td>Age</td>
            <td><input type="text" name="age" size="25"></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><input type="submit" name="submit" value="Send"></td>
        </tr>
        </table>
        </form>

    </td>
</tr>
</table>
</body>
</html>
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4.     <title>Autofill demo by _Leo_</title>
  5.     <link href="/base.css" rel="stylesheet" type="text/css">
  6.     <script language="JavaScript" type="text/javascript">
  7.     
  8.     // This function will parse HTTP GET string for variable/value pairs
  9.     function parseQueryString(str) {
  10.      str = str ? str : location.search;
  11.      var query = str.charAt(0) == '?' ? str.substring(1) : str;
  12.      var args = new Object();
  13.      if (query) {
  14.       var fields = query.split('&');
  15.       for (var f = 0; f < fields.length; f++) {
  16.        var field = fields[f].split('=');
  17.        args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
  18.       }
  19.      }
  20.      return args;
  21.     }
  22.     
  23.     // Ok, let's make args array globally availabe
  24.     var args = parseQueryString();
  25.     
  26.     // This function will autofill form fields values
  27.     function autofill() {
  28.         if (args['name'])
  29.             document.forms.namedItem('my_form').elements.namedItem('name').value = args['name'];
  30.         if (args['age'])
  31.             document.forms.namedItem('my_form').elements.namedItem('age').value = args['age'];
  32.     }
  33.     
  34.     </script>
  35. </head>
  36. <!-- Call autofill() function once document is loaded -->
  37. <body onLoad="autofill();">
  38. <table width="100%" border="0" cellspacing="0" cellpadding="0" style="height: 100%;">
  39. <tr>
  40.     <td height="100%" align="center" valign="middle">
  41.         <form action="show_autofill.html" name="my_form" id="my_form">
  42.         <table cellspacing="2" cellpadding="2" border="0">
  43.         <tr>
  44.             <td>Name:</td>
  45.             <td><input type="text" name="name" size="25"></td>
  46.         </tr>
  47.         <tr>
  48.             <td>Age</td>
  49.             <td><input type="text" name="age" size="25"></td>
  50.         </tr>
  51.         <tr>
  52.             <td colspan="2" align="right"><input type="submit" name="submit" value="Send"></td>
  53.         </tr>
  54.         </table>
  55.         </form>
  56.     </td>
  57. </tr>
  58. </table>
  59. </body>
  60. </html>
  • Alderak
  • Novice
  • Novice
  • User avatar
  • Posts: 28
  • Loc: Australia, Vic, Melbourne

Post 3+ Months Ago

I' not sure exactly ive only done this on Q basic 8) what it does there is when you put that in it acts much like a bookmak input a certain Value and it goes to that tag. I havn't seen this before in forms so sorry i thought that might help.
  • Carpdung
  • Newbie
  • Newbie
  • Carpdung
  • Posts: 11

Post 3+ Months Ago

Hi Leo,

I tried the solution you provided. I'm still having an issue. The FORM that I'm building has an action that calls a PERL script.
Code: [ Select ]
<form action="http://somename.com/cgi-bin/mailtocod.pl" method="POST">


I noticed in your code the action in the FORM was defined as "show_autofill.html". First I tested the form you gave and it worked nicely....just like your examples. :) However, I noticed that this method is not allowing the Perl script to get the values. So...I tried to imbed your Form into my Form:
Code: [ Select ]
<form action="http://somename.com/cgi-bin/mailtocod.pl" method="POST">
                   ...
   <form action="show_autofill.html" name="my_form" id="my_form">                  
     <tr>
     <td>Test Ticket:</td>
     <td>
        <input type="text" name="ticket_number" size="25"></td>
   </tr>
   <tr>
     <td>Test Last Name:</td>
     <td><input type="text" name="last" size="25"></td>
   </tr>
   <tr>
     <td colspan="2" align="right"><!-- input type="submit" name="submit" value="Send" --></td>
   </tr>
</form>
...
</form>
  1. <form action="http://somename.com/cgi-bin/mailtocod.pl" method="POST">
  2.                    ...
  3.    <form action="show_autofill.html" name="my_form" id="my_form">                  
  4.      <tr>
  5.      <td>Test Ticket:</td>
  6.      <td>
  7.         <input type="text" name="ticket_number" size="25"></td>
  8.    </tr>
  9.    <tr>
  10.      <td>Test Last Name:</td>
  11.      <td><input type="text" name="last" size="25"></td>
  12.    </tr>
  13.    <tr>
  14.      <td colspan="2" align="right"><!-- input type="submit" name="submit" value="Send" --></td>
  15.    </tr>
  16. </form>
  17. ...
  18. </form>


The Text appears but the values are blank again?? I tried constructing it so that it wasn't in a Form (since the "autofill();" is called in the body). But the values were still empty.

How can I get the values into the Form where "action=.../someperl.pl". Basically, when the End User selects/clicks the "Submit" button, the perl script is called and sucks-in those INPUT TAG values for me to display on an email. I can generate the variables on screen as in your examples, but I still can't get the Perl Script to get these values via $in{x='var'}; method.

Long story short, your form works. But my form has a different action, BUT I need the values that your form generates in my form so that the Perl script can use them. I'm almost there and it's killing me. :( :( :(

Thanks!
-Carpdung
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

No no no no no.....

You have to replace the action attribute of the form of my example with the actual URL of the script you need to call. I user the same HTML just because, and of course, there is no need to do that.

You have to leave just one opening FORM tag like this:

Code: [ Select ]
<form action="http://somename.com/cgi-bin/mailtocod.pl" method="POST" name="my_form" id="my_form">
  • Carpdung
  • Newbie
  • Newbie
  • Carpdung
  • Posts: 11

Post 3+ Months Ago

Obviously I have a lack of understanding of how this structure works. I chuckle at myself when I make stabs at something, and it was totally in the wrong direction. Thank goodness there's people like you who can keep me straight! :P :D :lol:

I modified the "Form" line to keep the call to the perl script and added the elements that you suggested. The whole thing worked out well!! I am so relieved! To make it cleaner, I made the INPUT types "hidden" so that they would be transparent...and only show up on the email. Everything in the world is fine again. :) :shock: :)

Thank you Thank you Thank you!!!!

-Carpdung 8)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: Liamw411 and 139 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.