html - How to capture onchange or onclick event in radio button group in PHP code?

I can't quite get this figured out and maybe I am going about it the wrong way?? (more than likely!) Here is what I would like to do: I have a php webpage with 2 radio buttons and when someone changes the value a couple labels will change and then set a variable. I have the code written so that when the page loads, it will check the correct value based on the value stored. My thought was that in the onchange() function I will just need to get the radio button that is checked and store the variable and refresh the page? If that isn't the best method, I am open for suggestions. Here is what I have:
<script>
function displayRadioValue() {
var ele = document.getElementsByName('country');
for(i = 0; i < ele.length; i++) {
if(ele[i].checked)
<? $COUNTRY="US"; ?>
}
}
</script>
<?php
$COUNTRY = urldecode($pluginSettings['COUNTRY']);
?>
Select your country
<input type="radio" id="us" name="country" value="US" onclick="displayRadioValue()" <? if ($COUNTRY=="US") echo "checked"; ?>>
<label for="us">US</label>
<input type="radio" id="other" name="country" value="Other" onclick="displayRadioValue()"<? if ($COUNTRY=="Other") echo "checked"; ?>>
<label for="other">Other</label><p/>
<? if ($COUNTRY=="US"){
echo "City:";
} else {
echo "Latitude";
}
?>
<input type="text" name="CITY" size="16" value="<? echo $CITY; ?>"> <p>
<? if ($COUNTRY=="US"){
echo "State:";
} else {
echo "Longitude";
}
?>
<input type="text" name="STATE" size="12" value="<? echo $STATE; ?>"> <p>
What am I doing wrong?
Answer
Solution:
try this bro
if(ele[i].is(':checked'))
this return true if radio button is checked else false
Answer
Solution:
give the same id to both radio inputs like this
<input type="radio" id="us" name="country" value="US" onclick="displayRadioValue()" <? if ($COUNTRY=="US") echo "checked"; ?>>
<label for="us">US</label>
<input type="radio" id="us" name="country" value="Other" onclick="displayRadioValue()"<? if ($COUNTRY=="Other") echo "checked"; ?>>
if (document.getElementById('us').checked) {
country = document.getElementById('us').value;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illuminate\http\exceptions\posttoolargeexception
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.