foreach repetation looping html table multiple times while checkboxes checked - php

Hello I have checkboxes and submit button inside table. Everything is running fine. I can get the checkbox event checked/unchecked after submit button. The only problem is my foreach loop is executing multiple times and i am getting multiple table executing checkbox with each checked option. Just a small mistake i am doing.
Here is my code:
$html = '<table width="538" cellspacing="0" cellpadding="0" border="0">
<tr>
<td>
<div>'.CreateRegister($page['register-fahrzeuge']).'</div>
'.CreateMessage().'
<div class="cont-liste-verlauf register">';
if($row = mysql_fetch_assoc($result))
{
$val= unserialize($row['configuration']);
$html .= '<table id="FAHRZEUGE" >
<tr>
<td>';
foreach($val as $config){
$html .= ' <input type="checkbox" name="Anzahl_Fahrzeuge_ohne_Bilder" id="Anzahl_Fahrzeuge_ohne_Bilder" '.($config=='Anzahl_Fahrzeuge_ohne_Bilder' ? 'checked="checked"' : '').' />
<label for="Anzahl_Fahrzeuge_ohne_Bilder">Anzahl_Fahrzeuge_ohne_Bilder</label><br>
<input type="checkbox" name="Fahrzeuge_ohne_Preis" id="Fahrzeuge_ohne_Preis" value="Fahrzeuge_ohne_Preis" '.($config=='Fahrzeuge_ohne_Preis' ? 'checked="checked"' : '') .'/>
<label for="Fahrzeuge_ohne_Preis">Fahrzeuge_ohne_Preis</label><br>
<input type="checkbox" name="Fahrzeuge_mit_Fehlern" id="Fahrzeuge_mit_Fehlern" value="Fahrzeuge_mit_Fehlern" '.($config=='Fahrzeuge_mit_Fehlern' ? 'checked="checked"' : '') .' />
<label for="Fahrzeuge_mit_Fehlern">Fahrzeuge_mit_Fehlern</label><br>
<input type="checkbox" name="Herausforderungen" id="Herausforderungen" value="Herausforderungen" '.($config=='Herausforderungen' ? 'checked="checked"' : '') .' />
<label for="Herausforderungen">Herausforderungen</label><br><br>';
}
$html .= '</td>
</tr>
</table>';
}
$html .= ' '.CreateButton($page['button']).'
</div>
</td>
</tr>
</table>';

I think you have $val array with elements for checked values.
Something like $val = array('Fahrzeuge_ohne_Preis','Herausforderungen');
And you want to print the form once, with these two checked.
If my guess is right, then you have to redo your algorithm completely.
$html .= ' <input type="checkbox" name="Anzahl_Fahrzeuge_ohne_Bilder" id="Anzahl_Fahrzeuge_ohne_Bilder" '.(in_array('Anzahl_Fahrzeuge_ohne_Bilder',$val) ? 'checked="checked"' : '').' />
<label for="Anzahl_Fahrzeuge_ohne_Bilder">Anzahl_Fahrzeuge_ohne_Bilder</label><br>
<input type="checkbox" name="Fahrzeuge_ohne_Preis" id="Fahrzeuge_ohne_Preis" value="Fahrzeuge_ohne_Preis" '.(in_array('Fahrzeuge_ohne_Preis',$val) ? 'checked="checked"' : '') .'/>
<label for="Fahrzeuge_ohne_Preis">Fahrzeuge_ohne_Preis</label><br>
<input type="checkbox" name="Fahrzeuge_mit_Fehlern" id="Fahrzeuge_mit_Fehlern" value="Fahrzeuge_mit_Fehlern" '.(in_array('Fahrzeuge_mit_Fehlern',$val) ? 'checked="checked"' : '') .' />
<label for="Fahrzeuge_mit_Fehlern">Fahrzeuge_mit_Fehlern</label><br>
<input type="checkbox" name="Herausforderungen" id="Herausforderungen" value="Herausforderungen" '.(in_array('Herausforderungen',$val) ? 'checked="checked"' : '') .' />
<label for="Herausforderungen">Herausforderungen</label><br><br>';
Remove the loop.

First you will have some problem if after you have to change the view, because you will have to do copy and paste and you will probably do some mistake.
Try to change to the code and only use one html for input checkbox and add the value checked if the value is in the array
EX
<?php
some code php....
?>
<input type="checkbox" name="Anzahl_Fahrzeuge_ohne_Bilder" id="Anzahl_Fahrzeuge_ohne_Bilder"
<?php if($config == "Anzahl_Fahrzeuge_ohne_Bilder") echo "checked"; ?> />
<label for="Anzahl_Fahrzeuge_ohne_Bilder">Anzahl_Fahrzeuge_ohne_Bilder</label><br>
<?php
other code php...
Your other problem is probably because in your sql you have more than one result, see you sql request and modify to only have one result (use "where" with some unique condition or "limit 0,1")
And other problem is that i dont undertand your code, because you have if and else every where and for each value you will have all the checkbox with one checked input and the others unchecked over and over again

Related

Checkbox is always checked even not checked

