i have checkbox summon with forearch like this
foreach( $orderarray as $key => $cucian )
{
switch ($cucian['tipe']) {
case 'cuci dan setrika':
echo "<input type='checkbox' name='cuci' /> Cuci";
echo "<input type='checkbox' name='setrika' /> Setrika";
break;
case 'setrika':
echo "<input type='checkbox' name='cuci' disabled /> Cuci";
echo "<input type='checkbox' name='setrika' /> Setrika";
break;
}
}
i have read this link :
PHP keep checkbox checked after submitting form
and add this
<?php if(isset($_POST['setrika'])) echo "checked='checked'"; ?>
but why after submitting form , all checkbox with name 'setrika' is checked
any method to solve my problem ?
thanks in advance
Either you give different names to your checkboxes, or use the array notation:
<input type='checkbox' name='setrika[]'>
Notice the [] brackets: you'll have to iterate over $_POST['setrika'], which will be an array:
$_POST['setrika'][$n]
grants you access to the $n-th position of your array.
Related
I have tried:
<input disabled='disabled'
checked='checked'
type='checkbox'
name='checkTema[]'
value=".$cat->cat_ID."> ".$cat->name."<br>"
But that won't pick up the value when I send the form
UPDATE
Thanks to a comment I've got a helping answer from another SO question, yet I am not sure how I should be applying the trick, tried the following but not sending the value
echo "<input type='hidden' name='checkTema[]' value='1' >";
echo "<input type='checkbox' checked='checked' disabled='disabled' name='checkTema[]' value=".$cat->cat_ID."> ".$cat->name."<br>";
Try return false in onclick event
<input
onclick="return false"
checked='checked'
type='checkbox'
name='checkTema[]'
value='.$cat->cat_ID.'> ".$cat->name."<br>"
If you need to POST disabled checkboxes, I suggest using a hidden input before:
<input type="hidden" name="checkbox_disabled" value="<?php echo $checkbox_value; ?>" /> <== Checked and disabled
Then in your PHP code, you can simply check:
if ($_POST['checkbox']) {
//Checkbox wasn't disabled on submission
} else {
//Fallback to using the disabled value:
$disabledVal = $_POST['checkbox_disabled'];
}
Additionally, you can simply undisable all the elements of the form before submission:
$('form').submit(function() {
$(this).find('input').attr('disabled', false);
});
See this question/answer for more information on how you can alternatively do this.
Try this code :
<input disabled='disabled' checked='checked' type='checkbox' name='checkTema[]' value="<?php echo $cat->cat_ID;?>"><?php echo $cat->name;?>
Thanks to a comment, I've got the solution in another SO question
Then my code changed to:
echo "<input type='hidden' name='checkTema[]' value=".$cat->cat_ID." >";
echo "<input type='checkbox' checked='checked' disabled='disabled'> ".$cat->name."<br>";
My table consists of 15 records which divided in 2 pages, however, those radio buttons in first page will not be checked as shown in image below.
Output
Here the code used in view page:
<?php foreach($rights->result() as $row){
echo "<label class='radio-inline'><input type='radio' name='rightRBtn[".$perm_id."]' value='$row->id'".(($row->id == $right_id) ? " checked='checked'":'')." />" . $row->description."</label>";
}?>
Browser source view shows the radio button box is actually checked too.
<td><label class='radio-inline'><input type='radio' name='rightRBtn[22]' value='0' checked='checked' />none</label><label class='radio-inline'><input type='radio' name='rightRBtn[22]' value='1' />view</label> </td>
Does anyone know what is the problem?
Simplify foreach() loop code by apply simple if-else and save yourself from some unnecessary quotes adjustment.
<?php foreach($rights->result() as $row){
if($row->id == $right_id){
echo "<label class='radio-inline'><input type='radio' name='rightRBtn[".$perm_id."]' value='$row->id' checked/>" . $row->description ."</label>";
}else{
echo "<label class='radio-inline'><input type='radio' name='rightRBtn[".$perm_id."]' value='$row->id'/>" . $row->description ."</label>";
}
}
?>
At my local end it's working:-
Code i used:-https://prnt.sc/ht9mzb (i just use json_decode and json_encode to make it stdclassobject array as you have)
output:-https://prnt.sc/ht9m1n
My workaround for this issue is to remove the responsive: true
$('#dataTables-example').DataTable({
// responsive: true
});
I have a textfile with information that I am using to create a quiz. I am only displaying one quiz question at a time however. I am generating radio buttons based on the number of questions and then I have a submit button after the loop that generates the radio buttons. I can't figure out how to get which radio button the user selected in the $_POST array when I click submit. My initial thought is to use a form tag and then have the loop run but I don't know if this works or how to make it work syntactically.
textfile (the last number is the index of the right answer):
What does charmander evolve to?#Charmeleon:charizard:squirtle#0
WHo is the main character in Pokemon?#Misty:Ash:Obama#1
Script:
<?php
$indexTemp = intVal($_SESSION["index"]);
if($_SESSION["numQuestions"] == $indexTemp){
echo "Your Results are: ";
echo "<form action=\"process.php\" method=\"post\"> Back to Main screen: <input type=\"submit\"><br \> </form>";
exit();
}
$filename = $_SESSION["quizOfTheDay"];
$quizStuff = file($filename);
$ctr =1;
$questionInfo = $quizStuff[$indexTemp];
$questionParse = explode("#", $questionInfo);
$_SESSION["correctAns"] = $questionParse[2];
echo $_SESSION["correctAns"]." from line 55 <br />";
$answerChoices = explode(":",$questionParse[1]);
echo "$questionParse[0] ? <br />";
#This is where the radio buttons are being generated
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans$ctr' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}
$_SESSION["index"] = $indexTemp +1;
echo "<form action=\"questions.php\" method=\"post\"> <input type=\"submit\"><br \> </form>";
?>
Obtaining data from radio buttons and checkboxes can be a little tricky, generally due to a lack of understanding of how radio buttons and checkboxes work.
It is important to remember two facts:
Checkbox "Names" Must Be Unique to Each Checkbox
Radio Button "Names" Must Be Identical For Each Group of Buttons
Update the foreach since you must have same name for all the radio buttons but with different values.
<?php
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value=".$ctr."> <label for='q1'>".$answerChoice."</label> <br />";
$ctr +=1;
}
?>
Now your for-each concatenation also looks wrong and i have updated it and since the concatenation is wrong the value will not be displayed in the radio button. The radio button value will be the count of the increment variable.
remove $ctr
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}
i have few answers to quiz stored in db. i am displaying them as radio buttons on form like this
$row = mysql_fetch_array(mysql_query('select * FROM quiz order by rand() limit 1'),MYSQL_ASSOC);
<?php
foreach ($row as $key => $value) {
echo "<input type='radio' name='answer'>".$value."</input><br />";
}
?>
but when i post this form the echo on other end is not getting a radio button answer i selected. please help
echo "<input type='radio' name='answer' value='".$value."'>".$value."</input><br />";
The only things that I can see is that
a) There is no form tag outside of the radio
b) You aren't giving the radio a value such as:
<input type='radio' name='answer' value="1"> Male
<input type='radio' name='answer' value="2"> Female
When I pass the html form having checkboxes to a php page, when I retrieve them I get value true for all the boxes that I have checked, and false for unchecked boxes. I need to get the value only.
<?php
$contact=$_POST['contact'];
foreach ($contact as $conid){
echo $conid
}
?>
One possible approach is to set names like that:
<input type='checkbox' name='box[1]'>
<input type='checkbox' name='box[2]'>
<input type='checkbox' name='box[3]'>
This way you could access them in PHP with
foreach ($_POST['box'] as $id=>$checked){
if ($checked =='on')
//process your id
}
The second approach is more clean, I think. Set value's attributes on checkboxes:
<input type='checkbox' name='box[]' value='1'>
<input type='checkbox' name='box[]' value='2'>
<input type='checkbox' name='box[]' value='3'>
With this you'll receive only checked values:
foreach ($_POST['box'] as $id){
//process your id
}