Solved

javascript value to html hidden input value

Posted on 2014-12-31
2
237 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:garysamuels
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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:garysamuels
Comment Utility
works great, thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now