I have a checkbox that is always checked no matter if I checked the box or not.
Here is how the checkbox is currently setup:
<input class="classname" id="check" value="1" <?php echo (($temp['test']) ? 'checked' : ''); ?> name="check" type="checkbox" />
Here is how the check box value is being captured:
$check = $_POST['check'] ?? 0;
I also tried the following code as well:
$check = (isset($_POST['check']) == '1' ? '1' : '0');
I tried the hidden field approach as well and it didn't work. Any ideas on why the checkbox is showing marked even though I removed the check.
I think Drussy might be onto something, where does $_POST['test'] come from?
Try this code:
$checked = isset($_POST['check']) ? 'checked' : '';
<input class="classname" id="check" value="1" <?php echo $checked; ?> name="check" type="checkbox" />

Checkbox is checked when go to Edit page - PHP HTML

I have a situation where I have create a new information after submit a new info, the information is a success into the database. But when I click on the 'Edit' page, all other information is displayed. But for the input type checkbox, the information does not display at all.
<tr>
<td>Transaction</td>
<td>
<div class="checkbox check-default check-success">
<input id="f1" type="checkbox" value="1" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>
<label for="f1"></label>
</div>
</td>
</tr>
I wonder what I am missing. Please help, thanks.
Remove the value=1 in your code.
Use this code below
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>
You specified the value of a default input value = 1, removes value = 1
try
<input id="f1" type="checkbox" name="Tbox" <?= ( $modules['transaction']=='1'? "checked" : "") ?>>

remember user checked boxes populated from db

I have a form with several check boxes populated from a db
CODE:
// populating Checkboxes from db
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills[]" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST[$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
The problem is when the user select some of these check-boxes and submit the form and get error, the form can not remember his choices and he has to re-select them again.
what can I do to go over this issue?
Thanks in advance
This is not correct:
isset($_POST[$n['eName']]
You should look in the $_POST['skills'] array.
According to your implementation of $n['eName'] the following code might work:
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
ps: Be warned that any quotation in $n['eName'] would most likely break your code - one should take measures for these cases.
Edit:
<form method=post>
<?php
$n['id']='someid';
$n['eName'] = 'test';
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
?>
<input type=submit >
</form>

Default check a checkbox html

I am trying to get a checkbox checked by default, but everything I have tried doesn't seem to work. I don't know if it has to do with the PHP that is in the code.
function show_subscription_checkbox ($id='0') {
global $sg_subscribe;
sg_subscribe_start();
if ( $sg_subscribe->checkbox_shown ) return $id;
if ( !$email = $sg_subscribe->current_viewer_subscription_status() ) :
$checked_status = ( !empty($_COOKIE['subscribe_checkbox_'.COOKIEHASH]) && 'checked' == $_COOKIE['subscribe_checkbox_'.COOKIEHASH] ) ? true : false;
?>
<p <?php if ($sg_subscribe->clear_both) echo 'style="clear: both;" '; ?>class="subscribe-to-comments">
<input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;" <?php if ( $checked_status ) echo 'checked="checked" '; ?>/>
<label for="subscribe"><?php echo $sg_subscribe->not_subscribed_text; ?></label>
</p>
This is a wordpress plugin that allows you to subscribe to blog comments.
I have tried
echo 'checked=\"checked\" ';
echo 'checked="checked" ' ;
echo 'checked> ';
The plugin author states that you used to be able to default check the checkbox but not anymore.
Since this is showing up in google for "default checked checkbox", I figured I'd answer it. Alpay was right: The correct way to ensure that a checkbox is checked by default is like so (followed by an example of one that is not checked):
<input type="checkbox" name="vehicle" value="Car" checked> I have a car
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
Answer was found on w3schools. The author of the original question was having trouble with his PHP code, which is not at all related to the question title.
In HTML, if you want a checkbox to be checked by default, see the following;
<input type="checkbox" name="name1" value="uc"> This checkbox is unchecked <br>
<input type="checkbox" name="name2" value="c" checked> This checkbox is checked<br>
So, you might consider changing
<?php if ( $checked_status ) echo 'checked="checked" '; ?>
to
<?php if ( $checked_status ) echo 'checked'; ?>
The problem is not in the HTML markup being generated; echo 'checked="checked" ', as in the question, works well, and so would the simpler echo 'checked'.
The problem is with the condition $checked_status. You are testing for a variable that is undefined, as far as the code posted is considered.
I had the same problem. I figured out that I was trying to put the checkbox into a table but left out the and.
didn't check:
if(!$stump){echo '<input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b>';}
checked:
if(!$stump){echo '<td><input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b></td>';}
In regular PHP you can use this to "save" the checked state after its been submitted.
<form name="checkbox" method="post" action="#" >
<input type="checkbox" name="checkbox1" value="Bike" <?php if ($_POST['checkbox1']=="Bike") echo "checked";?>>I have a bike
<input type="checkbox" name="checkbox2" value="Car" <?php if ($_POST['checkbox2']=="Car") echo "checked";?>>I have a car
<input type="submit" name="submit" value="Display this Data" />
if you want to use this data for something else after the submit, just add:
<?php
if(isset($_POST['checkbox1']) OR isset($_POST['checkbox2']) )
{
echo "for 1 : ".$_POST['checkbox1']."for 2: ".$_POST['checkbox2'];
}
?>
if you want to clear the form (basically clear all the post data) you can add:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="submit" name="Clear all form data" value= "Clear this form">
</form>

keeping radio button value after post

HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>

Categories