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.
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nap0leonCommented:
Can we see your code?
0
rgranlundAuthor 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

0
experts1Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

rgranlundAuthor Commented:
all that does is give me a list of the five input ids, not the chose values
0
Kim WalkerWeb Programmer/TechnicianCommented:
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?
0
rgranlundAuthor Commented:
@xmediaman:  How do you do that?
0
Kim WalkerWeb Programmer/TechnicianCommented:
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.
0
rgranlundAuthor 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
0
chaitu chaituCommented:
<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));"
0
experts1Commented:
Please post the full client side code for the form!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.