0

In my Javascript code after the line below:

url: ....../Misc-2/Ci-TodoList/index.php/home/jsonAddData,

I am getting the following error:

index.php:13 Uncaught SyntaxError: Unexpected token :

<script type="text/javascript" language="javascript">
$(document).ready(function() {
    $('#add').bind('keypress', function(e) {
        if(e.keyCode == 13){
            $.ajax({
            type: "POST",
            dataType: "JSON",
            url: <?php echo site_url("home/jsonAddData"); ?>,
            data: dataString,
            json: {title_posted: true},
            success: function(data){
            if(data.title_posted == true) { // true means data was successfully posted.
                $("#success").append("Success");
            } else if(data.title_posted == false) { // false means data failed to post.
                $("#success").append('Failure');
            }
          }
       });
    }
});

      }
    });
});
</script>

I'm pretty positive after a few debugging tricks that index.php:13 is referring to my PHP script. One of the things it was doing was saying that when I was loading the url helper, it said that that was causing the error. Then I autoloaded the url helper and now it's saying line 13 is causing the error, but line 13 is only a mysql select query as you can determine below:

<?php
class home extends CI_Controller {
    function __construct() {
        parent::__construct();
    }
    function index() {
        $data = array();
        $data['lists'] = $this->displayList();
        $this->load->view('home', $data);
    }
    function displayList() {
        $str = '';
        $query = $this->db->query("SELECT * FROM data");
        foreach ($query->result() as $row) {
            $b = '<input name="completed" type="checkbox" />';
            $a = $row->title . "<br>";
            $str .= $b.$a;
        }
        return $str;
    }
    function jsonAddData() {
        if($this->input->is_ajax_request()) {
        header('Content-type:application/json');
        $title = $this->input->post('title');
        $query = $this->db->query("INSERT INTO data (title) VALUES ('$title')");
        if($query) return json_encode(array('title_posted' => true));
        else return json_encode(array('title_posted' => false));
        }
    }
}
?>

Any ideas to why this is happening?

5
  • 1
    Are you positive that is the right index.php file? Commented Dec 27, 2011 at 15:07
  • It can't be... Post the file named "index.php" Lines 3 - 23 should do. Commented Dec 27, 2011 at 15:14
  • Unexpexted token is a js error. Are you seeing that in firebug? Your json may have dodgey characters in it Commented Dec 27, 2011 at 15:19
  • 1
    You're also not setting your headers in the correct CI way by the way. See the output class Commented Dec 27, 2011 at 15:21
  • @BenSwinburne yes I'm seeing it in the Chrome Javascript Console... and thanks for that tidbit of info. I'll most def read up on the output class. Commented Dec 27, 2011 at 15:42

1 Answer 1

3

You are getting JS error so PHP syntax is not the culprit. :)

Wrap <?php echo site_url("home/jsonAddData"); ?> in double quotations (I mean ""). Also you have other syntax errors, proper indenting can save your time in the future.

Here is the solved version:

$(document).ready(function() {
    $('#add').bind('keypress', function(e) {
        if(e.keyCode == 13){
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: "<?php echo site_url("home/jsonAddData"); ?>",
                data: dataString,
                json: {title_posted: true},
                success: function(data){
                    if(data.title_posted == true) { // true means data was successfully posted.
                        $("#success").append("Success");
                    } else if(data.title_posted == false) { // false means data failed to post.
                        $("#success").append('Failure');
                    }
               }
          });
       }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man.... this is a big help. What exactly was I doing wrong though? was it just the single quotations? Or was it something else too?
You are most welcome, there was a quotation issue and you also messed up with brackets, specially in this two } }); . No worries, cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.