TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-24-2007, 10:52 PM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default Are Operators Valid?

Hey,

Quick question. My site currently has 4 invalid HTML errors. It's all down to this function:

Code:
// Error checking
$('send_pm').onsubmit = function() {
valid = $(this.to).present() && $(this.subject).present() && $(this.message).present();
			
if(valid) {
	return true;
}
else {
	alert('Please fill in all fields');
}
	return false;
}
The validator is saying that the && operator is the cause of the error. Their's 4 errors, and 4 &'s.

I'm guessing it's my code, perhaps it's not wrote out correctly, though it does work. Does anybody have any suggestions? BTW: I'm using PrototypeJS

Cheers,

- Chris.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 09-24-2007, 11:26 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

&& in that context seems totally wrong. You're assigning the values and && is for checking, such as like: if kiwi is kiwi AND banana is banana. If you're attempting to concatenate the values then the plus symbol is the way to go with javascript:

Code:
valid = $(this.to).present() + $(this.subject).present() + $(this.message).present();
Although one & does work as well, I believe.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-24-2007, 11:47 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

No, in this case the && is correct. If you look at the code, it's checking if various fields are 'present' and assigning the valid variable if they all are.

The HTML validation errors are caused by the ampersands (&) in your JavaScript but you should not change them to anything else. Instead, it is advisable (not just because of validation) to put your JavaScript in its own file and reference it using a <script> HTML element.
Salathe is offline  
Reply With Quote
Old 09-24-2007, 11:53 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Im not sure present is a function in Protoype, try changing present() to empty(), you'll have to negate each condition too.

Code:
valid = !$(this.to).empty() && !$(this.subject).empty() && !$(this.message).empty();
Karl is offline  
Reply With Quote
Old 09-25-2007, 12:21 AM   #5 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

I think having the code in an external file, or maybe even just between the <head> tags might change the outcome.

Karl, hi... present() is a Form Element in Prototype. It has an example code, which I pretty much copied, but used an alert() instead of displaying some text. See the document (http://www.prototypejs.org/api/form/element/present)
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 09-25-2007, 07:20 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

If you're wanting to keep the code within the HTML document, you'll need to tell the validator not to check the script by making it a CDATA block (that's XML terminology). The two "commented" (//) lines are what's important here.

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

alert("This is compatible with XHTML");

//]]>
</script>
Salathe is offline  
Reply With Quote
Old 09-25-2007, 09:17 PM   #7 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Cool, I'll have a stab at it.

Cheers.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 09-25-2007, 09:49 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I've never been a big fan of CDATA as I think it looks somewhat messy, but it serves a purpose so why the hell not :) ! Everything in external files, for me.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:07 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design