基于Select和SQL结果的输入值?

时间:2014-04-05 00:00:38

标签: php mysql

好的,我有一个SQL表“departamentos”

IMAGE OF TABLE

我有这样的形式:

<form class="form-signin" action="controlador/controlador.php?accion=GuardarCompromiso" method="post" id="agregarCompromiso" style="display:none;" role="form">

        <select class="form-control" id="departamento" name="departamento" required>
                  <option selected disabled value="">Departamento</option>
                          <option value="Distribucion">Distribución</option>
                          <option value="Pycon">Pycon</option>
                          <option value="Planeacion">Planeación</option>
                          <option value="Comercial">Comercial</option>
                          <option value="Administracion">Administración</option>
                          <option value="Personal">Personal</option>
                          <option value="Ingenieria de Servicio al Cliente">Ingeniería de Servicio al Cliente</option>
                          <option value="Atencion a Clientes">Atención a Clientes</option>
                          <option value="Sistema Integral de Gestion">Sistema Integral de Gestión</option>
                          <option value="CFEctiva">CFEctiva</option>
                          <option value="Juridico">Jurídico</option>
                          <option value="Agencia Ures">Agencia Ures</option>
                          <option value="Agencia Miguel Aleman">Agencia Miguel Alemán</option>
                          <option value="Agencia Sahuaripa">Agencia Sahuaripa</option>
        </select>

        <input name="destinatario" id="destinatario" type="text" class="form-control" placeholder="Destinatario" required />
        <input name="correo" id="correo" type="text" class="form-control" placeholder="Correo" required />

        <div class="btn_login">
        <input class="btn btn-lg btn-primary btn-block" type="submit" value="Agregar Compromiso" />
        </div>
    </form>

现在,我需要根据所选选项以dinamically方式更改输入'destinatario'的值并输入'correo'。我知道这可以用Javascript或Jquery等完成,但我需要用MySQL结果来完成,因为MySQL表中的字段'encargado'和'correo'经常发生变化,所以我不会每次更新Javascript代码时间。

换句话说,我需要在select输入上有一个事件,比如onchange连接到db并发送一个像

这样的查询
select 'encargado' from departamentos where departamento=SELECTEDOPTION;

select 'correo' from departamentos where departamento=SELECTEDOPTION;

并将这些值带回输入'destinatario'并输入'correo'个别。

1 个答案:

答案 0 :(得分:2)

html代码的index.php文件

<html>
    <head>
        <title></title>
        <script type="text/javascript" src="jquery.js">
        </script>

        <script>
        function getData()
        {
            var formData = new FormData($("#myfrm")[0]);
            $.ajax({
                url: 'getData.php',
                type:'POST',
                data: formData,
                processData: false,
                contentType: false,
                cache: false,
                mimeType: 'multipart/form-data',
                success: function(html)
                {               
                    $("#dataDiv").html(html);
                } 
        });
        return false
        }


        </script>

    </head>

    <form id="myfrm">
        <select onchange='getData()' name='selOption'>
            <option>Option 1</option>
            <option>Option 2</option>
            <option>Option 3</option>
            <option>Option 4</option>
            <option>Option 5</option>
        </select>
        <div id="dataDiv">

        </div>
    </form>
</html>

getData.php(从javascript函数获取数据并从db返回过滤后的数据)

<?php
    $nm=$_REQUEST['selOption'];

    //Database connection Here
    $query1="select 'encargado' from departamentos where departamento=$nm";
    $rs=mysql_query($query1);
    while($row=mysql_fetch_array($rs))
    {

        echo "<input type='text' value=$row[0]><br>";
    }

?>