Jump to content

Form elements into string with Prototype JS


Firemankurt

Recommended Posts

I am a PHP guy struggling with JS.

 

I dynamically create forms with several fields that can change names at any time.  I need to gather all form input names and values into a string that I can then send in one $_GET element?  I will separate them out again in the PHP that receives the $_GET element.

 

Here is the section of my code giving me trouble. I simplified to reduce the possibility of masking one problem with another.

 

<html>
<head>
<title>FD Manual</title>
<script src="http://train.iaff106.org/js/scriptaculous/prototype.js" type="text/javascript"></script> 
<script src="http://train.iaff106.org/js/scriptaculous/scriptaculous.js" type="text/javascript"></script>

<script type="text/javascript">
NewSlidePreviewRequest = function (typeid)
{
	var newdata = $('S').serialize(true);
	alert(newdata);

	var jsonstr= JSON.stringify(newdata);	
	alert(jsonstr);
                // AJAX stuff sends string to PHP receiver for parsing and response
}
</script>
</head>

<body>
<form id="S">
<label for="ImgTopCaption">Image Top Caption</label>
<input type="text" id="ImgTopCaption" value="Image Top Caption">

<label for="Text">Text</label>
<textarea id="Text">This is my text</textarea>

<a href="#" onclick="NewSlidePreviewRequest(1)" >Click HERE</a>
</form>

</body>
</html>

 

I was expecting one of those alerts to contain a string like:

{ImgTopCaption: 'Image Top Caption', Text: 'This is my text'}

 

But not working like I hoped.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.