SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Write a variable to a form input value

    So I have a variable that I want to write as a form input value, if I use

    Code:
    <input name=fid_17 value=<script language="javascript">document.writeln(variable);</script>>
    I get "scr" in the input box.

    What is the correct way to write in the variable?

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't insert a script object into an attribute value like that.

    You need to assign the value with a function or something. There are several ways to do it. Here's one possibility
    Code:
    <script language="javascript" type="text/javascript">
    <!--
    function initInput()
    	{
    	var variable = somevalue;
    	document.forms[0].fid_17.value = variable;
    	}
    //-->
    </script>
    </head>
    
    <body onLoad="initInput()">
    	<form>
    	<input name="fid_17" id="fid_17" value="">
    	</form>
    </body>
    Oh, and get accustomed to surrounding your attribute values with "double quotes"
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much for the help, obviously you can see I'm a realy newbie when it comes to javascript. I could easily do all of this in php, but my work won't install it.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, I see. You were hoping to do this
    PHP Code:
    <input name="fid_17" value="<? echo $variable ?>" />
    Sorry, javascript takes a bit more work
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could document.write the entire line - btw.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  6. #6
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry beetle one more question, how could assign the variable to a select box instead of an input box, using your function above?

    Thanks again for your help.

  7. #7
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use that same method - and ie ( at any rate ) will use the value to select the right option.

    Failing that - for more compatability - change the selectedIndex:

    document.forms[0].elements['element'].selectedIndex = 5;

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  8. #8
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually using document.myForm._fid_14.value=variable leaves the select box blank. I would use your example of using the index value, but I know the actual value of the select box, not the index. Any other suggestions

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script language="javascript" type="text/javascript">
    <!--
    
    var variable = "option1";
    
    function initInput()
    	{
    	var selectObj = document.forms[0].fid_14;
    	selectObj.selectedIndex = getIndex(selectObj);
    	}
    
    function getIndex(tempObj)
    	{
    	for (var i=0; i<tempObj.length; i++)
    		if (tempObj.options[i].value == variable)
    			return i;
    	}
    
    //-->
    </script>
    </head>
    
    <body onLoad="initInput()">
    	<form>
    	<select name="fid_14">
    	<option value="option0">Option 0</option>
    	<option value="option1">Option 1</option>
    	<option value="option2">Option 2</option>
    	</select>
    	</form>
    </body>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes - you can loop throught the options to find the right one.

    Code:
    obj = document.forms[0].elements['select'];
    
    for (var i=0;i<obj.options.length;i++){
           if (obj.options[i].value == 'testing value'){
                obj.selectedIndex=i;
                break;
                }
           }
    Something like that(ish)

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  11. #11
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beetle - glad you're online...

    Give me a half hour or so - i'm working on a s**t hot
    bit of work to do with international locations...

    ( ie putting gatwick in a textbox and it coming back with all the options that might match )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  12. #12
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help guys I got it working. I have one more question I just don't understand about javascript...how can I use variables like in php <? echo $variable; ?>.

    I have a redirect link that I need to tag a couple variables to the end of.

    <INPUT type=hidden value=https://www.blahblah.com/info.html name=rdr>

    How can I write a javascript variable to the end so it will be

    <INPUT type=hidden value=https://www.blahblah.com/info.html?variable=value name=rdr>

    I'm confused

  13. #13
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With javacript, you can't directly include variables into the HTML. You have basically two options.

    1) Write the entire line with javascript's document.writeln() and insert variables where needed

    2) Perform everything with events calling functions

    Before i get started, you should know that '+ 'is the concat operator in javascript, versus the '.' for PHP.

    Example using #1 above
    Code:
    <script>
    document.write('<INPUT type=hidden value=https://www.blahblah.com/info.html?variable='+var1+' name=rdr>');
    </script>
    The above examples assumes that var1 is global.

    Example using #2 above
    Code:
    <script>
    function setHidden()
    	{
    	document.forms[0].rdr.value += '?variable=' + var1;
    	return true;
    	}
    </script>
    </head>
    <body>
    <form onSubmit="return setHidden()">
    <INPUT type=hidden value=https://www.blahblah.com/info.html name=rdr>
    </form>
    Again, this example assumes var1 is global

    Did I explain it all ok? Ask away if you have more questions.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  14. #14
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes it makes perfect sense...option one seems a lot easier. So I can use document.write anywhere in an html file as long and i place it within <script></script>...correct? Are there any other rules for document.write I should know about?

  15. #15
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Correct, anytime within <script></script> tags. However, it's standard to declare them this way <script language="javascript" type="text/javascript>. I don't know about you, but that's alotta typing for every script. So, skip that silly nonsense by including this META tag.
    <META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript" />

    Oh, and you should note the difference between
    1) document.write();
    2) document.writeln();
    Wherein #2 creates a linebreak in the run-time HTML, and #1 does not.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  16. #16
    SitePoint Zealot ant1832's Avatar
    Join Date
    Apr 2002
    Location
    Tucson, AZ
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php has a function urlencode to replace spaces with %20, does javasript have something like this.

  17. #17
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes!
    escape(string);
    -and-
    unescape(string);
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •