• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ajax issue

#1
[eluser]new_igniter[/eluser]
Hello,
I am using the standard ajax approach in CI where I am simply doing an ajax call onclick and calling a function in a controller. However, if I try to do any CI within the function I get the below error. I am guessing that the ajax is looking for native PHP and is tripping on the CI syntax, but am hoping someone might be able to help me to understand how to perform a query with the ajax request.

PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

#2
[eluser]Randy Casburn[/eluser]
Hey new_igniter, I'll be glad to help, but we'll need to clarify several things first...

[quote author="new_igniter" date="1217912378"]...if I try to do any CI within the function...[/quote]

We don't "do CI". The Web server's PHP instance executes your CI PHP scripts. One of these has your function in it.

[quote author="new_igniter" date="1217912378"]I am guessing that the ajax is looking for native PHP and is tripping on the CI syntax,...[/quote]

Again, you seem confused here since CI has no syntax. PHP has syntax that is executed by the PHP server instance run by the web server. Also, ajax is not looking for anything to come back from the server other than text.


[quote author="new_igniter" date="1217912378"]but am hoping someone might be able to help me to understand how to perform a query with the ajax request.[/quote]

In order to do this, we'll need to see your javascript that is making the ajax call and your controller function that is responding to the call.


[quote author="new_igniter" date="1217912378"]PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING[/quote]

This error says that you've probably missed a quote in a double quoted string on the line number that you've excluded from the error.

Looking forward to helping you,

Randy

#3
[eluser]new_igniter[/eluser]
Hi Randy, thanks for your time. Below is my code. Basically, if I try and load data from a model, in my getAlbum function in the controller, with something like:
Code:
$this->load->model('Albumsmodel', '', TRUE);
$this->Albumsmodel->getAlbum($groupID);
in my controller, then I get the error
PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

***code starts here***
Drop down HTML
Code:
onchange="getDropdown();

Controller
Code:
function getAlbum($groupID)
{
        echo "<select>";
        echo "<option value =\"volvo\">groups albums 1</option>";
        echo "<option value =\"saab\">groups albums 2</option>";
        echo "<option value =\"opel\" selected=\"selected\">groups albums 3</option>";
        echo "<option value =\"audi\">groups albums 1</option>";
        echo "</select>";
}

Ajax
Code:
var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}

function getDropdown() {
  http.open("GET", "http://pete.mombo.com/groups/getAlbum", true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      document.getElementById('album_container')[removed] = http.responseText;
    }
  }
  http.send(null);
}

#4
[eluser]Randy Casburn[/eluser]
Hey, I'll assume that when you comment out $this->Albumsmodel->getAlbum($groupID); you get a different error. So I'll need to see your Albumsmodel.php file's getAlbum() method. That is likely where the error is being generated from.

Before you just post it though, will you please go through the method and look for quote imbalances please.

Thanks,
Randy

#5
[eluser]new_igniter[/eluser]
Thanks so much for your time Randy! Here is my controller code and model code. If I comment out this model load and function, its fine. (Also, the existing select is just to see the UI react on the ajax call, which would be replaced by a loop when getAlbums returns an array)

Controller
Code:
function getAlbum($groupID)
    {
        $this->load->model('Albumsmodel', '', TRUE);
        $this->Albumsmodel->getAlbums($groupID);

        echo "<select>";
        echo "<option value =\"volvo\">groups albums 1</option>";
        echo "<option value =\"saab\">groups albums 2</option>";
        echo "<option value =\"opel\" selected=\"selected\">groups albums 3</option>";
        echo "<option value =\"audi\">groups albums 1</option>";
        echo "</select>";
    }

Code:
function getAlbums($groupID)
    {
        $query = $this->db->query("select * from albums where groupID = $groupID");

        if(empty($query))
            return false;

        return $query->result();
    }

#6
[eluser]Randy Casburn[/eluser]
let's apply some convention to your Model and do this with it:

Code:
function getAlbums($groupID)
{
    $sql = "select * from albums where groupID = $groupID";

    $query = $this->db->query($sql);

    if(empty($query))
        return false;

    return $query->result();
}

I personally don't like interpolating strings. I get confused as can be. I always concatenate strings and variables just because I don't get stupid with quotes. But that's just me. If it were me, I would do it like this:

Code:
$sql = 'select * from albums where groupID = ' . $groupID;

My editor (IDE) would highlight it in a cool and nifty way for me that way too ;-).

Try that...see what happens.

Randy


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.