3

My user needs to pick an option from a first <select> and according to his selection, a second <select> should offer different options.

You may have seen this when picking a State after picking a Country in many many websites.

I know there are hundred other questions like this but I have tried lots of their answers without any success.

So, here is where I am standing right now:

            <div class="col-md-2">
              <span class="input-group-addon" id="basic-addon1" style="border:solid 1px #CCC; border-radius:4px;">Juzgado: </span>
              <select class="form-control" id="juz" name="juz">
                <option value="1">Juzgado 1</option>
                <option value="2">Juzgado 2</option>
                <option value="3">Juzgado 3</option>
              </select>
            </div>

            <div class="col-md-2" id="nomDiv">
                  <span class="input-group-addon" id="basic-addon1" style="border:solid 1px #CCC; border-radius:4px;">Nominación: </span>
                  <select class="form-control" id="nom" name="id_juz_nom">

                  </select>
            </div>

Then... my AJAX

<script type='text/javascript'>
        $(document).ready(function(){
            $('select[name=juz]').on('change', function() {

                var juz = $(this).val() 

                $.ajax({
                url: "tomarNominaciones.php",
                type: "POST",
                data: { juz.value },

                success: function (response) {

                        document.getElementById('nom').innerHTML= response;

                }
            });
          });
        });

</script>

And finally this is my PHP:

    <?php 
$juz = $_POST['juz'];
include 'conectar.php';
mysql_select_db('juz_nom');
$query  = "SELECT * FROM juz_nom WHERE juzgado=\"".$juz."\"";
$result = mysql_query($query);
?>

<span class="input-group-addon" id="basic-addon1" style="border:solid 1px #CCC; border-radius:4px;">Nominación: </span>
<select class="form-control" id="nom" name="id_juz_nom">
<option>Seleccione Nominación</option>
<?php while ($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id_juz_nom']?>><?php echo $row['nominacion']?></option>
<?php } ?>
</select>

I've tried many methods such as this one and this other one. Without any success.

So, to be clear, what I need to do is: populate my second select with as many options as my MySQL query returns.

15
  • Ok so what doesnt work as it is? Commented Apr 17, 2016 at 21:09
  • 4
    juz.value will be undefined because juz is already a string. .val() already extracted the value. Commented Apr 17, 2016 at 21:11
  • 1
    Unfortunately, not right: A malicious user could just send a post request themselves with whatever value they like. Commented Apr 17, 2016 at 21:24
  • 1
    @GonHL Nope, a request could be made direct to your PHP page, or a user could modify the DOM so it sends something else.. Never trust client side input. Also use the @ to tag users. Commented Apr 17, 2016 at 21:26
  • 1
    Stop using the deprecated and as of PHP7 removed mysql_* functions. Migrate to PDO and start using Prepared Statements. Commented Apr 17, 2016 at 21:31

1 Answer 1

0

Change

data: { juz.value },

to

data: { juz },

and echo the HTML

as:

    echo "<span class='input-group-addon' id='basic-addon1' style='border:solid 1px #CCC; border-radius:4px;'>Nominación: </span>
    <select class='form-control' id='nom' name='id_juz_nom'>
    <option>Seleccione Nominación</option>";
 while ($row=mysql_fetch_array($result)) {
    echo "<option value=$row['id_juz_nom']>$row['nominacion']</option>
    </select>";}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.