mpdf showing a radio button and checkbox selected - php

I have a quick question.
I creating an pdf output when the user clicks on the button. However the problem I'm having is the radio and checkbox. I tried to use the standard
($answer == 'What the value is' ) ? 'checked':''
however when it is outputted to a pdf, it doesn't show the radio button or checkbox selected. Is there way I can have the radio buttons or checkbox's selected in an pdf output?
Thank you

Ok, I have found a solution. What I did was I put the the checkbox and radio button in if statement and I did put the code in the following format:
<input type="radio" checked="checked"> Yes
That did the trick..

try this:
if ($answer = "1"){
$output .= 'Yes <input type="checkbox" checked /> ';
}
else{
$output .= 'Yes <input type="checkbox" /> ';
}
Where is 1 true. I assume that, you get these values from db. In this cycle check these values if it checked.

<input type="radio" value="1" <?php $data->input_11 == 1 ? 'checked="checked"' : null ?> > Yes
<input type="checkbox" value="1" <?php $data->input_11 == 1 ? 'checked="checked"' : null ?> > Yes
Its Work for me

Related

how to enable a check box using stored values in a file like arrays

hiii,
i have a check box ,while submitting the form i will store the check box value like 1 or 0 ,if i reload the page then checked value not showing so i want retrieve from file .if i refresh or navigate to back it should maintan the state.
i have tried this,but not working
<input type="checkbox" id="enable" name="check[0]" value="1" checked='<?php echo ($arr[3]==1 ? 'true' : 'false');?>' />
please help me with this
thanks..
Echo checked if enabled:
<input type="checkbox" id="enable" name="check[0]" value="1" <?php echo $arr[3]==1 ? 'checked' : ''; ?> />

How to write a conditional statement with a value but only if it's the only value?

if($uriarray['value'] == 0 ){
I have an input form which includes values. The values are in a checkbox. I want to show something if it's the only value, but not if it's included with other values. So if the value is 0 I want to show something but if it includes other values like 1,2,3 etc, I don't want to show it. Is this something that's possible?
If & only if?
It depends on your HTML form. For example, if you specify the names of the checkboxes in array-form like:
<input type="checkbox" name="uriarray[]" value="0" />
<input type="checkbox" name="uriarray[]" value="1" />
<input type="checkbox" name="uriarray[]" value="2" />
<input type="checkbox" name="uriarray[]" value="3" />
you would receive an array in PHP with all checked values.
<?
if (count($_POST['uriarray']) == 1) {
$onlyValues = $_POST['uriarray'][0];
}
?>
If you also want to check the single value to be 0:
<?
if ((count($_POST['uriarray']) == 1) && ($_POST['uriarray'][0] == "0")) {
//Do something
}
?>

How to validate a checkbox input using PHP?

I have two check boxes in my form:
<div class="label-input-wrapper pickup">
<div class="form-label">I need Airport pick-up</div>
<div class="form-input">
<input type="checkbox" name="pick_up" value="Yes" />Yes
<input type="checkbox" name="pick_up" value="No" />No
<div class="error-msg">
<?php if(isset($errors['pick_up_no'])) { echo '<span style="color: red">'.$errors['pick_up_no'].'</span>'; } ?>
</div>
</div>
</div>
Variable that saves the value of the above check boxes: $pickup = $_POST['pick_up'];
Validation:
//Check the airport pickup and make sure that it isn't a blank/empty string.
if ($pickup == ""){
//Blank string, add error to $errors array.
$errors['pick_up_no'] = "Please let us know your airport pick up requirement!";
}
if (($pick_up = Yes) && ($pick_up = No)) {
//if both selected, add error to $errors array.
$errors['pick_up_no'] = "Can not select Yes and No together!";
}
But the above validation just printing Can not select Yes and No together! if both are NOT selected, or if only one selected or even both are selected. It gives same error message for all the selections. Why?
You're presently assigning with a single = sign instead of comparing == with
if (($pick_up = Yes) && ($pick_up = No))
you need to change it to
if (($pick_up == "Yes") && ($pick_up == "No")){...}
while wrapping Yes and No inside quotes in order to be treated as a string.
Edit:
Your best bet is to use radio buttons, IMHO.
Using checkboxes while giving the user both Yes and No options shouldn't be used; it's confusing.
It should either be Yes OR No and not and.
<input type="radio" name="pick_up" value="Yes" />Yes
<input type="radio" name="pick_up" value="No" />No
That is the most feasible solution.
I don't know why you need a checkbox for this (this is what radio button's are supposed to do), but you can do it something like:
if(!isset($_POST['pick_up']) || count($_POST['pick_up']) > 1) {
echo 'please select at least 1 choice';
} else {
echo $_POST['pick_up'][0];
}
Then make your checkbox names:
name="pick_up[]"
Or if you really need to have separate error messages. Do something like this:
$errors['pick_up_no'] = '';
if(!isset($_POST['pick_up'])) {
$errors['pick_up_no'] = "Please let us know your airport pick up requirement!";
} elseif(count($_POST['pick_up']) > 1) {
$errors['pick_up_no'] = "Can not select Yes and No together!"; // weird UX, should have prevented this with a radio button
}
if($errors['pick_up_no'] != '') {
echo $errors['pick_up_no'];
} else {
echo $_POST['pick_up'][0];
}
It is impossible for $_POST['pick_up'] to be both yes and no at the same time since the name is pick_up and not pick_up[] <- pointing to multiple variables. So it will either be yes or no either way because one parameter will overwrite the other. You would have to use javascript to verify only one is checked before submit in that case.
Use array of checkbox field elements;
<input type="checkbox" name="pick_up[]" />Yes
<input type="checkbox" name="pick_up[]" />No
then in your PHP script:
<?php
$pick_ups=$_POST['pick_up'];
$first=isset($pick_ups[0]); //true/false of first checkbox status
$second=isset($pick_ups[1]); //true/false of second checkbox status
?>
Again, as you've already been told, you should use radio-buttons for this purpose! Take into account that $_POST does not send checkbox if it is not set (check with isset($_POST['name'])

