I have a code:
while ($row = mysql_fetch_assoc($query_model)) {
echo ' <label class="checkbox">
<input name="model[]" value="'.$row["Model"].'" type="checkbox" checked>'.$row["Model"].'
</label>
';
}
I want to make checked option dynamic.
At first I will have all checkboxes checked, but when I will uncheck some checkboxes and click submit, I want to see, after page reloads, whole list with checked and unchecked fields (which I have unchecked formerly). Is it possible to put this option to while statement?
Normally I would do something like that:
if ($model == 'My model') {
echo ' checked';
} else {
echo '';
}
but in this case I don't know how to implement it.
Put the value of all the checked checkboxes in a array, let it be $available.
Now loop over the query result some what like this -
while ($row = mysql_fetch_assoc($query_model)) {
$checked = '';
if(in_array($row['Model'], $available){
$checked = 'checked';
}
echo '<label class="checkbox">
<input name="model[]" value="'.$row["Model"].'" type="checkbox" '.$checked.'>'.$row["Model"].'
</label>';
}
Yes you can do that like as follows :
while ($row = mysql_fetch_assoc($query_model)) {
if($model == 'My model') { // your condition
$checked=' checked';
} else {
$checked='';
}
echo '<label class="checkbox">';
echo '
<input name="model[]" value="'.$row["Model"].'" type="checkbox"'. $checked .'>'.$row["Model"];
echo '</label>';
}
You can try like,
if(isset($_POST['model']))
{
while ($row = mysql_fetch_assoc($query_model))
{
if(in_array($_POST,$row['Model'])
$checked="checked='checked'";
else
$checked="";
echo '<label class="checkbox"><input name="model[]" value="'.$row["Model"].'" type="checkbox" '.$checked.'>'.$row["Model"].'</label>';
}
}
else{
while ($row = mysql_fetch_assoc($query_model))
{
echo '<label class="checkbox"><input name="model[]" value="'.$row["Model"].'" type="checkbox" checked>'.$row["Model"].'</label>';
}
}
Related
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 am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.
My Form
<?php
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title;
}
} ?>
try this code
<?php
$k==0;
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title;
$k++;
}
} ?>
this code is inside while after I submit the form instead of retaining what I checked it checked all after submitting.
I just want to happen after submitting the only check box i check is checked.
What should i do ?
<input type="checkbox" title ="<?php echo $sym ?>"<?php if(isset($_POST['poscon'])) echo "checked='checked'"; ?> name="poscon[]" value ="<?php echo $pc?>"><?php echo $pc?>
refer to in_array
<?php
if(isset($_GET["poscon"])) {
$_SESSION["poscon"] = $_GET["poscon"];
$dr=$_SESSION['poscon'];
if(isset($_POST['submit'])) {
if(!empty($_GET['poscon']))
$_SESSION['poscon'] = $_POST['poscon'];
$part=$_GET["poscon"];
}
$poscon=mysqli_real_escape_string($link,$_GET['poscon']);
$p = mysqli_query($link,"select distinct PossibleCondition,Symptoms from healthguide where Subpart like '%$poscon%' and PossibleCondition REGEXP '^[N-Z].*$' Order by PossibleCondition ");
while($r=mysqli_fetch_array($p)) {
$pc=$r["PossibleCondition"];
$sym=$r["Symptoms"];
if(isset($_POST) && isset($_POST['poscon']) && in_array($pc,$_POST['poscon']))
$strIsChecked='checked="checked"';
else
$strIsChecked=null;
echo '<tr>';
echo '<td><input type="checkbox" '.$strIsChecked.' title ="'.$sym.'" name="poscon[]" value ="'.$pc.'"></td>';
echo '<td>'.$pc.'</td>';
echo '</tr>';
}
}
?>
$_POST['poscon'] is a array. Run my script and see how it works.
<?php
/**
* File test.php
*/
// Store checked Values in Array $arrChecked
$arrChecked=array();
if(isset($_POST) && $_POST['poscon']) {
// Debug: Show all POST Vars
echo "<pre>"; print_r($_POST); echo "</pre>";
foreach($_POST['poscon'] AS $checkboxValue) {
// fill array with checked values
// e.g. arrChecked['foo'] = true;
$arrChecked[$checkboxValue] = true;
}
}
/**
* Note for HTML-Block:
* shorthand php if/else used
* http://stackoverflow.com/questions/4567292/overview-of-php-shorthand
*/
?>
<form action="test.php" method="post">
<input type="checkbox" name="poscon[]" value="foo" <?php echo (isset($arrChecked) && $arrChecked['foo'] ? 'checked="checked"' : '');?>> foo
<input type="checkbox" name="poscon[]" value="bar" <?php echo (isset($arrChecked) && $arrChecked['bar'] ? 'checked="checked"' : '');?>> bar
<input type="submit" value="go">
</form>
This question already has answers here:
Get checked and unchecked checkboxes value
(3 answers)
Closed 8 years ago.
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[<?php echo $group['id']; ?>]" <?php echo $checked; ?>>
<?php
}
?>
Will output:
<form action="test.php" method="post">
<input type="checkbox" name="delete_user[1]">
<input type="checkbox" name="delete_user[2]">
<input type="checkbox" name="delete_user[3]">
<input type="checkbox" name="delete_user[4]" checked="checked">
<input type="submit" name="save_action" value="Save">
</form>
And when I check inputs as wanted, then this will process input.
<?php
if(isset($_POST['save_action']) {
if (empty($_POST['delete_user'])) {
$_POST['delete_user'] = array();
}
foreach($_POST['delete_user'] as $del) {
is_checked($del); //#todo
}
}
?>
I am looking for way to check if check-box is checked and return proper value ( Y or N ). In this point I declared is_checked() function for this purpose.
I would do it like this:
<form action="test.php" method="post">
<input type="checkbox" name="delete_user[]" value="1">
<input type="checkbox" name="delete_user[]" value="2">
<input type="checkbox" name="delete_user[]" value="3">
<input type="checkbox" name="delete_user[]" value="4" checked="checked">
<input type="submit" name="save_action" value="Save">
</form>
Now in PHP you can loop over the $_POST['delete_user'] and it will contain the values of the selected items.
foreach($_POST['delete_user'] as $item)
{
// code to delete $item
}
As the comments already mentioned, only the checked checkboxes will be set
So you either have to take the index as ID or add a val to each input element.
The latter would probably be cleaner (in my opinion)
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[]" <?php echo $checked; ?> value="<?php echo $group['id'];?>">
<?php
}
?>
And the code which does something with the inputs:
<?php
if(isset($_POST['save_action']) {
if (empty($_POST['delete_user'])) {
$_POST['delete_user'] = array();
}
foreach($_POST['delete_user'] as $del) {
// do something with $del - the id
}
}
?>
Use the checkbox name just as an array and then give the id as value. See the below code.
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[]" value="<?php echo $group['id']; ?>" <?php echo $checked; ?>>
<?php
}
?>
Then on the action page place this code
<?php
foreach($_POST['delete_user'] as $del) {
is_checked($del); //#todo
}
}
?>
This will work fine.
OK this is my script:-
<form action="results.php" method="post">
<?php mysql_select_db($database, $databasename) or die("Opps some things went wrong");
$sqlQueryTestDisplay = mysql_query("SELECT * FROM questions WHERE test_id='$testtaken_id' ORDER BY question_id ASC");
$i = 0;
while($DisplayItems = mysql_fetch_array($sqlQueryTestDisplay))
{
$i = $i + 1;
$question_id = $DisplayItems['question_id'];
$question = $DisplayItems['question'];
$opta = $DisplayItems['opta'];
$optb = $DisplayItems['optb'];
$optc = $DisplayItems['optc'];
$optd = $DisplayItems['optd'];
$answer[$i] = $DisplayItems['answer'];
$thisAnswer = $answer[$i];
echo '<li>'.$question.'</li>';
echo '<p>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_0" />'.$opta.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_1" />'.$optb.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_2" />'.$optc.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_3" />'.$optd.'</label>';
echo '<input name="ans'.$i.'" type="hidden" value="'.$thisAnswer.'" />';
echo '</p>';
}
echo '<input name="total_questions" type="hidden" value="'.$i.'" />';
echo '<input name="test_id" type="hidden" value="'.$TestID.'" />';
?>
<input name="submittest" type="submit" />
</form>
As you can see i am using array to store values in different fields. Now on the next page i.e on my result.php page I am writing this:-
<?php
if(isset($_POST['submittest']))
{
global $ans1;
$TotalQuestions = $_POST['total_questions'];
$TestID = $_POST['test_id'];
$TestResult = 0;
for ($i=1; $i<=$TotalQuestions; $i++)
{
$ansValue = 'ans'.$i;
$optansValue = 'optans'.$i;
$ans = $_POST[$ansValue];
$optans = $_POST[$optansValue];
if ($ans == $optans)
{
$TestResult = $TestResult + 1;
}
}
$st_id = $row_Recordset1['id'];
mysql_select_db($database, $databasename) or die("Opps some things went wrong");
$sqlQueryInsertResult = mysql_query("INSERT INTO results (student_id, test_id, test_result) VALUES ('$st_id', '$TestID', '$TestResult')");
header('location:results.php');
}
?>
Now my script is not reading ans1, ans2.....and so on AND even quesans1, quesans2.....and so on.
I think problem is in the way i am calling the array using the $_POST method.
Is the syntax correct, how can i FIX it... Please help :|
All of your radio buttons are returning "radio" as their value. Make them return the answer and you should be OK
Change your radio definitions to:
'<INPUT type="radio" name="optans'.$i.'" value="'.$opta.'" >'.$opta.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optb.'" >'.$optb.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optc.'" >'.$optc.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optd.'" >'.$optd.'</INPUT>'
Might be worth not putting the answer onto the page as a hidden field - makes a quiz quite easy. Do another SQL query to check the answers when they come back
Use some more loops to display the radio buttons and you can shorten your code a bit.
0Change your test options to this:
echo '<label><input type="radio" name="optans'.$i.'[]" value="1" id="RadioGroup'.$i.'_0" />'.$opta.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="2" id="RadioGroup'.$i.'_1" />'.$optb.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="3" id="RadioGroup'.$i.'_2" />'.$optc.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="4" id="RadioGroup'.$i.'_3" />'.$optd.'</label>';
and your processor to this:
<?php
if(isset($_POST['submittest']))
{
global $ans1;
$TotalQuestions = $_POST['total_questions'];
$TestID = $_POST['test_id'];
$TestResult = 0;
for ($i=1; $i<=$TotalQuestions; $i++)
{
$ans = $_POST['ans'.$i];
$optans = $_POST['optans'.$i];
for ($j=0;$j<count($optans);$j++) {
if ($optans[$j]==$ans) {
$TestResult = $TestResult + 1;
}
}
}
This is a very insecure way to compare test answers. Someone could easily View Source and see the correct answers. You should validate the test answers after the $_POST