Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript: assign value to hidden form element

Posted on 2017-08-14
2
Medium Priority
?
45 Views
Last Modified: 2017-08-14
Thank you for looking at my question,

I have a user input form (php) that has some javascript validation which should, if the conditions are met, assign a value to a hidden form element but it isn't working and I can't see why.
	<form name="Form1" action="Forms/Section1.php" method="GET" onSubmit="return jsValidate()">
		<input type="hidden" name="StockItem" />

Open in new window


The javascript validation looks like this
if (z.length == 23){
	if (z.slice(0,2) == "VE" || z.slice(0,2) == "SE"){
		
		//alert("So Far So Good");
		
		document.getElementById("StockItem").value = 1; //Full Build
		alert (document.getElementById("StockItem").value);
	} else {
		errorcount++;
		document.getElementById('VECO').style.color='#FF0000';
	}
} else if (checknum==1){
	
	//alert("So Far So Good");
	
	document.getElementById("StockItem").value = 0; //Component
	alert ("Assembly Stock Item: " + document.getElementById("StockItem").value);
} else {
	errorcount++;
	document.getElementById('VECO').style.color='#FF0000';
}

Open in new window


If either of the conditions are met (z == 23 characters starting VE or SE, or checknum == 1)  then, when it's not commented out, the "So Far So Good" alert appears on the screen but the following
alert(document.getElementById("StockItem").value);

Open in new window

doesn't
and no value is assigned to the StockItem element - as can be seen in the address of the php page the data should pass to
http://xx.xx.xx.xx/ed1/Forms/Section1.php?StockItem=&Meter=1000&ProdnOrder=553456789&SalesOrder=9999&SerialNo=&VECode=VE884S05E0G0T01D0004000

Open in new window


What am I missing?
0
Comment
Question by:Crxfrd
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 42253496
The element you want assign a value to has no id, onky the name. Just change it to this
	<form name="Form1" action="Forms/Section1.php" method="GET" onSubmit="return jsValidate()">
		<input type="hidden" name="StockItem" id="StockItem" />

Open in new window

0
 

Author Closing Comment

by:Crxfrd
ID: 42253501
Bingo! Thank you
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

885 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