Click here to Skip to main content
15,846,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to disable a select element if my checkbox has been checked. Just by code using javascript and all, it was successful but it got tricky when I involved some php.

this is my html:
<select id="month">
   <option></option> //data fetch from db
</select>
<input type="checkbox" id="curr" name="pre" onclick="preCheck()" 
value="present" <?php if($row['exit'] == "present"){ echo "checked"; }else{ echo "unchecked"; } ?> >


this is my js:
function preCheck(){
    var curr= document.getElementById('curr');
    if (curr.checked){
        document.getElementById("month").setAttribute("disabled", "disabled");
    }else{
        document.getElementById("month").removeAttribute("disabled");
    }
}


What I have tried:

I had looked up the problem and have noticed that onclick will not work with php but i was wondering if there was a way around it as I am not that skilled in programming
Posted
Updated 2-Jun-23 12:32pm
v2

1 solution

Php, is server sided, all Php instructions are processed on it.
But to interact with Javascript on the Client web page, you can fire Javascript functions,

using a call to your function, with 'echo' and ' ....

PHP
echo "<script> preCheck(); </script>" ;

// that will add this line of JS code to the page to be send,
// and at page load at client side, the function involved will fire.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900