Link to home
Create AccountLog in
Avatar of jimdgar2
jimdgar2Flag for United States of America

asked on

How to update form data from MySQL with OnClick

I'd like advice on the cleanest method to achieve the following, using only Javascript & PHP.

I have a web page with form data which is initially blank. When the user selects from a pulldown (constructed with <select> ... <option>) I want to look up data from MySQL and populate the form. I know how to use OnClick to run a Javascript function, I know how to use Javascript to write data back to the form, and I know how to use PHP to extract data from the MySQL database. How do I combine these?

Simplified code:

    <script type="text/javascript">
      function fillData (rSelect) {
        var tempvar = rSelect.form.Data.value;

/* What I'd really like is to stick some php code here to extract the data from mysql and populate
   the variable(s) like tempvar, but I know that isn't how it works. I'm looking for an equivalent. */

        rSelect.form.Field1.value = tempvar;
      }
    </script>
...
          <form method="post" action="newdata_entry" id="my_form">

          <select size="1" name="Data" OnChange="fillData(this)">
            <option>Text1</option>
            <option>Text2</option>
          </select>
          <textarea rows="20" cols="175" name="Field1" id="my_message"></textarea>
...

Can I use JS to fill variables, then reload the web page and have php run the 2nd time through?

SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of jimdgar2

ASKER

OK, spent 2-3 hrs here and there trying to figure out how to use ajax and finally had to give up (for now). I'm finding it much harder to get my head around compared to php & javascript. I decided this implementation could stand a page reload so I'm just going with OnChange launching php and then refreshing the page. Will get back to ajax when I have time for a more elegant solution.

Thanks for the help. More points awarded to Kumaranmca for providing an example.