• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Submit javascript code snippet via web form field

Having built a small CMS system, I am adding a form that will let users submit their own HTML code snippets which they can then add to their web page content via the CMS. This all works fine apart from one problem, when the code snippet to be submitted includes code for a form with javascript validation, the original form doesn't submit.

I need to know if there is a way have the code entered into the form field 'shielded' so that it doesn't affect the form that is being used to submit that code.

Hoping that someone can help.

Thank you.
0
BigFriendlyGiant
Asked:
BigFriendlyGiant
1 Solution
 
Jason C. LevineNo oneCommented:
Hi BigFriendlyGiant,

What kind of escaping are you doing with the form field?

0
 
Slick812Commented:
greetings BigFriendlyGiant, , ,  I have had problems similar to what you describe is happening in your javascript "validation" and other functions that are "added later" to the existing javascript variables and functions entries already on a page. Many times common functions and variables will try and use the same names that are already in use. I would guess that the added javascript validation function uses the same function name that you use, so "the original form doesn't submit" because there may now be two functions with that name. What I do now, is to add 2 or 3 numbers to the end all of my javascript function names that have common names , like this -

function validate(id) {

}

change to - -

function validate42(id) {


}

you should check on your variable and function names and rename all of the ones that have commonly used names like ID, validate, checkForm, name, user, and many others.
0
 
Ray PaseurCommented:
As a general rule, anything that a client puts into the textarea should come through into the action script, where you would escape it with mysql_real_escape_string() and store it in your data base, unmodified in any way.  When you get ready to echo this to the browser, you would use htmlentities() to reduce the risk of injected code.

If you want to show us the code that is causing you trouble we may be able to offer more specific guidance.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
BigFriendlyGiantAuthor Commented:
Hi,

jason1178, I use mysql_real_escape_string. but I do not believe the problem lies in the validation or what happens server side when the form is submitted. I think that the problem happens client side and that the form used to submit the code snippets tries to use the javascript validation submitted in the code snippet itself.

Slick812, that is almost correct, apart that the fields names in the form submitted in the code snippet do not correspond to the field names in the form used to submit the code snippet. My guess is that as the code snippet is in itself a form with javascript validation, it will not submit as the fields in the code snippets are empty, i.e. not valid as per the validation in the code snippet if that makes sense. The problem is that the code snippet being submitted is a subscription HTML form that my clients are likely to try to use on their sites often and add themselves via the form to submit code snippets.

Ray_Paseur, as per my response to jason1178, I believe the problem lies before the form data reaches the code to escape the data or to validate it.

A copy of the code being submitted via the form as code snippet is attached.

Thanks.
<!--
Do not modify the NAME value of any of the INPUT fields
the FORM action, or any of the hidden fields (eg. input type=hidden).
These are all required for this form to function correctly.
-->
<form method="post" action="abcxyz" id="frmSS5" onsubmit="return CheckForm5(this);">
	<table border="0">
		<tr>
	<td><span class="required">*</span>&nbsp;
Your Email Address:</td>
	<td><input type="text" name="email" value="" /></td>
</tr><tr>
	<td><span class="required">*</span>&nbsp;
Preferred Format:</td>
	<td><select name="format"><option value="h">HTML</option><option value="t">Text</option></select></td>
</tr>
	</table>
</form>

<script type="text/javascript">
// <![CDATA[

			function CheckMultiple5(frm, name) {
				for (var i=0; i < frm.length; i++)
				{
					fldObj = frm.elements[i];
					fldId = fldObj.id;
					if (fldId) {
						var fieldnamecheck=fldObj.id.indexOf(name);
						if (fieldnamecheck != -1) {
							if (fldObj.checked) {
								return true;
							}
						}
					}
				}
				return false;
			}
		function CheckForm5(f) {
			var email_re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
			if (!email_re.test(f.email.value)) {
				alert("Please enter your email address.");
				f.email.focus();
				return false;
			}
		
						if (f.format.selectedIndex == -1) {
							alert("Please choose a format to receive your email campaigns in");
							f.format.focus();
							return false;
						}
					
				return true;
			}
		
// ]]>
</script>

Open in new window

0
 
BigFriendlyGiantAuthor Commented:
I have resolved the problem by making it possible for my clients to upload the code as the content of a file which is then read via a PHP script which takes that content and writes it to the database. A roundabout way to do it but it works for now.
0
 
Slick812Commented:
It is so difficult to try and "guess" what your problem may or may not be, especially with "User Submitted" html and javascript to be added to a page. I am glad that you have got something to work, by using a file to store content, and reading it to fix whatever conflict that occurred before, , , file writes and reads are rapid and if you can delete the file after reading, this will probally be a solution. However, if you need anymore help, I would say you will need to show us the user submitted code that did not work.
0
 
BigFriendlyGiantAuthor Commented:
No other solution given perhaps due to me finding it difficult to explain clearly what the problem was.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now