Radio button checked but not showing as checked - php

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
});

Related

How to make a checkbox checked and disabled?

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>";

onclick action not working as intended with radio buttons

For the last 4 hours I've been struggling to get something to work. I checked SO and other sources but couldn't find anything related to the subject. Here is the code:
<?php
$email=$_SESSION['email'];
$query1="SELECT * FROM oferte WHERE email='$email'";
$rez2=mysql_query($query1) or die (mysql_error());
if (mysql_num_rows($rez2)>0)
{
while ($oferta = mysql_fetch_assoc($rez2))
{
$id=$oferta['id_oferta'];
echo "<input type='radio' name='selectie' value='$id' id='$id'> <a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo "</br>";
}
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
echo "</form>";
echo "</div>";
}
else
{
}
?>
The while drags all of the user's entries from the database and creates a radio button for each one of them with the value and id (because I don't really know which one is needed) equal to the entry's id from the db. I echoed that out and the id is displayed as it should so no problems there.
The delete script works ok as well so I won't attach it unless you tell me to. All good, no errors, until I try to delete an entry. Whatever I choose from the list of entries, it will always delete the last one. Note that I have two other inputs echoed out, those will be the "view" and "modify" buttons for the entry.
I really hope this is not JavaScript related because I have no clue of JS. I think this will be of major help to others having this problem. Please let me know if I need to edit my question before downrating. Thanks!
After edit:
This is the delete script, which as I said earlier works fine.
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
echo $id;
require_once('mysql_connect.php');
$query = "DELETE FROM oferte Where id_oferta = '$id'";
mysql_query($query) or die(mysql_error());
//header('Location: oferte.php');
}
else
{
//header('Location: oferte.php');
}
?>
The session is started as well, like this:
<?php
session_start();
?>
The reason the last $id is deleted is because this line is outside/after the while loop:
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
You want to move this line inside the loop so that you have a button that executes delete for each radio button.
Update:
To have links to delete and
echo "<input type='radio' name='selectie' value='$id' id='$id'> ";
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
Also I do not think the radio button is needed here at all since you are not really doing anything with it. You could simply echo out the value of your choice and have these links as follows:
echo $oferta['denumire_locatie'] . ' '; // replace $oferta['denumire_locatie'] with something of your choice
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
echo "<br />";
The problem, in this case, is JavaScript related, yes. What I recommend you to do is to simply add a Remove link for each item.
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo " - <a href='delete.php?id={$oferta['id_oferta']}'>Remove</a>";
echo "</br>";
Your $id is outside your while() loop.
The last one is getting deleted because the $id has the last one's value when the loops is exited.
Include all your code :
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
Inside your while loop.
When the rendered html reaches the browser, it will be something like this:
<input type='radio' name='selectie' value='1' id='1'> <a href='oferta.php?id=1'>TEXT</a>
<input type='radio' name='selectie' value='2' id='2'> <a href='oferta.php?id=2'>TEXT</a>
<input type='radio' name='selectie' value='3' id='3'> <a href='oferta.php?id=3'>TEXT</a>
<input type='radio' name='selectie' value='4' id='4'> <a href='oferta.php?id=4'>TEXT</a>
<br/>
<input type="button" id="sterge" value="Sterge" onclick="window.location.href='delete.php?id=5'" />
With this markup you won't be able to accomplish what you want without using javascript to update the onclick attribute whenever you select a radio button.
On the other hand, instead of using the client-side onclick event you can use the button's default behaviour, which is to submit the form.
You'll just have to set the action attribute:
<form method="post" action="http://myurl.php">
and write the myurl.php page which will just read the posted variable $_POST['selectie'] and call the delete method with the posted id.

How to Keep Checkbox checked when summon with foreach

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.

sql display data in radio buttons

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

How to disappear one checkboxs after it is checked

I am making a table for booking time, I have put times as checkboxs, checboxs are all from one form, and I wand that user can select one checkbox and after submit the form, checkbox becomes unselectable or disappears from. I tried to validate if data is found on database and then make it disappear or unselecteble, but it was not successful.
my table looks like this:
http://i.imgur.com/hQdbl.jpg?1
Actually date and time come from another table on database. This is my php code for each check bos:
echo"
<input type='radio' name='datetime' value='". $row['day1']." ".$row['time1']."'>
<p>". $row['day1'] ,"</p><br/>". $row['time1'] ."
can you please help me :-\
If I got your question right you meant something like this?
if($_POST['datetime'] != ($row['day1'] . " " . $row['time1'])) {
echo"
<input type='radio' name='datetime' value='". $row['day1']." ".$row['time1']."' disabled>
<p>". $row['day1'] ,"</p><br/>". $row['time1'] ."
// etc
} else {
echo"
<input type='radio' name='datetime' value='". $row['day1']." ".$row['time1']."'>
<p>". $row['day1'] ,"</p><br/>". $row['time1'] ."
}

Categories