0

Im a beginner in AJAX & javascript, so please bear with me.

I want to pass a variable (document.getElementById('txtSearch').value) from my AJAX to a PHP so i tried to code something like this :

$("#btnSearch").click(function() {
            var keyword = "";
            alert(document.getElementById('txtSearch').value);
            $.ajax({
                url: BASE_URL + 'index.php/jwm/search', //This is the current doc
                type: "POST",
                dataType:'json', // add json datatype to get json
                data: ({keyword: document.getElementById('txtSearch').value}),
                error : function(jq, st, err) {
                    console.log(arguments);
                    alert(st + " : " + err);
                },
                success: function(data){
                    alert(data);
                }
            });  
        }); 

However, i got an error : parse error syntax error unexpected token < and sometimes the error change a little bit to be like this : parse error syntax error unexpected token T.

EDIT This is my PHP code :

function search() {
        $keyword = $_POST['keyword'];
        echo "TEST : $keyword";
        $result = $this->jwm_m->getDataByKeyword('msmall', $keyword);
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST');  
        header('Content-type: application/json', true);
        echo json_encode($result);
    }

This is what my console get :

arguments: null
caller: null
length: 0
name: ""

My goal is to passing document.getElementById('txtSearch').value, please help me out.

Thanks for your help :D

4
  • 3
    is your php script actually returning a valid json file ? Commented Jul 22, 2013 at 9:29
  • Inspect the response in your Developer Tools Network Panel. The parse error usually means there's an error in the backend and it's not returning proper JSON back to you. Commented Jul 22, 2013 at 9:30
  • Did you able to get document.getElementById('txtSearch') any result for this line try to execute in console Commented Jul 22, 2013 at 11:01
  • As a matter of fact, adding on to @godfrzero: Is the function you defined called search() being called when you make that AJAX call? Does it work when you go to that URL directly? Commented Jul 22, 2013 at 11:02

1 Answer 1

3

A couple of things to note about your updated question, in the PHP code:

  • You mentioned that you get an error parse error syntax error unexpected token T. This is probably because of the echo "TEST : $keyword"; in PHP, which causes the response to be invalid JSON.

  • You're setting the headers after echo "TEST : $keyword";, this will throw a PHP warning similar to Cannot modify header information - headers already sent by...

So remove the echo "TEST : $keyword"; line and try sending the request again. This time, keep the network panel of your developer tools open and check the data being sent/received. If you're still having issues, update your question with the server response & request body. The more information you provide, the more likely it is that you'll get a good solution.

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

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.