Per favore datemi una mano perché sto uscendo matto.
Nel file default.php di una vista del frontend di un componente ho questo codice AJAX
function cartaInternoChanged()
{
var mypostrequest = new assegnaXMLHttpRequest();
mypostrequest.onreadystatechange = function() {
if (mypostrequest.readyState == 4){
if (mypostrequest.status == 200 || window.location.href.indexOf("http") == -1){
var select = mypostrequest.responseText;
select = select.match(/<select(.*?)<\/select>/m);
alert(select); //stampa null
document.getElementById("select-formatocarta").innerHTML = select;
}
else{ alert("An error has occured making the request"); }
}
}
var parametri = "option=com_preventivilibri&task=formaticarta&tmpl=component&";
parametri += "tipocarta="+document.forms["preventivo"].elements['tipocarta'].value;
mypostrequest.open("POST", "index.php", true);
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
mypostrequest.send(parametri);
return false;
}
La funzione formaticarta() che viene chiamata si trova nel controller ed ha questo codice
function formaticarta()
{
$db = &JFactory::getDBO();
$query = "SELECT id, formato, h, l FROM #__gestionepreventivi_formaticarta";
$db->setQuery($query);
$result = $db->loadAssocList();
$options = array();
foreach($result as $option)
$options[] = JHTML::_('select.option',$option['id'],JText::_($option['formato']." (".$option['h']." x ".$option['l'].")"));
echo JHTML::_('select.genericlist', $options, 'formatocarta');
}
Perché
select = select.match(/<select(.*?)<\/select>/m);
non trova alcun match?
Se provo a stampare direttamente la variabile "select" il menu appare, quindi la risposta della funzione php è corretta, ma la match() non se ne accorge.
Se al posto di
var select = mypostrequest.responseText;
metto
select = "bla bla <select name='formatocarta'><option value='1'>A4</option><option value='2'>A5</option></select> bla bla";
il match() fa il suo lavoro, rimuove tutto ciò che non è il tag select, quindi la regex funziona
Il problema sta nel fatto che il responseText non è interpretato come stringa... come mai?
Il punto è che oltre al tag select arriva altro codice aggiunto da Joolma che non mi serve e voglio rimuoverlo selezionando solo il tag select.
Aiutatemi, non so più che pesci prendere.