Javascript: assign value to hidden form element

Posted on 2017-08-14
Medium Priority
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 {
} else if (checknum==1){
	//alert("So Far So Good");
	document.getElementById("StockItem").value = 0; //Component
	alert ("Assembly Stock Item: " + document.getElementById("StockItem").value);
} else {

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

Open in new window

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

Open in new window

What am I missing?
Question by:Crxfrd
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
LVL 31

Accepted Solution

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


Author Closing Comment

ID: 42253501
Bingo! Thank you

Featured Post

Independent Software Vendors: 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

718 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