Solved

Submit javascript code snippet via web form field

Posted on 2011-02-25
7
371 Views
Last Modified: 2012-05-11
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
Comment
Question by:BigFriendlyGiant
7 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 34985718
Hi BigFriendlyGiant,

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

0
 
LVL 33

Expert Comment

by:Slick812
ID: 34987704
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34992368
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:BigFriendlyGiant
ID: 34992622
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
 

Accepted Solution

by:
BigFriendlyGiant earned 0 total points
ID: 34992640
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
 
LVL 33

Expert Comment

by:Slick812
ID: 34992706
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
 

Author Closing Comment

by:BigFriendlyGiant
ID: 35067688
No other solution given perhaps due to me finding it difficult to explain clearly what the problem was.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …

760 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

22 Experts available now in Live!

Get 1:1 Help Now