简体   繁体   中英

how manage auto-submit with the action atribute in the form

I needed make a dinamic dependent dropdown in a form,so i found this solution that uses the JS auto-submit function:

function autoSubmit()
    {
         var formObject = document.forms['dados'];
            formObject.submit();
    }

then I use the onchange event in the first dropdown to call the auto-submit function:

<label>Campus:</label>
<select name="campus" onchange="autoSubmit();">
<option VALUE="null"></option>
<?php

//Popula a lista com os cursos do DB

$sql = "SELECT id,nome FROM campus";
$countries = mysql_query($sql,$conn);
while($row = mysql_fetch_array($countries))
{
if($row[nome]==$campus)
        echo ("<option VALUE=\"$row[nome]\" selected>$row[nome]</option>");
    else
    echo ("<option VALUE=\"$row[nome]\">$row[nome]</option>");
 }

 ?>
</select>

with this the element "campus" will be setted to be used in the second dropdown SELECT statement:

$campus = $_POST['campus'];
...
<label>Curso:
<span class="small">curso corrente</span>
</label>
<select name="curso">
<option VALUE="null"></option>
<?php

$consulta2 = "SELECT curso FROM campus_cursos WHERE campus = \"" . $campus . "\"";
$cursoslista = mysql_query($consulta2,$conn);
while($row = mysql_fetch_array($cursoslista))
{
    echo ("<option VALUE=\"$row[curso]\">$row[curso]</option>");
}

?>
</select>

this code is working,but the problem is that in this way I cant set a action atribute in the form because if i do this every time the first dropdown changes it will redirect to the action's URL.this is the form that works:

<form  name="dados" method="POST" onsubmit="return validar();">

with no action atribute I cant use a submit button to send the data of all the others elements to the right URL.there is a way to this?

我使用由“ on change”事件触发的ajax脚本解决了这个问题。ajax脚本调用一个外部文件,该文件返回一个元素数组。该脚本使用这些元素填充下拉列表。

You should use Ajax code to populate the second dropdown values.

On Form's page:

<label>Campus:</label>
<select name="campus" id="campus">
<option VALUE="null"></option>
<?php

//Popula a lista com os cursos do DB

$sql = "SELECT id,nome FROM campus";
$countries = mysql_query($sql,$conn);
while($row = mysql_fetch_array($countries))
{
if($row[nome]==$campus)
        echo ("<option VALUE=\"$row[nome]\" selected>$row[nome]</option>");
    else
    echo ("<option VALUE=\"$row[nome]\">$row[nome]</option>");
 }

 ?>
</select>
<label>Curso:
<span class="small">curso corrente</span>
</label>
<select name="curso" id="curso">

</select>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#campus').change(function(){
var campusName = $(this).val();
$('#curso').load('generateCurso.php?campus='+campusName);
});
});
</script>

Write a PHP file, called generateCurso.php

<?php
$campus = $_GET['campus'];

$consulta2 = "SELECT curso FROM campus_cursos WHERE campus = \"" . $campus . "\"";
$cursoslista = mysql_query($consulta2,$conn);
?>
<option VALUE="null"></option>
<?php
while($row = mysql_fetch_array($cursoslista))
{
    echo ("<option VALUE=\"$row[curso]\">$row[curso]</option>");
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM