I have two radio buttons and when user clicks on one radio button and clicks button it should stay selected. But what is happening is only last one remains selected even if I select and search with first radio button.
My code:
<input type="radio" <?php if(isset($_POST['gender']) && isset($_POST['gender']) == 'male') echo "checked='checked'"; ?> name="gender" id="male" value="male" onclick="getGender(this.value);" /><strong>Groom</strong>
<input type="radio" <?php if(isset($_POST['gender']) && isset($_POST['gender']) == 'female') echo "checked='checked'"; ?> name="gender" id="female" value="female" onclick="getGender(this.value);" /><strong>Bride</strong>
Thanks in advance.
Just change
&& isset($_POST['gender']) == 'male')
to
&& ($_POST['gender'] == 'male')
and do the same for female
Related
I have this problem: In a form, I want to display the info from a box (idCreditorCnpCui) only if the second or the third option is selected from the form (if "P" or "O" option is selected). While I select any of these options, the box appears as it should. The problem I encountered is that when I press the "sent" button (with O or P option), the idCreditorCnpCui box disappears from the view.
This is the HTML with the options of the form:
<div class="form-field">
<label for="payment.creditorOrganizationOrPerson"><localization:localize key="creditorOrganizationOrPerson.label"/></label>
<div class="form-field-element">
<input type="radio" property="payment.creditorOrganizationOrPerson" value="N" onclick="display();" /> N <br>
</div>
<div class="form-field-element">
<input type="radio" property="payment.creditorOrganizationOrPerson" value="P" onclick="display();" /> P <br>
</div>
<div class="form-field-element">
<input type="radio" property="payment.creditorOrganizationOrPerson" value="O" onclick="display();" /> O
</div>
</div>
This is the box I want to be display or no according the option selected:
<div class="form-field" id="idCreditorCnpCui" style="display:none">
<label for="payment.creditorCnpCui"><localization:localize key="creditorCnpCui.label"/></label>
<div class="form-field-element">
<input type="text" property="payment.creditorCnpCui" size="36" maxlength="35" styleClass="input-field" styleId="payment.creditorCnpCui"/>
</div>
And this is the js
function display(toDisplay,idDiv){
if(toDisplay == false){
document.getElementById(idDiv).style.display = "none";
}else{
document.getElementById(idDiv).style.display = "block";
}
}
display(false,"idCreditorCnpCui");
If I turn the final line to "display(true, "idCreditorCnpCui"), the box appear even when the option "N" is selected by default. That should not happen since the N option means the idCreditorCnpCui box is hidden. Any idea please.
I want a field to be display just if an option is selected. But when I send the form, the field disappears even when the option that display that field is selected
I'm new here and I'm writing my first php code. I want to prepare a multiple choice quiz in which, after click on a button at the end of the page, the radios button (the choices of the test) will remain checked and unchangeable. I simplify the code in this way:
<?php
$choice1 = "a";
$choice2 = "b";
?>
<form method="post" name="testForm" id="testForm">
<ul>
<li><input type="radio" name="q1" value="a"
<?php if(isset($_POST['test-result']) && $radioVal==$choice1){ echo "disabled='disabled' checked";}?>
<?php if(isset($_POST['test-result']) && $radioVal<>$choice1){ echo "disabled='disabled'";}?>>
<?php echo "<span id='question'>". $choice1 ."</span>";?> </li>
<li><input type="radio" name="q1" value="b"
<?php if(isset($_POST['test-result']) && $radioVal==$choice2){ echo "disabled='disabled' checked";}?>
<?php if(isset($_POST['test-result']) && $radioVal<>$choice2){ echo "disabled='disabled'";}?>>
<?php echo "<span id='question'>". $choice2 ."</span>";?> </li>
</ul><br>
<button class='button' name='test-result'>Finaliza el test</button>
</form>
<?php
if (isset($_POST['test-result']))
{
$radioVal = $_POST["q1"];
}
?>
However, after click on the button, all radios remain uncheged (and this is ok) but the choices made are not checked. Can someone help me? Thanks in advance!
Checkbox is not showing checked because variable $radioVal is set after input box. To resolve your issue could you please set $radioVal before from.
In my form, I have x groups of radio buttons generated:
for ( $j = 0; $j < $x; $j++ ) {
<input type="radio" name="gender<?php echo $x; ?>" value="male" />Male
<input type="radio" name="gender<?php echo $x; ?>" value="female" />Female
...
}
When, after submit, I try to retrieve these values:
for ( $j = 0; $j < $x; $j++ ) {
echo $_POST['gender'.$j];
...
}
I get these error messages:
Notice: Undefined index: gender0 in ...
Notice: Undefined index: gender1 in ...
Notice: Undefined index: gender2 in ...
Yet I see these radio groups in my markup correctly defined:
<input type="radio" value="male" name="gender0">Male
<input type="radio" value="female" name="gender0">Female
...
<input type="radio" value="male" name="gender1">Male
<input type="radio" value="female" name="gender1">Female
So why these error messages?
Just make a var_dump($_POST); on the beginning of your receiving script, then you’ll see exactly what you get.
And you are aware that you get values only for radio buttons that are actually checked, right?
So if you can not be certain if the user will have a radio button checked per group, then check whether that POST entry is available before accessing it, using isset or empty.
Btw., an even more convenient way to deal with data like this, is to name the form fields in a way that gets you an array in PHP in the first place:
<input type="radio" name="gender[0]" value="male" />Male
<input type="radio" name="gender[0]" value="female" />Female
<input type="radio" name="gender[1]" value="male" />Male
<input type="radio" name="gender[1]" value="female" />Female
– that will get you an array in $_POST['gender'], that you can loop through with a foreach loop like this:
if(isset($_POST['gender']) && is_array($_POST['gender'])) {
foreach($_POST['gender'] as $index => $value) {
// …
}
}
I have an HTML form with several input boxes and their corresponding enter button. When I type something in the first input box and either press Enter or the button, it works fine. But if I press Enter in the other boxes it works as if it was in the first box. The buttons work OK.
Here is the code I have:
<script type="text/javascript">
function fn (num) {
alert(num);
document.getElementById(num).focus();
}
</script>
</head>
<body>
<form>
<input id="1" type="text"></input> <button onclick="fn(1);">press</button><br/>
<input id="2" type="text"></input> <button onclick="fn(2);">press</button><br/>
<input id="3" type="text"></input> <button onclick="fn(3);">press</button><br/>
</form>
Apparently this is a 'quirk' in ECMA (I have tested in FF18.0.1 and Safari 5.1.7).
Is there a fix for this quirk?
One solution that I found is to add one form per input box.
<form>
<input id="1" type="text"></input> <button onclick="fn(1);">press</button><br/>
</form>
<form>
<input id="2" type="text"></input> <button onclick="fn(2);">press</button><br/>
</form>
<form>
<input id="3" type="text"></input> <button onclick="fn(3);">press</button><br/>
</form>
Is this a bug or a feature? Is there a better solution?
Thanks for any suggestions.
You'll want to use onKeyPress instead of onClick. Try this:
function myKeyPress(args) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
// do something here (enter has been pressed)
}
else {
return false; // somehow prevents "default" form behaviors
}
}
Try this :
$("id").keypress(function(e) {
if(e.which == 13) {
respective functions
}
});
or
<input type="text" name="q" id="q"
onkeypress="if (event.keyCode == 13) {call function}" />
I am trying to place a series of links on one page that, when clicked, will select a default value of a radio group on another page. The perfect example of this is here. By clicking one of the blue links on the far right, they define the default value of the radio group on the next page. That is exactly what I am trying to do.
Can anyone help me understand how they did that?
This isn't 'checking' the box on the other end. The link that you're clicking contains a variable in the URL (GET variable). When the new page loads, it's likely using a server-side scripting language to test for each radio button.
In this case, ?default_amt=5 is what is attached for the $5 contribution. Then, on the page:
<input type="radio" checked="checked" value="5" name="amount" id="amt_preset_1" onclick="BSD.contribution.clearother();" class="radiobutton">
Is the value for the radio button. Simply put something like this php code to 'check' it
<?php if ($_GET['default_amt'] == "5") {echo "checked=\"checked\"; } ?>
The links on the previous page could be this:
Set amount to 1 on new page
Set amount to 2 on new page
Set amount to 3 on new page
Set amount to 4 on new page
'newpage.html' would have the following:
<input type="radio" value="1" name="toPay" <?php if ($_GET['amount'] == "1") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="2" name="toPay" <?php if ($_GET['amount'] == "2") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="3" name="toPay" <?php if ($_GET['amount'] == "3") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="4" name="toPay" <?php if ($_GET['amount'] == "4") {echo "checked=\"checked\"; } ?>>