Welcome Guest, Not a member yet? Register   Sign In
Using Ajax
#7

[eluser]archielles[/eluser]
[quote author="waldmeister" date="1250855020"]
Including JS files:
Code:
//put this in the head of your html file - in the view that contains the head of your page
{script src="<?php echo base_url().'path/to/file.js'; ?>" type="text/javascript" }{/script}

From here on, above code is placed in html or presentation view files right? just like how I normally import js script in the heading section.

Code:
{script type="text/javascript" src="xhr.js"}{/script}

and has:

Code:
// file asesInput.js
// using POST method
var xhr = createRequest();
function inputData(aTicketNum)
{
        
        if(xhr)
        {
            var requestbody ="numOfTicket="+encodeURIComponent(aTicketNum)+"&memberID;="+encodeURIComponent(holdID);
            xhr.open("POST", "asesInputData.php", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function()
            {
                
                if (xhr.readyState == 4 && xhr.status == 200)
                {
                    
                    var serverText = xhr.responseText;
                    if(serverText.indexOf('|'!=-1))
                    {
                        
                        element=serverText.split('|');
                        document.getElementById(element[0]),innerHTML= element[1];
                        if(element[2]!=null)
                        {
                        alert(element[2]);
                        }    
                    }
                        
                }
            }
            
            xhr.send(requestbody);
            
        }
            
}
With this, it can work just like normally it would. Data manipulation is done in asesInputData.php and anything that is echoed will be returned to javascript in xhr.responseText.

Your ajax framework that you showed me is shorter but where should I put that? in controller or view? it seems I have to change alot of structure here?

Since controller can load view and display it as like html page, it also load javascript specified in that file. Do I need to use different way to call ajax although it work just fine? or contradict with MVC structure?


Messages In This Thread
Using Ajax - by El Forum - 08-21-2009, 12:20 AM
Using Ajax - by El Forum - 08-21-2009, 12:43 AM
Using Ajax - by El Forum - 08-21-2009, 01:19 AM
Using Ajax - by El Forum - 08-21-2009, 01:30 AM
Using Ajax - by El Forum - 08-21-2009, 02:16 AM
Using Ajax - by El Forum - 08-21-2009, 02:31 AM
Using Ajax - by El Forum - 08-22-2009, 08:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB