Hey Guys,

I have an issue with my checkbox and the values that are posted to the next page. The issue is that each time the checkbox is clicked it just adds another value. example - if i check it and uncheck it and check it again i will get 3 values instead of 1. So if the user makes a mistake and needs to uncheck it they end up with 2 values instead of empty. I use PHP to post between pages if that makes any difference.

Thanks:)

<script type="text/JavaScript">
function concat(one)
{
        if(one.checked) {
                var currval = document.getElementById('locality').value;
                var concatval = one.value;
                document.getElementById('locality').value=""+currval+concatval+"";
        }
}
</script>
<tr>	
<input type="hidden" name="locality" id="locality">
<td align="right"><b>Locality</b></td>
<td align="left">
<input type="checkbox" name="locality" value="NATIONAL " id="national" onClick="concat(this);">
NATIONAL
</td>
<td width="59">
<input type="checkbox" name="locality" value="NSW " id="nsw" onClick="concat(this);">
NSW
</td>
<td width="59">
<input type="checkbox" name="locality" value="QLD " id="qld" onClick="concat(this);">
QLD
</td>
<td width="58">
<input type="checkbox" name="locality" value="SA " id="sa" onClick="concat(this);">
SA
</td>
<td width="59">
<input type="checkbox" name="locality" value="TAS " id="tas" onClick="concat(this);">
TAS
</td>
<td width="59">
<input type="checkbox" name="locality" value="VIC " id="vic" onClick="concat(this);">
VIC
</td>
<td width="243">
<input type="checkbox" name="locality" value="WA " id="wa" onClick="concat(this);">
WA
</td>
</tr>

Use indexOf and replace methods in Javascript. What I would do is something like below

function concat(one)
{
	var currval = document.getElementById('locality').value;
	var concatval = one.value;
	if(one.checked)
	{
		if(currval.indexOf(one.value) == -1)
		{
			document.getElementById('locality').value=""+currval+concatval+"";
		}
	}
	else if(!one.checked)
	{
		if(currval.indexOf(one.value) != -1)
		{
			document.getElementById('locality').value=currval.replace(one.value,"");
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.