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>
Related
I am working on a PHP Quiz System which displays all questions on one page.
The following is my code.
PHP
$getQ = mysqli_query($condb, "select * from questions where eid = '$exam'");
if($getQ){
if(mysqli_num_rows($getQ)==0){
$summaryLink = 'examSummary.php?exam='.$exam;
redirect($summaryLink);
}
Form
<?php while($ans = mysqli_fetch_array($getQ)){ ?>
<div class="ques" id="ques">
<input type="hidden" name="exam" value="<?php echo $exam; ?>">
<input type="hidden" name="question" value="<?php echo $token; ?>">
<input type="hidden" name="qid" id="qid" value="<?php echo $ans['qid'];?>">
<input type="hidden" name="sid" id="sid" value="<?php echo $_SESSION['sid']; ?>">
<p>
<input type="radio" name="answer" value="1" id="answer" onChange="update();">
<label for="<?php echo $ans['option_1'];?>"><?php echo $ans['option_1'];?></label>
</p>
<p>
<input type="radio" name="answer" value="2" id="answer" onChange="update();">
<label for="<?php echo $ans['option_2'];?>"><?php echo $ans['option_2'];?></label>
</p>
<p>
<input type="radio" name="answer" value="3" id="answer" onChange="update();">
<label for="<?php echo $ans['option_3'];?>"><?php echo $ans['option_3'];?></label>
</p>
<p>
<input type="radio" name="answer" value="4" id="answer" onChange="update();">
<label for="<?php echo $ans['option_4'];?>"><?php echo $ans['option_4'];?></label>
</p>
<p>
<input type="radio" name="answer" value="5" id="answer" onChange="update();">
<label for="<?php echo $ans['option_5'];?>"><?php echo $ans['option_5'];?></label>
</p>
<div class="status2"></div>
What I want to do is, once the user select an answer for a questions, the selected option and few other hidden form data (qid, eid, sid) should be passed to updateAnswer.php
updateAnswer.php will update the selected answer to the database.
I tired the following jQuery. But it wont do anything.
<script>
function update() {
var radVal = $("input[type=radio][name=answer]:checked").val();
jQuery.ajax({
url: "updateAnswer.php",
data:'eid='+$("#eid").val()+'&sid='+$("#sid").val()+'&qid='+$("#qid").val()+'&answer='+$(radVal).val(),
type: "POST",
success:function(data){
$(".status2").html(data);
},
error:function (){}
});
}
</script>
The system should pass data to updateAnswer.php when a user clicks on an option(answer).
But with my current code, when a user clicks on an option won't do anything.
So, there are some mistakes in your code, first one is that the id should always be unique, so before your while loop take a variable and initialize it to zero and increment it on every iteration; You can use this variable and append after every id to make it unique. Like so -
<?php
$i = 0;
while(bla bla){
$i++;
?>
<!-- You can also use question id here ↓↓ which should be unique -->
<input type="hidden" name="qid" id="<?php echo "qid".$i; ?>" value="<?php echo $ans['qid'];?>">
<!-- session id will be the same -->
<input type="hidden" name="sid" id="sid" value="<?php echo $_SESSION['sid']; ?>">
<!-- same for others -->
<?php
}
?>
You also need to give this($i or question-id(whichever you choose)) as an attribute to radio buttons to identify which question id we need to get and on update function you need to pass the current checked element, like so -
<input type="radio" name="answer" value="1" class="answer" onChange="update(this);" data-id="<?php echo $i; ?>">
<input type="radio" name="answer" value="2" class="answer" onChange="update(this);" data-id="<?php echo $i; ?>">
<!-- same for others -->
I've created a working snippet for your problem(without PHP and AJAX of course). I've given them unique id and values, I'm printing the desired values on the console. I've changed the AJAX code as well, you can use it in your code to check if it works. Comments are mentioned wherever necessary, see if it helps you.
function update(element) {
let answer = $(element).val(); // get the value of checked element
let id = $(element).attr('data-id'); // get id of the radio button clicked
let eid = $('#eid').val(); // exam id will be same for all the questions
let sid = $('#sid').val(); // session id will be same
let qid = $('#qid' + id).val(); // get question id
// send ajax request
/*jQuery.ajax({
url: "updateAnswer.php",
data: 'eid=' + eid + '&sid=' + sid + '&qid=' + qid + '&answer=' + answer,
type: "POST",
success: function(data) {
$('#status' + id).html(data); // show status on the related div
},
error: function() {}
});*/
console.clear();
console.log(`id: ${id}, eid: ${eid}, sid: ${sid}, qid: ${qid}`)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ques" id="ques1">
<input type="hidden" name="exam" value="some_exam" id="some_exam">
<input type="hidden" name="eid" value="some_examid" id="eid">
<!-- question id 1 (use php) -->
<input type="hidden" name="question" id="question1" value="question1">
<input type="hidden" name="qid" id="qid1" value="qid1">
<input type="hidden" name="sid" id="sid" value="some_session">
<p>
<!-- Give question id in data-id attribute (use php) and pass current element in update function -->
<input type="radio" name="answer" value="1" class="answer" onChange="update(this);" data-id="1">
<label>option_1_1</label>
</p>
<p>
<input type="radio" name="answer" value="2" class="answer" onChange="update(this);" data-id="1">
<label>option_1_2</label>
</p>
<p>
<input type="radio" name="answer" value="3" class="answer" onChange="update(this);" data-id="1">
<label>option_1_3</label>
</p>
<p>
<input type="radio" name="answer" value="4" class="answer" onChange="update(this);" data-id="1">
<label>option_1_4</label>
</p>
<p>
<input type="radio" name="answer" value="5" class="answer" onChange="update(this);" data-id="1">
<label>option_1_5</label>
</p>
<div class="status2" id="status1"></div>
---------------------------------------
<!-- Second question starts (incremet i)-->
---------------------------------------
<div class="ques" id="ques2">
<input type="hidden" name="exam" value="some_exam" id="some_exam">
<input type="hidden" name="eid" value="some_examid" id="eid">
<input type="hidden" name="question" id="question2" value="question1">
<input type="hidden" name="qid" id="qid2" value="qid2">
<input type="hidden" name="sid" id="sid" value="some_session">
<p>
<input type="radio" name="answer" value="option_2_1" class="answer" onChange="update(this);" data-id="2">
<label>option_2_1</label>
</p>
<p>
<input type="radio" name="answer" value="2" class="answer" onChange="update(this);" data-id="2">
<label>option_2_2</label>
</p>
<p>
<input type="radio" name="answer" value="3" class="answer" onChange="update(this);" data-id="2">
<label>option_2_3</label>
</p>
<p>
<input type="radio" name="answer" value="4" class="answer" onChange="update(this);" data-id="2">
<label>option_2_4</label>
</p>
<p>
<input type="radio" name="answer" value="5" class="answer" onChange="update(this);" data-id="2">
<label>option_2_5</label>
</p>
<div class="status2" id="status2"></div>
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
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!
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>