Current location: Hot Scripts Forums » Programming Languages » PHP » Best way??


Best way??

Reply
  #1 (permalink)  
Old 06-18-09, 12:09 AM
connor connor is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Knob Noster, Mo
Posts: 228
Thanks: 1
Thanked 0 Times in 0 Posts
Best way??

I was wondering the best way to make a php file that is metad to javascript....

Something Like this????:
PHP Code:

<?PHP

switch(isset($_GET['script'])){
    case 
'viewport':
        if(isset(
$_SESSION['logged_in'])){
            echo 
"<script type=\"text/javascript\">
             var viewPort = new Ext.Viewport({
                layout: 'border',
                items: [{
                    region: 'north',
                    html: '<h1 class=\"x-panel-header\">Page Title</h1>',
                    autoHeight: true,
                    border: false,
                    margins: '0 0 5 0'
                }, {
                    region: 'center',
                    xtype: 'tabpanel',
                    items: {
                        title: 'Default Tab',
                        html: 'The first tab\'s content. Others may be added dynamically'
                    }
                }, {
                    region: 'south',
                    title: 'Information',
                    collapsible: true,
                    html: 'Information goes here',
                    split: true,
                    height: 100,
                    minHeight: 100
                }]
            });
            </script>"
;
        }else{
            echo 
"<script type=\"text/javascript\">
            var viewPort = new Ext.Viewport({
                layout: 'border',
                items: [{
                    region: 'north',
                    html: '<h1 class=\"viewPort-header\">BLAH</h1>',
                    autoHeight: true,
                    border: false,
                    margins: '0 0 5 0'
                }, {
                    region: 'center'
                }, {
                    region: 'south',
                    title: 'Site Information',
                    collapsed: true,
                    collapsible: true,
                    html: 'Site information will go here...',
                    split: true,
                    height: 100,
                    minHeight: 100
                }]
            });
            </script>"
;
        }
    break;
    case 
'login':
        echo 
"<script type=\"text/javascript\">
            Ext.onReady(function(){
            Ext.QuickTips.init();
         
            // Create a variable to hold our EXT Form Panel. 
            // Assign various config options as seen.     
            var login = new Ext.FormPanel({ 
                labelWidth:80,
                url:'./includes/login.php',
                hideborders:true,
                frame:true, 
                title:'Staff Login', 
                defaultType:'textfield',
                monitorValid:true,
            // Specific attributes for the text fields for username / password. 
            // The \"name\" attribute defines the name of variables sent to the server.
                items:[{ 
                        fieldLabel:'Username',
                        name:'loginUsername', 
                        allowBlank:false
                    },{ 
                        fieldLabel:'Password',
                        name:'loginPassword', 
                        inputType:'password', 
                        allowBlank:false 
                    }],
         
            // All the magic happens after the user clicks the button     
                buttons:[{ 
                        text:'Login',
                        formBind: true,     
                        // Function that fires when user clicks the button 
                        handler:function(){ 
                            login.getForm().submit({ 
                                method:'POST', 
                                waitTitle:'Connecting', 
                                waitMsg:'Sending data...',
         
                    // Functions that fire (success or failure) when the server responds. 
                    // The one that executes is determined by the 
                    // response that comes from login.asp as seen below. The server would 
                    // actually respond with valid JSON, 
                    // something like: response.write \"{ success: true}\" or 
                    // response.write \"{ success: false, errors: { reason: 'Login failed. Try again.' }}\" 
                    // depending on the logic contained within your server script.
                    // If a success occurs, the user is notified with an alert messagebox, 
                    // and when they click \"OK\", they are redirected to whatever page
                    // you define as redirect. 
         
                                success:function(){ 
                                    Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
                                        if (btn == 'ok'){
                                            var redirect = './'; 
                                            window.location = redirect;
                                        }
                                    });
                                },
         
                    // Failure function, see comment above re: success and failure. 
                    // You can see here, if login fails, it throws a messagebox
                    // at the user telling him / her as much.  
         
                                failure:function(form, action){ 
                                    if(action.failureType == 'server'){ 
                                        obj = Ext.util.JSON.decode(action.response.responseText); 
                                        Ext.Msg.alert('Status', obj.errors.reason); 
                                    }else{ 
                                        Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText); 
                                    } 
                                    login.getForm().reset(); 
                                } 
                            }); 
                        } 
                    },{
                        text:'Shut Down',
                        formBind: false,
                        handler:function(){ 
                            Ext.Msg.alert('&raquo;&nbsp;Google', 'You are being forwarded to www.google.com.', function(btn, text){
                                if (btn == 'ok'){
                                    var redirect = 'http://www.google.com'; 
                                    window.location = redirect;
                                }
                            });
                        }
                    }] 
            });
         
         
            // This just creates a window to wrap the login form. 
            // The login object is passed to the items collection.       
            var win = new Ext.Window({
                layout:'fit',
                width:300,
                height:150,
                closable: false,
                resizable: false,
                plain: true,
                bodyborder: false,
                hideborders: true,
                border: false,
                items: [login]
            });
            win.show();
        });
        </script>"
