Why do I get this error:
Cannot read property 'push' of undefined
It seems like I can't push my data to my barcodes
array? The console.log(data)
works.
var myScanObj = {
$el: null,
$res: null,
$searchButton: null,
$resetButton: null,
$hiddenInput: null,
$scanInput: null,
$selectType: null,
barcodes: [],
init: function() {
myScanObj.cacheDom();
myScanObj.bindEvents();
},
cacheDom: function() {
$el = $('.formControl');
$res = $('#result');
$searchButton = $el.find('#searchButton');
$resetButton = $el.find('#resetButton');
$hiddenInput = $el.find('input[name=hiddenField]');
$scanInput = $el.find('input[name=myScan]');
$selectType = $el.find('#myType');
},
bindEvents: function() {
$searchButton.on('click', this.addBarcode.bind(this));
},
addBarcode: function() {
var postForm = {
'myType': $selectType.val(),
'myScan': $hiddenInput.val()
};
$.ajax({
url: "test.php",
type: "POST",
data: postForm,
dataType: "text",
success: this.successAjax
});
},
showBarcode: function() {
},
successAjax: function(data) {
this.barcodes.push(data);
console.log(data);
}
};
myScanObj.init();
This is my test.php code:
<?php
$output = '';
$output .= $_POST["myType"];
$output .= $_POST["myScan"];
echo $output
?>