TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Building Javascript PHP problem (http://www.talkphp.com/absolute-beginners/5071-building-javascript-php-problem.html)

captainmerton 10-29-2009 08:50 PM

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> 


ScotDiddle 10-30-2009 05:17 PM

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>


captainmerton 10-31-2009 10:02 AM

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.


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

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0