I'm new to web programming, I'm doing some practice and I'm stuck. Apart from never having used this type of languages I work with the inherited code of another person with whom I have no possibility to contact. Based on the structuring of all the code I have managed to create this ajax function that returns a list of style objects: [Object1, DefObject1] [Object2, DefObject2] ...
This list would need to use it as possible selection objects in a dropdown that is defined in the same document "historial.js"
ajax function
var sel = null;
$(document).ready(function(){
//function to load different types of pices
$.ajax({
url: 'http://localhost/public/index.php/tipusDePesaTots', //url local
type : "POST",
dataType : 'json',
success: function(result){
console.log(result);
sel = result;
//alert(sel.toSource());
},
error: function(result){
alert(sel);
}
})
return sel;
});
creation of the form with the selector:
function mostraDialeg(pesaActual, usuari){
alert(sel.toSource());
//Get piece's number and year
if (pesaActual.length == 14) {
var posicioPesaSubs = pesaActual.substring(6, 7);
var anyPesaSubs = pesaActual.substring(10);
} else {
var posicioPesaSubs = pesaActual.substring(6, 8);
var anyPesaSubs = pesaActual.substring(11);
}
//Comprobació
//console.log('Peça: ' + posicioPesaSubs + ' Any: ' + anyPesaSubs + ' Usuari: ' + usuari);
quadre = '<div id="qDialeg" class="modal fade">';
quadre += '<div class="modal-dialog">';
quadre += '<div class="modal-content">';
quadre += '<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button></div>';
quadre += '<div class="modal-body">';
quadre += '<form class="form-horizontal">';
quadre += '<div class="camps"><label for="etiqueta">Etiqueta:</label>';
quadre += '<input type="text" name="etiqueta" id="etiqueta" size="50" value=""/> </div>';
quadre += '<div class="camps"><label for="tipusP">Tipus:</label>';
quadre += '<select name="tipusP" id="tipusP"><option value="sel">$sel</option>';
quadre += '</select></div>';
quadre += '<div class="camps"><label for="aplicat">Aplicat a:</label>';
quadre += '<input type="text" name="aplicat" id="aplicat" size="50" value=""/></div>';
quadre += '<div class="camps"><label for="compromis">Compromis:</label>';
quadre += '<input type="text" name="compromis" id="compromis" size="50" value=""/></div>';
quadre += '<div class="camps"><label for="acompliment">Acompliment (%):</label>';
quadre += '<input type="text" name="acompliment" id="acompliment" value=""/></div>';
quadre += '<div class="camps"><label for="validacio">Validació:</label>';
quadre += '<select name="validacio" id="validacio"><option value="PRA">Pra</option><option value="DIR">Dir</option></select>';
quadre += '<input type="checkbox" name="validat" id="validat" value="1"/></div>';
quadre += '<div class="camps"><label for="comments">Altres:</label>';
quadre += '<textarea name="comments" id="comments" cols="25" rows="3"></textarea></div>';
quadre += '<a id="okPesa"></a>';
quadre += '<a href="" class="btn btn-primary botoDialeg">Cancel</a>';
quadre += '<a id="modifica"></a>';
quadre += '</form></div></div></div></div></div>';
The selector is the tipusP,
I run into a couple of problems, the first and foremost is that I can not capture the value of sel outside the ajax function, I get that sel is not defined and secondly I'm not sure how to define the selector of the form to take the values of the variable.
Can anyone shed some light on the subject?
Greetings and forgiveness because possibly the question may not be very complex with some practice, but as I say I have never used these languages and I am finding it difficult to find the solution.
EDIT: forgive my ignorance, I believe that murphy has come into action, I have managed to capture the variable sel, I have simply declared it outside the function $ (document) .ready (function () {and not only outside the function ajax as I had it, now I'm going to keep trying to pass the variable to the selector, I've modified the code that works for me now.