Solved

javascript value to html hidden input value

Posted on 2014-12-31
2
249 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

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