Solved

javascript value to html hidden input value

Posted on 2014-12-31
2
251 Views
Last Modified: 2014-12-31
On an e-commerce product page I have the add to basket button set to call a javascript function. The function creates 2 objects and for testing they are displayed in an alert box just before the last line of javascript fires the add to basket submit process.

alert(obj.image_id + ' AND ' + obj.image_name);
 
I've added 2 hidden input fields and I'm trying to set the value of these fields equal to the 2 javascript objects. Without any luck.

<html>
 <head> 
 <script>
  var req;
  function screenshot() {
	  $('#btncapture').val('Adding to cart...');
	  $('#btncapture').attr('disabled', 'disabled');
	  if (req) { req.abort(); } 
	  req = $.ajax({
		type: 'POST',
		url: 'screenshot.php',
		data: "containt="+$('#main-container').html().replace(/&/g, "%26"),
		cache: false,
		success: function(response){
			var obj = $.parseJSON(response);
			alert(obj.image_id + ' AND ' + obj.image_name);
			document.getElementById('newimage_id').innerHTML = obj.image_id;
			document.getElementById('newimage_name').innerHTML = obj.image_name;
			$('#PRODProducts').submit();
		}
	});
  }
</script>
</head>
<body>      
        <form NAME="PRODProducts" id="PRODProducts" method="post" action="Screen=BASK">
          <input type="hidden" name="Action" value="ADPR" />
          <input type="hidden" name="Product_Code" value="101" />
          <input type="hidden" name="newimage_id" id="newimage_id" value="overwrite this ID value" />
          <input type="hidden" name="newimage_name" id="newimage_name" value="overwrite this Name value" />
          <input type="hidden" name="containt" id="containt" value="" />
          <input id="btncapture" type="button" value="Add To Basket" onClick="screenshot()" class="wl-buttons" />
        </form>
</body>
</html>

Open in new window


Anyone have an idea of how to fix this?
Untitled.html
0
Comment
Question by:Gary Samuels
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40526072
use :
			document.getElementById('newimage_id').value = obj.image_id;
			document.getElementById('newimage_name').value = obj.image_name;

Open in new window

or because you've jQuery :
			$('#newimage_id').val( obj.image_id );
			$('#newimage_name').val( obj.image_name );

Open in new window

0
 

Author Closing Comment

by:Gary Samuels
ID: 40526081
works great, thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web experts ! HTML ! Javascript project for medical MRI sharing 24 49
how can i turn the array into an object 4 39
Ajax and PHP 9 29
Set time on Session (ASP) 14 21
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

809 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