;
    break;
}
?>
__________________
Connor Strandt
JCS Photography
www.JCStrandt.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-18-09, 04:18 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

switch(isset($_GET['script'])) 

... this would be equal to:
PHP Code:

switch([true false]) 

... so it would always jump to "viewport" as it's "true".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-18-09, 07:14 AM
connor connor is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Knob Noster, Mo
Posts: 228
Thanks: 1
Thanked 0 Times in 0 Posts
So how would I call the script for viewport and then for login? What is happening, when I call my javascript via (js.php?script=login) it doesn't show up.
__________________
Connor Strandt
JCS Photography
www.JCStrandt.com

Last edited by connor; 06-18-09 at 07:32 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-18-09, 10:11 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
isset() returns true or false. But you need the value of $_GET['script'] in your switch() statement.

I think you should be able to do this without my help?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-18-09, 05:23 PM
connor connor is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Knob Noster, Mo
Posts: 228
Thanks: 1
Thanked 0 Times in 0 Posts
This is what I have now, but I am still not getting any javascript that is working, and it works when I am using a .js file... I am missing something and it is erking me that I can't find it.

PHP Code:

<?PHP

switch($_GET['script']){
    case 
'viewport':
        if(isset(
$_SESSION['logged_in'])){
            echo 
"<script type=\"text/javascript\">
             var viewPort = new Ext.Viewport({
                layout: 'border',
                items: [{
                    region: 'north',
                    html: '<h1 class=\"x-panel-header\">Page Title</h1>',
                    autoHeight: true,
                    border: false,
                    margins: '0 0 5 0'
                }, {
                    region: 'center',
                    xtype: 'tabpanel',
                    items: {
                        title: 'Default Tab',
                        html: 'The first tab\'s content. Others may be added dynamically'
                    }
                }, {
                    region: 'south',
                    title: 'Information',
                    collapsible: true,
                    html: 'Information goes here',
                    split: true,
                    height: 100,
                    minHeight: 100
                }]
            });
            </script>"
;
        }else{
            echo 
"<script type=\"text/javascript\">
            var viewPort = new Ext.Viewport({
                layout: 'border',
                items: [{
                    region: 'north',
                    html: '<h1 class=\"viewPort-header\">BLAH</h1>',
                    autoHeight: true,
                    border: false,
                    margins: '0 0 5 0'
                }, {
                    region: 'center'
                }, {
                    region: 'south',
                    title: 'Site Information',
                    collapsed: true,
                    collapsible: true,
                    html: 'Site information will go here...',
                    split: true,
                    height: 100,
                    minHeight: 100
                }]
            });
            </script>"
;
        }
    break;
    case 
'login':
        echo 
"<script type=\"text/javascript\">
            Ext.onReady(function(){
            Ext.QuickTips.init();
         
            // Create a variable to hold our EXT Form Panel. 
            // Assign various config options as seen.     
            var login = new Ext.FormPanel({ 
                labelWidth:80,
                url:'./includes/login.php',
                hideborders:true,
                frame:true, 
                title:'Staff Login', 
                defaultType:'textfield',
                monitorValid:true,
            // Specific attributes for the text fields for username / password. 
            // The \"name\" attribute defines the name of variables sent to the server.
                items:[{ 
                        fieldLabel:'Username',
                        name:'loginUsername', 
                        allowBlank:false
                    },{ 
                        fieldLabel:'Password',
                        name:'loginPassword', 
                        inputType:'password', 
                        allowBlank:false 
                    }],
         
            // All the magic happens after the user clicks the button     
                buttons:[{ 
                        text:'Login',
                        formBind: true,     
                        // Function that fires when user clicks the button 
                        handler:function(){ 
                            login.getForm().submit({ 
                                method:'POST', 
                                waitTitle:'Connecting', 
                                waitMsg:'Sending data...',
         
                    // Functions that fire (success or failure) when the server responds. 
                    // The one that executes is determined by the 
                    // response that comes from login.asp as seen below. The server would 
                    // actually respond with valid JSON, 
                    // something like: response.write \"{ success: true}\" or 
                    // response.write \"{ success: false, errors: { reason: 'Login failed. Try again.' }}\" 
                    // depending on the logic contained within your server script.
                    // If a success occurs, the user is notified with an alert messagebox, 
                    // and when they click \"OK\", they are redirected to whatever page
                    // you define as redirect. 
         
                                success:function(){ 
                                    Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
                                        if (btn == 'ok'){
                                            var redirect = './'; 
                                            window.location = redirect;
                                        }
                                    });
                                },
         
                    // Failure function, see comment above re: success and failure. 
                    // You can see here, if login fails, it throws a messagebox
                    // at the user telling him / her as much.  
         
                                failure:function(form, action){ 
                                    if(action.failureType == 'server'){ 
                                        obj = Ext.util.JSON.decode(action.response.responseText); 
                                        Ext.Msg.alert('Status', obj.errors.reason); 
                                    }else{ 
                                        Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText); 
                                    } 
                                    login.getForm().reset(); 
                                } 
                            }); 
                        } 
                    },{
                        text:'Shut Down',
                        formBind: false,
                        handler:function(){ 
                            Ext.Msg.alert('&raquo;&nbsp;Google', 'You are being forwarded to www.google.com.', function(btn, text){
                                if (btn == 'ok'){
                                    var redirect = 'http://www.google.com'; 
                                    window.location = redirect;
                                }
                            });
                        }
                    }] 
            });
         
         
            // This just creates a window to wrap the login form. 
            // The login object is passed to the items collection.       
            var win = new Ext.Window({
                layout:'fit',
                width:300,
                height:150,
                closable: false,
                resizable: false,
                plain: true,
                bodyborder: false,
                hideborders: true,
                border: false,
                items: [login]
            });
            win.show();
        });
        </script>"
