mdelafuq
mdelafuq

Reputation: 120

Input value based on Select and SQL result?

Ok, I have a SQL table "departamentos"

IMAGE OF TABLE

And I have form like this:

<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>

Now, I need to dinamically change values of input 'destinatario' and input 'correo' depending on selected option. I know this can be done with Javascript or Jquery or something like, but I need this to be done with a MySQL result, because fields 'encargado' and 'correo' in MySQL table have often changes, so I wont be updating Javascript code every time.

In other words, I need an event on the select input, like onchange connect to db and send a query like

select 'encargado' from departamentos where departamento=SELECTEDOPTION;

and

select 'correo' from departamentos where departamento=SELECTEDOPTION;

and bring those values back to input 'destinatario' and input 'correo' respectivey.

Upvotes: 0

Views: 3195

Answers (1)

Paresh Gami
Paresh Gami

Reputation: 4782

index.php file for html code

<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 (Get Data from javascript function and return the filtered data from 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>";
    }

?>

Upvotes: 2

Related Questions