Welcome Guest, Not a member yet? Register   Sign In
Create custom Ajax-requests
#1

[eluser]Grind[/eluser]
On a page, checking and unchecking a checkbox must be stored to the database via Ajax. Normally I achieve it like this:
Code:
[removed]
<!--

function rights(group_id, value, module)
{
    var checked = (value) ? 1 : 0;
    
    createXMLHttpRequest();
    var params = "checked="+checked;
    params += "&group;_id="+group_id;
    params += "&module;="+module;
    
    xmlHttp.open("POST", "/ajax/saverights.php", true);
    xmlHttp.onreadystatechange = saveready;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
    xmlHttp.send(params);
}

function createXMLHttpRequest()
{
    if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
}

function saveready()
{
    if (xmlHttp.readyState == 4)
    {
        var response = xmlHttp.responseText;
        alert(response);
    }
}
//-->
[removed]

But now with CI I get a 404 when I try to access /ajax/saverights.php. How does CI handle Ajax requests? Is it possible for me to write everything by myself? Which URI should I refer to in the JS-code?

Thanks.
#2

[eluser]Ben Edmunds[/eluser]
It works the same way as any other URL. So for ajax/saverights you would create a controller named ajax with a saverights method (action) and then simply call it at ajax/saverights
#3

[eluser]Grind[/eluser]
Thanks for your reply.
I don't get a 404 anymore, but if I print_r() the post-array via print_r($_POST) or print_r($this->input->post()) the array is empty. What could be wrong?

EDIT
Got it. Thank you once again Smile
#4

[eluser]Ben Edmunds[/eluser]
Anytime
#5

[eluser]jedd[/eluser]
Hey Ben - this might be a new record - a 33:1 ratio of signature to message content! Well done you. ; )

You could reduce that to a mere 31:1 ratio if you didn't mention your email and twitter addresses twice in your signature.

You could reduce that to a mere 31:1 ratio if you didn't mention your email and twitter addresses twice in your signature.
#6

[eluser]Ben Edmunds[/eluser]
The goal is to see if I can have my signature take up half a page.

Almost there...




Theme © iAndrew 2016 - Forum software by © MyBB