[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Populate hidden fields with JavaScript problem

Posted on 2010-08-19
2
Medium Priority
?
461 Views
Last Modified: 2012-06-27
Hi Guys,

First off I know nothing (about JavaScript)

What I would like to do is have a certain amount of clickable links, that once clicked a hidden form field within a HTML table cell is updated to the value of  ' 1 '.

So lets say there is 1 table row with 4 columns, each of these columns has a link in (1,2,3,4) and a hidden field.  Once a link is clicked the value of the hidden field in the same column as the link gets updated to ' 1 '

Then if another link is clicked the same happens.  

So when the ' Submit ' button is pressed there is a value of  ' 1 ' set to all of the hidden fields that the corresponding link was clicked.

I've attached some code, which may make more sense.  I've tried to use some JavaScript but it only seems to add 1 value to only 1 hidden field.

Many thanks

Resna

<?php
foreach($_POST as $key => $value){
echo $value;
}
?>

<html>
<head>

<script type="text/javascript">

function add(){

i = document.getElementById('link').value;

eval("document.form1." + i + ".value='1'")

}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="Untitled-3.php">
  <table width="600" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td><input type="hidden" name="s1" />
        <a href="#" id="link" value="s1" onclick="add()">3</a></td>
        
      <td><input type="hidden" name="s2" />
        <a href="#" id="link" value="s2" onclick="add()">4</a></td>
        
      <td><input type="hidden" name="s3" />
        <a href="#" id="link" value="s3" onclick="add()">5</a></td>
        
      <td><input type="hidden" name="s4" />
        <a href="#" id="link" value="s4" onclick="add()">6</a></td>
        
    </tr>
  </table>
  <input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>

Open in new window

0
Comment
Question by:Resna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Jonah11 earned 2000 total points
ID: 33481361
Your code is not working because all the links have the same ID.  

The easiest way to fix it, however, is to put the link number as an argument to your javascript function.  Please see attached code.  I also created a test function so you can see that it's working.

Hope that helps.
<html>
<head>

<script type="text/javascript">

function add(linkNum){
  document.form1["s" + linkNum].value=1;
}

function testUpdates() {
	for(i=1; i<=4; i++) {
		alert(document.form1["s" + i].value);
	}
}

</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="Untitled-3.php">
  <table width="600" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td><input type="hidden" name="s1" value="0"/>
        <a href="#" id="link1" value="s1" onclick="add(1)">3</a></td>
        
      <td><input type="hidden" name="s2"  value="0"/>
        <a href="#" id="link2" value="s2" onclick="add(2)">4</a></td>
        
      <td><input type="hidden" name="s3"  value="0"/>
        <a href="#" id="link3" value="s3" onclick="add(3)">5</a></td>
        
      <td><input type="hidden" name="s4"  value="0"/>
        <a href="#" id="link4" value="s4" onclick="add(4)">6</a></td>
        
    </tr>
  </table>
  <input type="submit" name="button" id="button" value="Submit" onclick="testUpdates()"/>
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:Resna
ID: 33481795
Thanks Jonah11

Works perfectly, and I understand where I went wrong!

Many thanks

Resna
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question