We help IT Professionals succeed at work.

javascript values

I have a piece of javascript that creates an array from form imput.  However, the array only updates if you hit refresh, it does not update upon submit.
Comment
Watch Question

Top Expert 2011

Commented:
Can we see your code?

Author

Commented:
The bracketed "IF" statements are from the CMS:

{if option_field == "ad_society_1" OR option_field == "ad_society_2" OR option_field == "ad_society_3" OR option_field == "ad_society_4" OR option_field == "ad_society_5"}
	 	
		{if option_field == "ad_society_1"}
	 	
			<tr>
				<td width="150"><span class="red">*</span><label>{option_label}</label></td>
				<td width="250">{select id="societyonea"}<option {selected} value="{option_value}">{option_name}</option>{/select}</td>
				<td width="150"><div id="society_div"> </div></td>
			</tr>
		{/if}
		
		
		{if option_field == "ad_society_2"}
	 	
			<tr>
				<td width="150"><label>{option_label}</label></td>
				<td width="250">{select id="societytwoa"}<option {selected} value="{option_value}">{option_value}</option>{/select}</td>
				<td width="150"><div id="society_div"> </div></td>
			</tr>
																		
		{/if}
		
		{if option_field == "ad_society_3"}
	 	
			<tr>
				<td width="150"><label>{option_label}</label></td>
				<td width="250">{select id="societythreea"}<option {selected} value="{option_value}">{option_value}</option>{/select}</td>
				<td width="150"><div id="society_div"> </div></td>
			</tr>

		{/if}
		
		{if option_field == "ad_society_4"}
	 	
			<tr>
				<td width="150"><label>{option_label}</label></td>
				<td width="250">{select id="societyfoura"}<option {selected} value="{option_value}">{option_value}</option>{/select}</td>
				<td width="150"><div id="society_div"> </div></td>
			</tr>

		{/if}
		
		{if option_field == "ad_society_5"}
	 	
			<tr>
				<td width="150"><label>{option_label}</label></td>
				<td width="250">{select id="societyfivea"}<option {selected} value="{option_value}">{option_value}</option>{/select}</td>
				<td width="150"><div id="society_div"> </div></td>
			</tr>
<script type="text/javascript">         
var arr3 = ['societyonea','societytwoa','societythreea','societyfoura','societyfivea'];
var arr4 = new Array();
for(var i=0;i<arr3.length;i++){
    arr4[i] = $('#'+arr3[i]).val();
}
var societyb = arr4.join(',');
document.write('<input type="hidden" id="hiddenelementb" name="custom_data[society_search_1]" value="'+societyb+'">');
document.write(societyb);
</script>	
		{/if}

	{/if}

Open in new window

Commented:
Modify your javascript as follows:


<script type="text/javascript">         
var arr3 = ['societyonea','societytwoa','societythreea','societyfoura','societyfivea'];
var arr4 = new Array();
var societyb = '';
for(var i=0;i<arr3.length;i++){
    arr4[i] = '#' + arr3[i];
    societyb = societyb + arr4[i]; 
}
document.write('<input type="hidden" id="hiddenelementb" name="custom_data[society_search_1]" value="'+societyb+'">');
document.write(societyb);
</script>

Open in new window

Author

Commented:
all that does is give me a list of the five input ids, not the chose values
Kim WalkerWeb Programmer/Technician

Commented:
It looks like your script is only being executed in-line when the page loads. Is your submit button set up to execute the script again when clicked?

Author

Commented:
@xmediaman:  How do you do that?
Kim WalkerWeb Programmer/Technician

Commented:
There are actually several ways...

with an onclick event on the submit button
<input type="submit" value="SUBMIT" id="submitBtn" onclick="javascript: executeCode();">

Open in new window


with an onsubmit event on the form
<form name="form1" action="/#' method="post" onsubmit="javascript: executeCode();">

Open in new window


or using the DOM in your javascript
document.form1.submitBtn.onsubmit = executeCode;

Open in new window


With either of these situations, though, your script would need to be in a function to be called at the appropriate time. As it is now, it can only execute when the page loads.

Author

Commented:
What do you think is wrong with the:

<form id="update_cart_form" onsubmit="javascript: executeCode(); && return validateForm(this);" action="MY ACTION" method="post">

It will update but skips validation
<form id="update_cart_form" onsubmit="executeCode(); return validateForm(this);" action="MY ACTION" method="post">


or try this


<form id="update_cart_form" onsubmit="return (executeCode() && validateForm(this));"

Commented:
Please post the full client side code for the form!