View Full Version : how to pass javascript variable to <input type=hidden value>

Feb 13th, 2011, 12:47 PM
hi guyz i do have a problem in passing javascript variable to <input type=hidden value="">

here's my code:

while ($row = mysql_fetch_array($result))

<script type="text/javascript">
function viewcodec(){
var randomValueCodec = randomString(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
document.getElementById('commentMarkCodeCompCodec-'+<?php echo $row['p_id'];?>).innerHTML = randomValueCodec;
document.getElementById('commentMarkCodeComp-'+<?php echo $row['p_id'];?>).innerHTML = randomValueCodec;


<form action="" method="post" name="postsForms">
<div class="commentBox" align="right" id="commentBox-<?php echo $row['p_id'];?>" <?php echo (($comment_num_row) ? '' :'style="display:none"')?>>

<input type=text id="commentMarkname-<?php echo $row['p_id'];?>" name="commentmarkname" class="commentMarkname" size="35" maxlength="20">
<textarea class="commentMark" id="commentMark-<?php echo $row['p_id'];?>" name="commentmark" cols="60"></textarea>
<input type=text id="commentMarkcode-<?php echo $row['p_id'];?>" name="commentmarkcode" class="commentMarkcode" size="35" maxlength="20">
<br clear="all" />
<span id='commentMarkCodeCompCodec-<?php echo $row['p_id'];?>'><b><script type="text/javascript">viewcodec();</script></b></span>
<input type="hidden" id="commentMarkCodeComp-<?php echo $row['p_id'];?>" name="commentMarkCodeComp" value="">
<br clear="all" />
<br clear="all" />
<a id="SubmitComment" style="float:right" class="small button comment"> Comment</a>


Feb 13th, 2011, 12:56 PM
Why are you using innerHTML on an input? I suppose you would rather want to change the input’s value attribute’s value.

Feb 13th, 2011, 01:05 PM
thanks for the quick reply sir...

you mean like this?

document.getElementById('commentMarkCodeCompCodec-'+<?php echo $row['p_id'];?>).value(randomValueCodec);
document.getElementById('commentMarkCodeComp-'+<?php echo $row['p_id'];?>).value(randomValueCodec);

i tried already. it's just the same.. and it will not display to my span... :(

Feb 13th, 2011, 02:28 PM
This is the wrong forum.
Despite the similar sounding names, Java is not the same as Javascript.
Moving from Java forum to Javascript forum.

Feb 13th, 2011, 07:55 PM
document.getElementById('commentMarkCodeCompCodec-'+<?php echo $row['p_id'];?>).innerHTML = "<b>" + randomValueCodec + "</b>";
document.getElementById('commentMarkCodeComp-'+<?php echo $row['p_id'];?>).value = randomValueCodec;
Like that. I don't know anything about PHP, but provided that the id that you're referencing is correct that should work. Also, if the javascript is in the body of the page you must put it after the input. getElementById does not work if the element hasn't been created yet.

Edit: Just realized the first one is a span. This should fix it (again assuming the id references are correct).

<span id="commentMarkCodeCompCodec-<?php echo $row['p_id'];?>"></span>
<input type="hidden" id="commentMarkCodeComp-<?php echo $row['p_id'];?>" name="commentMarkCodeComp" value="">

<script type="text/javascript">viewcodec();</script>