;
    break;
}
?>
__________________
Connor Strandt
JCS Photography
www.JCStrandt.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-18-09, 05:35 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Are you trying to load this file with AJAX?
Did you start the session in this file? (session_start)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-19-09, 01:30 AM
connor connor is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Knob Noster, Mo
Posts: 228
Thanks: 1
Thanked 0 Times in 0 Posts
Im new to the whole JavaScript stuff, so I dont know how to do much with AJAX, EXTJS...
__________________
Connor Strandt
JCS Photography
www.JCStrandt.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-19-09, 04:01 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Can you post how you use the code? Meaning, how you embed or try to open it? I don't see any obvious mistakes that would prevent this to output anything.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 06-19-09, 07:14 AM
connor connor is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Knob Noster, Mo
Posts: 228
Thanks: 1
Thanked 0 Times in 0 Posts
As of right now its just the way as if I was using .js files, because I have no clue where to start with AJAX.

PHP Code:

echo '<script type="text/javascript" src="./includes/js1.php?script=login"></script>'
__________________
Connor Strandt
JCS Photography
www.JCStrandt.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 06-19-09, 07:27 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
I wasn't suggesting you to use AJAX, I was just asking, because if you were using AJAX I'd know what'd be wrong.

Anyway, I think I'll be able to help here too. External JS files are not supposed to go between <script> tags. So try and remove there. Also, some browsers won't parse that file if you don't send the correct content-type header. It's a security thing.

Remove the <script> tags and add this line to the very top of your page:
PHP Code:

header('Content-Type: text/javascript'); 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump


All times are GMT -5. The time now is 11:15 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.