First radio button only taken
<div class="field_radio" name="preference" >
<input class="radio1" type="radio" name="preference" id="preference"
value="team" onclick="ShowHideDiv()" /><label for="radio1">
<span>Team</span></label>
<input class="radio2" type="radio" name="preference" id="preference"
value="individual" onclick="ShowHideDiv()" /> <label for="radio2">
<span>Individual</span></label>
</div>
Php section
if (empty($_POST["preference"])) {
$errorMSG .= "preference is required ";
} else {
$preference = $_POST["preference"];
}
you took same id on both input field
<div class="field_radio" name="preference">
<input class="radio1" type="radio" name="preference" id="preference_one" value="team" onclick="ShowHideDiv()" />
<input class="radio2" type="radio" name="preference" id="preference_two"
value="individual" onclick="ShowHideDiv()" />
</div>
<div class="field_radio">
<input type="radio" class="radio1" name="preference" id="preference1" value="team" onclick="ShowHideDiv()" />
<label for="radio1"><span>Team</span></label>
<input type="radio" class="radio2" name="preference" id="preference2" value="individual" onclick="ShowHideDiv()" />
<label for="radio2"><span>Individual</span></label>
</div>
Note: Ids of the radio buttons are same make it different will solve your problem!
Related
I made radio buttons and all radio buttons are selected, where I try my code.
How to choose just one radio button in sintaks laravel?
This is My View Page :
<div class="form-group">
<b>Paket</b>
<br/>
<fieldset>
<input type="checkbox" name="delux" id="delux" value="d"> <label for="">Paket Delux </label>
<input type="checkbox" name="paket1" id="p1" value="p1"> <label for="">Paket 1</label>
<input type="checkbox" name="paket2" id="p2" value="p2"> <label for="">Paket 2</label>
</fieldset>
</div>
<div class="form-group">
<b>Jenis Pembayaran</b>
<br/>
<fieldset>
<form id="form_radio" name="form_radio">
<input type="radio" value="tunai" name="tunai" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="nontunai" id="rd2"> <label for="">non tunai</label>
</fieldset>
</div>
<input type="submit" value="Upload" class="btn btn-primary">
</form>
And this is My controller :
public function input()
{
$jenis = JenisMkn::select('id_jenis','jenis_makanan')->get();
return view('upload_gambar',['jenis'=>$jenis]);
}
public function proses(Request $request)
{
$cek = Gambar::get('checkbox');
echo $cek;
$radio = Gambar::get('radio');
echo $radio;
What the fault in my code?
Any help? Thank you.
Simply give them the same name, cek this code:
<input type="radio" value="tunai" name="transaksi" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="transaksi" id="rd2"> <label for="">non tunai</label>
You can rename the name input with your name.
If you want only one radio button to get selected then you must specify same value for name property. for example:
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
Hope it helped.
I'm new to php and html and I was wondering, how having a code like below:
<!DOCTYPE html>
<html>
<body>
<form>
<p>Question Number 1</p>
<input type="radio" name="question1" value="A">A<br>
<input type="radio" name="question1" value="B">B<br>
<input type="radio" name="question1" value="C">C<br>
<input type="radio" name="question1" value="D">D
<p>Question Number 2</p>
<input type="radio" name="question2" value="A">A<br>
<input type="radio" name="question2" value="B">B<br>
<input type="radio" name="question2" value="C">C<br>
<input type="radio" name="question2" value="D">D
<p>Question Number 3</p>
<input type="radio" name="question3" value="A">A<br>
<input type="radio" name="question3" value="B">B<br>
<input type="radio" name="question3" value="C">C<br>
<input type="radio" name="question3" value="D">D
<p>Question Number 4</p>
<input type="radio" name="question4" value="A">A<br>
<input type="radio" name="question4" value="B">B<br>
<input type="radio" name="question4" value="C">C<br>
<input type="radio" name="question4" value="D">D
</form>
</body>
</html>
I want to show a question one bye one with button next and with check if
a value is null. What should I use? PHP or Javascript.
$('input[type="radio"]').on('change', function(e) {
$('#'+e.target.name+'').hide();
var nextRadioGroupName = "question" + (parseInt(e.target.name.replace('question', '')) + 1);
$('#'+nextRadioGroupName+'').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<form>
<div id = "question1">
<p>Question Number 1</p>
<input type="radio" name="question1" value="A">A<br>
<input type="radio" name="question1" value="B">B<br>
<input type="radio" name="question1" value="C">C<br>
<input type="radio" name="question1" value="D">D
</div>
<div id = "question2" style="display:none">
<p>Question Number 2</p>
<input type="radio" name="question2" value="A">A<br>
<input type="radio" name="question2" value="B">B<br>
<input type="radio" name="question2" value="C">C<br>
<input type="radio" name="question2" value="D">D
</div>
<div id = "question3" style="display:none">
<p>Question Number 3</p>
<input type="radio" name="question3" value="A">A<br>
<input type="radio" name="question3" value="B">B<br>
<input type="radio" name="question3" value="C">C<br>
<input type="radio" name="question3" value="D">D
</div>
<div id = "question4" style="display:none">
<p>Question Number 4</p>
<input type="radio" name="question4" value="A">A<br>
<input type="radio" name="question4" value="B">B<br>
<input type="radio" name="question4" value="C">C<br>
<input type="radio" name="question4" value="D">D
</div>
</form>
Firstly add a div element for per question group to easily show/hide them
Define change event with Jquery and find the next question group as iteration like "question1", "question2" etc.
Then show the next question group
Code for RadioButton:
<h1 style="margin:0; margin-top:10px; padding:0; padding-left:25px; padding-bottom:10px; font-family:sans-serif;">
</h1>
<div style="background:#1794FF; color:#fafafa; padding:10px;">
<h3></h3>
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" />
<label for="radio3" class="css-label">Near Zipcode</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" />
<label for="radio1" class="css-label">City-Wide</label>
</td>
</tr>
</table>
</div>
<div style="background:#1794FF; color:#222; padding:10px;">
Php Code:
<?PHP
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
After a form submission I arrive at the code above. However, it says the value is "on". Why isn't it printing the chosen radio button name?
May you can assign a value:
PHP Code
<form method="POST">
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="1" />
<label for="radio1" class="css-label">Neighbourhood Only</label><br>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked" value="2"/>
<label for="radio2" class="css-label">Zipcode Only</label><br>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" value="3" />
<label for="radio3" class="css-label">Near Zipcode</label><br>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" value="4" />
<label for="radio1" class="css-label">City-Wide</label><br>
<input type="submit" value="Submit">
</form>
PHP Code:
<?php
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
A radio button is has a default value of "on", you should specify your value in your input initialization like so:
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" value="Your Value Here" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
So you can replace "Your Value Here" with "Zipcode Only" for this one. The label is just to describe the radio button value for the front-end user.
Use a value attribute on your input radio.
Use form method "post" and also put value for each radio:
<form method="post">
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="Neighbourhood Only" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<tr>
</table>
<input type="submit" />
</form>
I am writing a php site that has a form with a series of check boxes. I will be loading an array from a file that I would like to go through and check some of the boxes by default when the form is loaded.
Here is an example:
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" />
</form>
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
Is this possible? What would be the best way to do it.
You should fill your array before the HTML part. And then:
<input type="checkbox" name="option1" value="option1" <?php if (in_array("option1", $array)) { echo 'checked="checked"'; } />
Try this :
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" <?php if(in_array("option1",$array)){?> checked="checked"<?php}?> />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" <?php if(in_array("option2",$array)){?> checked="checked"<?php}?> />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" <?php if(in_array("option3",$array)){?> checked="checked"<?php}?> />
</form>
How would i get the value of a radio button and use that in my session so for example;
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage <?= isset($_SESSION['minor']) ? "style=\"display: block;\"" : "style=display: none;\"" ?>">
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1');?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2');?>" />
</div>
as you can see the code calls the name "minor" but even if NO is selected it will still show the div, regardless of the value, i need to pass the value into my isset($_Session['minor']) parameter, how would this be achieved ?
#Xavier: Try --
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage"<?php echo isset($_SESSION['minor']) && $_SESSION['minor'] != 'no' ? 'style="display: block;"' : 'style="display: none;"' ?>>
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1'); ?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2'); ?>" />
</div>