php - Call Javascript Function Based on Radio Button Checked Value
Get the solution ↓↓↓I have a form that sets default values based on a user's account settings (stored in MySQL DB and pulled using PHP). When the form is displayed, it pulls the relevant account information and populates the form (First name, last name, email, account type, etc.)
When a user's account type is 'admin' I want the rest of the form to become disabled, and have certain options selected. If the user's account type is 'admin', and therefore the 'edit_type_admin' radio button checked by default (see below) I would like to call the makeEditDisable() function below as the page is loaded (without requiring any clicks).
Right now the form displays the account type as follows:
Client or Admin: <br />
Client <input type="radio" name="edit_type" id="edit_type_client" value="client" <? if($selected_type == 'client') { echo 'checked'; } ?> onclick="makeEditEnable()" />
Admin <input type="radio" name="edit_type" id="edit_type_admin" value="admin" <? if($selected_type == 'admin') { echo 'checked'; } ?> onclick="makeEditDisable()" />
When toggling between the two options the onclick makeEditEnable()/makeEditDisable() functions work correctly:
<script type="text/javascript">
function makeEditDisable(){
var x=document.getElementById("edit_access_begin")
x.disabled=true;
var x=document.getElementById("edit_access_expire")
x.disabled=true;
var x=document.getElementById("edit_project_access_yes")
x.checked=true;
var x=document.getElementById("edit_project_access_yes")
x.disabled=true;
var x=document.getElementById("edit_project_access_no")
x.disabled=true
}
function makeEditEnable(){
var x=document.getElementById("edit_access_begin")
x.disabled=false;
var x=document.getElementById("edit_access_expire")
x.disabled=false;
var x=document.getElementById("edit_project_access_yes")
x.disabled=false;
var x=document.getElementById("edit_project_access_no")
x.disabled=false
}</script>
Answer
Solution:
Add this to a script or function that loads after the elements have loaded, and of course aftermakeEditDisable()
has loaded:
<? if($selected_type == 'admin') { echo 'makeEditDisable();'; } ?>
This should call your function.
Answer
Solution:
You can add a function call to your page using PHP like @Jeffman answer.
But make sure you add this like after the script itself or it will generate inundefined function
error on the client.
or you can call it on the page onload or onDOMReady event.
I also want to suggest a cleanup to your script http://jsfiddle.net/DHxvk/
var editableFields = ['edit_access_begin','edit_access_expire','edit_project_access_yes','edit_project_access_no'];
function makeEditDisable(){
for(var i=0; i<editableFields.length; i++)
document.getElementById(editableFields[i]).disabled=true;
}
function makeEditEnable(){
for(var i=0; i<editableFields.length; i++)
document.getElementById(editableFields[i]).disabled=false;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.