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
document.getElementById('txtSearch')any result for this line try to execute in consolesearch()being called when you make that AJAX call? Does it work when you go to that URL directly?