3

I'm trying to implement a custom suggestion engine using jquery.

I take the user input, call my codeigniter v2 php code in order to get matches from a pre-built synonyms table.

My javascript looks like this:

var user_str = $("#some-id").val();

$.ajax({
    type: "POST",
    url: "http://localhost/my-app/app/suggestions/",
    data: {"doc": user_str},
    processData: false
})
.done(function (data)
{
    // Show suggestions...
});

My PHP code (controller) looks like this:

function suggestions()
{
    $user_input = trim($_POST['doc']);
    die("[$user_input]");
}

But the data is NOT posted to my PHP :( All I get as echo is an empty [] (with no 500 error or anything)

I've spent 2 days looking for an answer, what I've read in SO / on google didn't help. I was able to make this work using GET, but then again, this wouldn't work with unicode strings, so I figured I should use POST instead (only this won't work either :()

Anyone can tell me how to fix this? Also, this has to work with unicode strings, it's an important requirement in this project.

I'm using PHP 5.3.8

10
  • Is your Javascript inside a JS file, or your view? Commented Feb 19, 2015 at 16:43
  • @Craig It's in a separate js file Commented Feb 19, 2015 at 16:46
  • how is your php file called ? Commented Feb 19, 2015 at 16:49
  • It's called app.php Commented Feb 19, 2015 at 16:50
  • 1
    Remove processData: false, also unless you're using php5.4+ strings aren't treated as utf-8 by default. Commented Feb 19, 2015 at 17:51

2 Answers 2

2

Try using the $.post method, then debug. Do it like this:

JS

var user_str = $("#some-id").val();
var url = "http://localhost/my-app/app/suggestions";
var postdata = {doc:user_str};
$.post(url, postdata, function(result){
    console.log(result);
});

PHP

function suggestions(){
    $user_input = $this->input->post('doc');
    return "MESSAGE FROM CONTROLLER. USER INPUT: ".$user_input;
}

This should output the message to your console. Let me know if it works.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I followed your approach, and did some digging. I have it working now, even with unicode POSTed strings, but I had to turn off this csrf_protection, which is really not ideal as far as I'm aware. Any idea how to make it work without disabling the csrf_protection?
nice!.. glad i could help.
in that case, you need to find a way to filter and clean your $user_input before you send it back to the front end. Look into utf8_encode/utf8_decode
I did, please take a look at the code that I just posted. Thanks for your help, I really appreciate your time man :)
0

For those interested, this works for me, even with csrf_protection being set to true

var cct = $.cookie('csrf_the_cookie_whatever_name');
$.ajax({
    url: the_url,
    type: 'POST',
    async: false,
    dataType: 'json',
    data: {'doc': user_str, 'csrf_test_your_name': cct}
})
.done(function(result) {
    console.log(result);
});

Comments

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.