Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very dynamic form validation

Status
Not open for further replies.

ca4928

Programmer
Jan 4, 2005
13
GB
I am trying to create a very dynamic form using AJAX. My AJAX function eval()s the responseText of the request as this is the best way of doing several other things.

I am writing this to input data into a database, and I would like to only use one function in php to input data into each of several tables. This means that I need to dynamically load both the text boxes and their validation rules.

Because i am eval()ing the responseText, I need my servder to return something like this.

Code:
formnode.innerHTML=' \
  <form method="" action="" onsubmit="return validate(this);"> \
    //4 or 5 text boxes here
    <input type="submit" value="Save"> \
  </form> ';

var validate=new function(frm) { 
    //for each text box validate it e.g.
    if(frm.name.value=='')alert('Please Input Name');

    //then submit data using AJAX
    AJAX_processform(frm);
    return false;
};

The problem I am having is that, instead of creating the function validate, it runs the code from the function as it eval()s.

Any help would be appreciated.
 
Instead of using eval, try setting the innerHTML of a dummy div, writing some script tags out, with your code inside.

Something like this:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--

		// your script here
		var myDiv = document.getElementById('dummyScriptDiv');
		myDiv.innerHTML = '<script type="text\/javascript">' + yourReturnedHTML + '<\/script>';

	//-->
	</script>
</head>
<body>

	<!-- your html here -->

	<div id="dummyScriptDiv"></div>
</body>
</html>

I've not tried it, but it's worth a crack.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thank's, I have managed to find the problem, for some reason, if you put the "new" in "validate=new function", it doesn't work. I removed the new, and it seems to be working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top