Populate checkbox form from database in codeigniter

I`m using codeigniter to build small website. I am curious how to populate checkbox from database ?
Let`s say I have the following query;
$this->db->select('status');
$this->db->where('id', 3');
$this->db->get('table);
How to make checkbox to be checked if the result of the query above is 1 ?
You can do something like this. It's just an idea since I can't really see EXACTLY what you're returning. $entry['status'] is the result from your query.
if($entry['status'] == 1){
echo '<input type="checkbox" checked="checked" disabled="disabled"/>';
}
else {
echo '<input type="checkbox" unchecked="unchecked" disabled="disabled"/>';
}
Take a look at the form_checkbox() section of the codeigniter form helper documentation
If you want to have the select box checked if the status value is greater than 1 you could do something like this.
$result = $this->db->select('status')->get_where('table', array('id'=> 3))->row();
if($result->status > 1)
{
print '<input type="checkbox" name="checkbox" checked="checked" value="'.$result->status.'" />';
}else{
print '<input type="checkbox" name="checkbox" value="'.$result->status.'" />';
}
You'll want to check that the row actually returns values or you'll get a var on a non object error. This is just an example.
This works for me and is simple:
<?php foreach($tb as $table_row){ ?>
<input type="checkbox" <?php if ($table_row['reconciled'] == "1" {echo "checked = checked";} ?>

A Simple HTML Checkbox with PHP Problem

I have a field in my update form called approve which is using the html checkbox element. now i am querying the approve value from the database which will hold the binary value (0 or 1), i want the checkbox to perofrm the following actions in condition.
a) While Querying from database.
1)if the value of active is 1 then it should be checked by default and also it should hold the value 1 to process it to update
2)the same applies for 0, if the value is zero then it is unchecked and will hold the value 0 to process
P.S: I want to use this for updating the form not inserting.
Do it like this:
PHP embedded in HTML way:
<input name="chk" type="checkbox" value="<?=$value?>" <?php if($value==1) echo 'checked="checked"';?> />
Pure PHP way:
<?php
echo '<input name="chk" type="checkbox" value="'.$value.'"';
if($value == 1)
echo ' checked="checked" ';
echo '/>';
?>
Just a 1-line shorter version ;)
<?php echo "<input name=\"chk\" type=\"checkbox\" value=\"$value\"".( ($value == 1) ? " checked=\"checked\"" : "" )." />"; ?>
Like this:
<input type="checkbox" value="<?php echo $row['approved']; ?>" <?php if($row['approved'] == 1): echo 'checked="checked"'; endif; />

Categories