我有一个查询返回一个数组,我在第一个选择中显示它,然后当你点击选项的一个选项时,它返回所选名称的id,所以我在生成最后一个选择时遇到问题第一个Select的匹配,即代码:
<html>
<head>
<script language="javascript" type="text/javascript">
function ajaxFuncio(selectobj){
var i = selectobj.selectedIndex;
var idOptionFirst = selectobj[i].getAttribute('id');
return idOptionFirst;
//alert(idOptionFirst);
}
function secondSelect(){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}
catch (e){
// Internet Explorer
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("En el teu navegador no funciona!");
return false;
}
}
}
// Crear una funció que envia dades al servidor
//i actualitza la seccio div
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 0){
var ajaxDisplay = document.getElementById('info');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
alert("asdf");
}
}
// Agafem els valors passats
var selectPrimer = document.getElementsByName('selActi');
var codi = ajaxFuncio(selectPrimer);
var cadenaconsulta = "?idTActivitat=" + codi ;
ajaxRequest.open("GET", "test_ajax.php" + cadenaconsulta, true);
ajaxRequest.send(null);
}
</script>
</head>
<body>
<?php
$conn=oci_connect('*******','****','****')
or die("Failed to connect to Oracle: ".mysqli_error());
//PRIMER SELECT
$sqlFirstSel=oci_parse($conn, 'SELECT * FROM ACTIVIDAD');
oci_execute($sqlFirstSel);
$cadena = "<form id='formActis' method='POST' action='test_ajax.php'>";
$cadena .= "<table>";
$cadena .= " <tr>";
$cadena .= " <td>";
$cadena .= " <select name='selActi[]' onChange='ajaxFuncio(this);'>";
$cadena .= " <option selected>Selecciona l'Activitat:</option>";
$cadena .= " <option></option>";
while (($row=oci_fetch_array($sqlFirstSel,OCI_BOTH))!=false){
$cadena.=" <option name='idFirstSel' id='".$row['ID']."'>".$row['NOM']."</option>";
}
$cadena .= " </select>";
$cadena .= " </td>";
$cadena .= " <td>";
$cadena .= " <input type='button' onclick='secondSelect();' value='enviar'/>";
$cadena .= " </td>";
$cadena .= " </tr>";
$cadena .= "</table>";
//SEGON SELECT
$sqlSeconSel=oci_parse($conn, 'SELECT * FROM TIPO_ACTIVIDAD WHERE ID_ACTIVIDAD='.$idTActivitat.';');
oci_execute($sqlSeconSel);
$cadena .= "<table>";
$cadena .= " <tr>";
$cadena .= " <td>";
$cadena .= " <select name='selTActi[]'>";
$cadena .= " <option selected>Selecciona Tipus Activitat:</option>";
$cadena .= " <option></option>";
while (($row=oci_fetch_array($sqlSeconSel,OCI_BOTH))!=false){
$cadena.=" <option name='idSecondSel' id='".$row['ID']."'>".$row['NOM']."</option>";
}
$cadena .= " </select>";
$cadena .= " </td>";
$cadena .= " </tr>";
$cadena .= "</table>";
$cadena .= "</form>";
echo $cadena;
echo "<div id='info'></div>";
?>
</body>
</html>
返回这些错误,但第一个选择有效,所以第二个没有,如果你在函数ajaxFuncio(selectobj);将返回更改为警报,它会在该警报上返回没有问题的ID。所以错误行:
Notice: Undefined variable: idTActivitat in C:\xampp\htdocs\UF4\test_ajax.php on line 85
Warning: oci_execute(): in C:\xampp\htdocs\UF4\test_ajax.php on line 86
Warning: oci_fetch_array(): in C:\xampp\htdocs\UF4\test_ajax.php on line 94
我可以在浏览器上看到带有结果的图片: