jimdgar2
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?
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks for the help. More points awarded to Kumaranmca for providing an example.