3

So I have a PHP backend that pulls some data from SQL, let's just say its a list of user ID numbers.

I want to be able to display that list in an html select, via jquery, after a button click.

In an attempt to partially answer my own question, I assume that I could either have a jquery function perform an ajax request, grab the data from PHP/SQL, and then somehow spit out the select with jquery. Or, I could perhaps do the SQL query via PHP right there on the page, and somehow have the jquery function grab the output from that and put it into a select.

How would you do it?

a fill-in-the-blanks code example follows:

idea 1:

function button_click() {


               $.ajax({
                url: "PHP_backend.php",  // this does the sql query and returns the results
                type: 'POST',
                data: 'returnquery',

                    success: function(result) {
                    //?????  put the result array or whatever into a submit, perhaps with a foreach or something similar..??

                    }
                }); // end ajax    

            }

Or idea 2:

$result = mysql_query("SELECT userIDnumbers FROM users",$db);
while ($row = mysql_fetch_array($result)){
/// throw these results into an array or similar, $userIDarray[]
/// maybe I could have this PHP create hidden html fields for each row, and insert its value, and then get that via jquery
}

function button_click() {
///  create the html select, displaying the values from the sql query
/// get values from hidden html fields?
}

2 Answers 2

2

if you are sure that the button will be clicked always or very most of time, idea2 is better becouse overhead of send/receive Ajax (trafic) and its delay (time) will be removed

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. With that in mind, what would be the best way to move the data from the php/sql query into jquery? Short of doing the hidden html values, I don't know of another way to move data between the two (I'm a newbie).
@jeremy, JSON is the best way to move every data(2D or more-dimention array) like query result (table) into javascript via ajax. for more details and an example see this
0

if the web page is "public" (not for an intranet, behind a vpn), I strongly advise to not use any sql in jquery. It's simplistic to call the php ajax response file with arbitrary sql (ie what I want), and even modify anything in the data or database.

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.