Skip to content Skip to sidebar Skip to footer

Dynamically Update Dropdown Based On Previous Selection With Php Pdo

This has been answered before, however I'm asking again for two reasons: I can't find any resources that utilize PDO, and regardless of that, all of the ones I've found consist of

Solution 1:

The onchange call should be on the select element not on the label

<labelclass="col-sm-2 form-control-label">Codigo Productor (*)</label><selectname="vendedor_codigo onchange="productorInfo(this.value)">

But also it occurs to me you may not quite understand the process. Your ajax call won't be fired when the page loads so this bit:

<selectid="ajax-vendedor"name="vendedor_nombre"><?phpforeach ($ajax_productor_resultas$dd_productor_display) : ?><optionplaceholder="Seleccione codigo"value="<?=$dd_productor_display['vendedor_nombre']; ?>"><?=$dd_productor_display['vendedor_nombre']; ?></option>

i would think is giving you undefined variable warnings (unless you are setting $ajax_productor_result initially in some way)

Responses from ajax are usually drawn in .js via success: function

(result) {
            $("#ajax-vendedor").html(result);
        }

from the looks of this though - unless there is more code that what has been posted, you are passing the .html() function an array or database rows so it's never going to display anything.

so you need to 1)draw a select with no options in it on pageload (or default options if you have them) 2)return a response that the success function can make use e.g. a json string which jquery can the parse 3)format the data in jquery into the <options> and then user the .html() function to update the select 4)if you want this to happen when the page initially loads then add in a document ready call to the productorInfo(id) function - this would be relevant if you are setting the initial select value in some way (so it may not be relevant to you)

Post a Comment for "Dynamically Update Dropdown Based On Previous Selection With Php Pdo"