mysql - populating different data with select onChange in php -


I need some explanation about how to populate selection (s) with data from mysql basically i What I'm trying to do:

This will be the first selection box with some data.

  & lt; Select & gt; & Lt; Options & gt; 1 & lt; / Options & gt; & Lt; Options & gt; 2 & lt; / Options & gt; & Lt; Options & gt; 3 & lt; / Options & gt; & Lt; / Select & gt;  

When the user first chooses an option in the selection, then there is a second selection below it, which should reflect the value according to the selection made in the first selection.

<> pre & lt; Select & gt; & Lt; Options & gt; 1.1 & lt; / Options & gt; & Lt; Options & gt; 1.2 & lt; / Options & gt; & Lt; Options & gt; 1.3 & lt; / Options & gt; & Lt; / Select & gt;

The data is common with MySQL. I'm not sure that you need to post on the same page, but if I do, how can the selected values ​​be saved in the previous selection box?

Thank you.

You should use javascript so that you do not need a page refresh. I recite your question again and in order to draw dynamic data, each other will have a solution to an AJAX request:

HTML

  & Lt; Select name = "select1" id = "select1" & gt; & Lt; Option value = "1" & gt; 1 & lt; / Options & gt; & Lt; Option value = "2" & gt; 2 & lt; / Options & gt; & Lt; Option value = "3" & gt; 3 & lt; / Options & gt; & Lt; / Select & gt; & Lt; Select name = "select2" id = "select2" & gt; & Lt; Option value = "1" & gt; 1 & lt; / Options & gt; & Lt; Option value = "2" & gt; 2 & lt; / Options & gt; & Lt; Option value = "3" & gt; 3 & lt; / Options & gt; & Lt; / Select & gt;  

jQuery

  & lt; Script type = "text / javascript" & gt; $ (Document) .ready (function () {$ ('# select1') .changes (getDropdownOptions);}); Function getDropdownOptions () {var val = $ (this) .val (); // populate.php $ .post ('populate.php', {value: val}, populateDropdown, 'html') to fire a post request; } Function populateDropdown (data) {if (data! = 'Error') {$ ('# select2'). Html (data); }} & Lt; / Script & gt;  

populate.php

  & lt ;? Php if empty ($ _ POST ['value'])) // Query for option based on pricing = sql = 'SELECT * from table WHERE value =' mysql_real_escape_string ($ _ post ['value']); // Repeat your results and make html output ..... // Return HTML option output $ html = '& lt; Option value = "1" & gt; 1 & lt; / Option & gt; '; $ Html = '& Lt; Option value = "b" & gt; B & lt; / Option '; Die ($ HTML); } Die ('error'); ? & Gt;  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -