I have user titles, which are stored in database in this way. 1,2,6,10 and etc.
I want to check if the user already has this titles and if he does, to check the check box.
<?php
$user_titles = explode(',', $user['titles']);
//foreach($user_titles as $uTitles){
//echo $uTitles;
//}
?>
<input type="checkbox" name="title[]" value="1">Test1<br/>
<input type="checkbox" name="title[]" value="2">Test2<br/>
<input type="checkbox" name="title[]" value="3">Test3<br/>
You need to add the checked attribute to the checked ones.
<input type="checkbox" name="title[]" value="1" <?php if(in_array(1, $user_titles) echo 'checked="checked"'; ?>>Test1<br/>
Move the titles to new array and iterate trough them. Inside the loop, check if the value is in the $user_titles array and add 'checked' to the input tag.
<?php
$titles = [
1 => "Test1",
2 => "Test2",
3 => "Test3",
];
foreach ($titles as $value => $title) {
$checked = in_array($value, $user_titles) ? 'checked' : '';
echo '<input type="checkbox" name="title[]" value="' . $value . '" ' . $checked . '>' . $title . '<br/>';
}
?>
If i understand correctly you have the values stored as a string delimited by a comma.
Use an iteration method, for example:
<?php $checked = ''; ?>
<?php for($i = 1; $i <= count($possible_checkbox); $i++ ) {
if(in_array($i, $user_titles ) ) { $checked = 'checked'; } else {$checked = '' }
?>
<input type="checkbox" value="<?php echo $i; ?>" name="title[]" <?php echo $checked; ?> />
<?php } ?>
Hope it helps.
Related
I want to get checked the checkbox value if this is checked
Here is my code:
<?php
foreach ($dosage_form_list as $val) {
?>
<input type="checkbox" name="dosage_form_input[]" value="<?php echo $val['dosage_form']?>">
<?php echo $val['dosage_form'];?>
<?php
}
?>
Just append checked=checked if you need it to be.. checked.
<input type="checkbox" name="dosage_form_input[]" value="<?php echo $val['dosage_form']?>" checked="<?= $someConditional ? 'checked' : ''; ?>">
I got my solution:
<?php
$dosage_form_list_exist = preg_replace('/\s*,\s*/', ',',
$dosage_form_list_exist); // It removes spaces after the comma only not
between the values like "Oral Solutions" etc
// $dosage_form_list_exist = str_replace(' ', '', $dosage_form_list_exist);
// It removes all the spaces from the Variable
$dosage_list = explode(',',$dosage_form_list_exist);
foreach ($dosage_form_list as $val) {
?>
<input type="checkbox" name="dosage_form_input[]" value="<?php echo
$val['dosage_form']?>" <?php echo (in_array($val['dosage_form'],$dosage_list)
? 'checked' : ''); ?>>
<?php echo $val['dosage_form'];?>
<?php
}
?>
I have multiple checkbox values stored in a single column in my DB ( Example cat,dogs or dogs,cows etc. it is retrieved as $animals and exploded to compare the values.
$animal_values = explode(",", $animals);
$cat = "cat";
$dogs = "dogs";
$cow = "cow";
foreach($animal_values as $value) {
if( $value == $cat ){
echo '<li>
<input type="checkbox" id="cat" checked="checked" name="animals[]" value="cat" />
<label for="cat">Cat</label>
</li>';
}
if( $value == $dogs ){
echo '<li>
<input type="checkbox" id="dogs" checked="checked" name="animals[]" value="dogs" />
<label for="dogs">dogs</label>
</li>';
}
if( $value == $cow ){
echo '<li>
<input type="checkbox" id="cow" checked="checked" name="animals[]" value="cow" />
<label for="cow">cow</label>
</li>';
}
This way the values that are present shows up as checked checked boxes.
But the thing is I also want to show boxes for whose values are missing as unchecked boxes. How can I achieve this ??
<?php
$animal_value = explode(",", $animal);
echo '<li>
<input type="checkbox" id="cat"';?> <?php if (in_array("cat", $animal_value)) { echo 'checked="checked"'; } ?> <?php echo 'name="animal[]" value="cat" />
<label for="cat">Cat</label>
</li>';
echo '<li>
<input type="checkbox" id="dog"';?><?php if (in_array("dog", $animal_value)) { echo 'checked="checked"'; } ?> <?php echo ' name="animal[]" value="dog" />
<label for="dog">Dog</label>
</li>';
echo '<li>
<input type="checkbox" id="cow"';?><?php if (in_array("cow", $animal_value)) { echo 'checked="checked"'; } ?> <?php echo 'name="animal[]" value="cow" />
<label for="cow">COW</label>
</li>';
?>
Any suggestions for Improvement or any other approach its welcome.
I'm building a very basic questionnaire (with response page) and it works but my form takes up too much space and looks clunky. How would i loop these question arrays so i can save space?
Here is a small bit of my code asking 2 questions:
<h3>Organisation?</h3>
<p><b>It was well organised</b><p>
<?php
$answers = array("Disagree","Agree");
for($i=0;$i<2;$i++) {
print '<input type="radio" name="org" value="'.$answers[$i].'">'.$answers[$i];
};
?>
</p>
<p><b>Adequate materials?</b><p>
<?php
$answers = array("Disagree","Agree");
for($i=0;$i<2;$i++) {
print '<input type="radio" name="lib" value="'.$answers[$i].'">'.$answers[$i];
};
?>
</p>
You should put the two rows in one php tag and use foreach instead, also you should let your php print the headers.
<?php
$answers= array("Disagree","Agree");
echo "<p><b>It was well organised</b></p><p>";
foreach ($answers as $value) {
print '<input type="radio" name="org" value="'.$value.'"> '.$value;
};
echo "</p>";
echo "<p><b>Adequate materials?</b></p><p>";
foreach ($answers as $value) {
print '<input type="radio" name="lib" value="'.$value.'"> '.$value;
};
echo "</p>";
?>
Sidenote: You have 4 opening p tags <p> and just 2 closing tags </p> in your own code. I added another two closing tags in my code where I expected them to be.
You could do:
<?php $questions = array(
'org' => 'It was well organized',
'lib' => 'Adequate materials?',
); ?>
<h3>Organisation?</h3>
<?php foreach ($questions as $f => $question): ?>
<p><b><?= $question; ?></b></p>
<?php foreach (array('Disagree', 'Agree') as $answer): ?>
<input type="radio" name="<?= $f ?>" value="<?= $answer ?>"> <?= $answer ?>
<?php endforeach; ?>
<?php endforeach; ?>
I think you are looking for something like this:
<h3>Organisation?</h3>
<?php
$answers = array("Disagree","Agree");
$first = ""; $second = "";
foreach($answers as $answer) {
$first .= '<input type="radio" name="org" value="' . $answer . '">' . $answer;
$second .= '<input type="radio" name="lib" value="' . $answer . '">' . $answer;
}
echo "<p><b>It was well organised</b></p><p>" . $first . "</p>";
echo "<p><b>Adequate materials?</b></p><p>" . $second . "</p>";
?>
Why not just make it a function?
function answers($name, $array)
{
$returnStr="";
for($i=0;$i<count($array);$i++)
{
$returnStr.='<p><input type="radio" name="'.$name.'" value="'.$array[$i].'"/> '.$array[$i].'</p>';
}
return $returnStr;
}
Then just call it
<h3>Organisation?</h3>
<p><b>It was well organised</b></p>
<?php echo answers("org",array("Agree","Disagree"));?>
if this is my checkbox
<input type="checkbox" name="Filter[]" value="Steak" id="Filter"/>
and if checkbox is checked var_export returns me
["Filter"]=> array(1) { [0]=> string(7) "Steak"
how do I echo "checked=checked" if checkbox is checked?
What you need is in_array(), this will check whether that value exists in the array, if your array contains the value, the function will return true and you can simply echo out the checked attribute
if (in_array('YOUR_VALUE_HERE', $arr)) {
echo 'checked="checked"';
}
You can also make a function passing value and array as parameter and returning the value from the function.
$checked = in_array('Steak',$_POST['Filter']) ? ' checked="checked"' : '';
echo '<input type="checkbox" name="Filter[]" value="Steak" id="Filter"'.$checked.'/>';
<?php // Check if the box was sent.
$checked = "";
$status = (isset($_REQUEST['status']));
if ($status == 'checked' )
{
$status = 1;
$checked = 'checked="checked"';
}
else
{
$status = 0;
}
echo $status;
echo <<<END
<form action="" method="post">
<input type="checkbox" name="status" $checked /> Testbox<br />
<input type="submit" onclick="return showDiv();"/>
</form>
END;
?>
I think this might solve your problem
$checked = in_array('Steak',$_POST['Filter']) ? ' checked="checked"' : '';
echo '';
I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
I've established an array for the checkboxes via the "name" attribute.
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to?
If not, is there any good way to do this?
So in your HTML table, you'll need to render each checkbox like this:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $key is the item number.
Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.
Assuming you've got a product list array like this:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.
You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.
If i'm understanding you correctly, you'll have checkboxes something like this:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3.....
i need all this values in further page, so have taken hidden variables
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".   "; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>