954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using JS validation within PHP tags

Hi there,
This may or may not be a dumb question, but I've never come across it before.
Can you point to a javascript 'onsubmit="return validation()"' on a form, but WITHIN PHP tags?

Such as the below.

function validation(){
	
var reply = document.getElementById('comreply');
var div = document.getElementById('errormsg');

if((reply.value == '') || (reply.value == ' ')){
	div.innerHTML = "Please type a reply";
	reply.focus();
	return false;}
else{
	return true;}
}
<? echo "<form name=\"reply\" method=\"post\" action=\"replydone.php\" onsubmit=\"return validation()\"><textarea name=\"comreply\" id=\"comreply\" cols=\"25\" rows=\"4\"></textarea></form> ?>


Thanks for all replies.

ben.marks
Newbie Poster
21 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Hi,

In your example, you're essentially just outputting a string of HTML, with inline JavaScript to the browser, so yes, you can do that.

That seemed quite simple (only if you know, of course), but does that answer your question, or have I misunderstood?

R.

blocblue
Posting Pro in Training
475 posts since Jan 2008
Reputation Points: 142
Solved Threads: 79
 

Thanks for the reply blocblue...

you see thats what I thought at first and that it shouldnt have made any difference at all as it was all correct, just in PHP tags, however when putting it all in and testing it with nothing in the text area box, no errormsg was displayed and the form submitted anyway, instead of returning false.

ben.marks
Newbie Poster
21 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
ben.marks
Newbie Poster
21 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: