How to make a checkbox checked and disabled? - php

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

Related

Radio button checked but not showing as checked

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

Hidden value requires to be send twice

Basically I make a calculation and use a hidden value to put it into an input field.
So I've tried to recreate my problem in the code below as to not give away the actual code I'm working with since it's a bit more sensitive.
The question is whether it's possible to get the hidden value inside the disabled box without having to click the send button twice.
If I'm asking the impossible just say so I'll figure something out.
<form action='test.php' method='post'>
<?php
#$result = #$_POST['number1'] * #$_POST['number2'];
echo "<input type='text' name='number1'>
<input type='text' name='number2'>
<input type='text' value='"; if(isset($_POST['value'])) echo $_POST['value']; echo"' disabled>
<input type=hidden name='value' value='" . $result . "'>"
?>
<br>
<input type=submit>
</form>
do you expect the $result in the disabled input if the $_POST["value"] is not set?
echo "<input type='text' value='" .(isset($_POST['value'])?$_POST['value']:$result)."' disabled>";

Javascript button wont work or show

I have been trying for few hours now to try and get this working. However, the code is working fine but the second button just does not show on my website. Could you please help?
echo "<td><input class=button_normal type=button value=Google Renter onclick=window.window.open(href='https://www.google.co.uk/')";
echo "<input class=button_normal type=button value=Yahoo onclick=window.window.open(href='https://www.yahoo.co.uk')</td>";
You haven't added quotations to onclick, value and class. You also forgot to close the input tag.
echo "<td><input class='button_normal' type='button' value='Google Renter' onclick='window.window.open.href=\'https://www.google.co.uk/\''/>";
echo "<input class='button_normal' type='button' value='Yahoo' onclick='window.location.href=\'https://www.yahoo.co.uk\''/></td>";
What I suggest is this:
<script>
function goToYahoo() {
window.open('https://www.yahoo.co.uk');
}
function goToGoogle() {
window.open('https://www.google.co.uk');
}
</script>
<?php
echo "<td><input class='button_normal' type='button' value='Google Renter' onclick='goToGoogle()'/>";
echo "<input class='button_normal' type='button' value='Yahoo' onclick='goToYahoo()'/></td>";
?>
window.location is a property not method:
window.location(href='https://www.yahoo.co.uk')
Should be:
window.location.href='https://www.yahoo.co.uk'
Basically in your code you are missing input tag closing "/>"
echo <<<"FOOBAR"
<td>
<input class="button_normal" type="button" value="Google Renter" onclick="window.open('https://www.google.co.uk/')"/>;
<input class="button_normal" type="button" value="Yahoo" onclick="window.open('https://www.yahoo.co.uk')"/>
</td>
FOOBAR;

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.

Problem in sending values between pages in PHP

I want to send data from one page to the other via a form in PHP. In the initial page I have included the following PHP script that generates an input form with hidden fields. The names of these hidden fields are generated by the PHP:
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='$software'></input>";
echo "<input type='hidden' name='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
In the second page, named process-results.php, I would like to get the names of these hidden fields via the $_GET method but of course using $_GET[$software] and $_GET[$version] wouldn't work...Can someone tell me if there is a solution to this issue or if there is a better alternative? Thanks in advance
Instead of
"<input type='hidden' name='$software'></input>";
you should use
"<input type='hidden' name='software' value='".$software."'></input>";
for each. This way, you can use $_GET['software'] to retrieve the value. Do this for each of your hidden inputs.
I think you may want something like:
<form ... >
<input type="hidden" name="software" value="<?php echo $software ?>" />
<input type="hidden" name="version" value="<?php echo $version ?>" />
</form>
and then
$_GET['software'];
$_GET['version'];
I'm not sure what you're trying to accomplish, but this looks odd to me. Isn't the below code more of what you're looking for?
<?php
echo "<form class='available-form' name='available_os' method='get' action='process-results.php'>";
echo "<input type='hidden' name='software' value='$software'></input>";
echo "<input type='hidden' name='version' value='$version'></input>";
echo "<input type='submit' name='available-button' value='Find Available Libraries for this Software'></input>";
echo "</form>";
?>
That way you will get a query string in form of ?software=yoursoftwarename&version=yourversion and it will be available via $_GET["software"] and $_GET["version"] on the next page.
You could iterate over each of the items in the $_GET array on process-results.php. The problem is that the keys for the value will be whatever $software and $version are set to on the first page. Try something like this:
foreach($_GET as $key=>$string) {
// Do stuff with them
}
Add
enctype="multipart/form-data"
To the tag... so it looks like
<form enctype="multipart/form-data" method......
If you really need to have the dollar-sign inside the name, escape it:
echo "<input type='hidden' name='\$software'>";
or put the string in single-quotes:
echo '<input type="hidden" name="$software">';
Otherwise PHP is looking for a variable named "$software", if you look inside the browser-source you will see that the name-attributes are empty(except you're having those variables defined somewhere).

Categories