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 10-29-2009, 08:50 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default Building Javascript PHP problem

Hi I'm having difficulty with a display class i've created and how it prints out javascript. I am finding that when i build the javascript using a method in the class then call the method to print out the javascript when building the page html the javascript subsequently wont work however when i simply include the same javascript from an include file i can get exactly the same javascript to work. I'm sure its how its displaying as html but dont know how to resolve or control how the browser formats it. Any guidance?

Here's how the javascript looks in the include file (this works):

PHP Code:
<script language='javascript'>
function 
inviteFormVerifyMe(){
var 
msg='';

if(
document.getElementById('email').value==''){
    
msg+='- Email\n\n';}

if(
document.getElementById('retypeemail').value==''){
    
msg+='- Retype Email\n\n';}

if(
msg!=''){
    
alert('The following fields are empty or invalid:\n\n'+msg);
    return 
false
}else{
    return 
true }

}
</script> 
Here's how it looks when produced by the php class and the html is viewed in the browser (it doesnt work):

PHP Code:
<script language='javascript'>function inviteFormVerifyMe(){var msg='';if(document.getElementById('email').value==''){msg+='- Email

'
;}if(document.getElementById('retypeemail').value==''){msg+='- Retype Email

'
;}if(msg!=''){alert('The following fields are empty or invalid:

'
+msg);return false}else{return true}}</script> 
captainmerton is offline  
Reply With Quote
Old 10-30-2009, 05:17 PM   #2 (permalink)
The Visitor
 
Join Date: Oct 2009
Location: Richmond VA
Posts: 1
Thanks: 0
ScotDiddle is on a distinguished road
Default RE: Building Javascript PHP problem

captainmerton,

Javascript is not happy with line breaks.

I took your posted PHP output, made each JS statement contained on a single line, added an onBlur="inviteFormVerifyMe();" for Email and retypeemail form fields, and it workded lile a champ.

Below is the code that works.

Scot L. Diddle, Richmond VA



Code:
<script language='javascript'>
function inviteFormVerifyMe(){
	var msg='';
	if(document.getElementById('email').value=='') { 
		msg+='- Email';
	}
	
	if(document.getElementById('retypeemail').value=='') { 
		msg+='- Retype Email';}if(msg!='') { 
			alert('The following fields are empty or invalid:'+msg);
			return false
		}
		else{
			return true
		}
}

</script>

<form name="HiMom" id="HiMom" action="<?php echo $_SERVER['PHP_SELF'] ?>" >

<input onBlur="inviteFormVerifyMe();" type="text" name='Email' id="Email>">&nbsp;Email<br />
<input onBlur="inviteFormVerifyMe();"type="text" name='retypeemail' id="retypeemail>">&nbsp;Re-Type Email<br />

</form>
ScotDiddle is offline  
Reply With Quote
Old 10-31-2009, 10:02 AM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks but is there any way to create a string in a php class that contains the entire javascript script code then print it out as html in a way that will work. I dont want to include javascript files in a php class i'd rather build the javascript dynamically i.e. a build javascript method for example. I can get the javascript to work ok its getting it to come out in the html in a format that th browser can use that is th issue.
captainmerton 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Treat PHP Strings as Objects (Like Javascript) Wildhoney Tips & Tricks 15 05-29-2013 03:37 PM
Easily Format JSON using PHP and Interpret using Javascript Wildhoney Advanced PHP Programming 13 02-01-2013 11:05 AM
Problem executing MSSQL query in PHP trmbne2000 MySQL & Databases 2 06-30-2009 02:52 PM
Problem with Prototype and PHP wiifanatic Javascript, AJAX, E4X 3 02-18-2008 01:39 AM
Preventing Spam with PHP and Javascript Wildhoney General 9 10-24-2007 11:35 AM


All times are GMT. The time now is 01:14 PM.

 
     

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