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>
Related
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
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>
i have a form in three steps, from step 1 to step two there is a checkbox, i would like to keep the checkbox value in a session and also to show if it's checked or unchecked even if the user goes to step 3 of the form then comes back to step two,
right now on form submit i have all the $_POST vars in $_SESSION vars but i cant make it work with checkboxes, this is what i got right now:
<input type="checkbox" value="<?php echo isset($_POST['afficher_ddn'])? "1":"0"; ?>"
style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
but this doesnt work.
<input type="checkbox" value="1" style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo isset($_SESSION['afficher_ddn']) && $_SESSION['afficher_ddn'] == "1" ? 'checked="checked"' : ''; ?> />
For one, you don't change the value of the checkbox. If it is not checked it won't pass to the post vars. See here: Does <input type="checkbox" /> only post data if it's checked?
Now, the current standard for checked is checked="checked" I have no idea where or why this standard became one at the firm I work for, but it's what we do so I relay that here.
If the code provided here does not work for you, I would var_dump($_SESSION) to make sure it's set as well as check the actual HTML in something like firebug (firefox) or developer tools in chrome. Sometimes goofiness happens and checked="checked" is actually set in the html but doesn't display in browser. In those times I usually yank out some hair and clear caches. then it usually clears up.
Your code is fine.Possibly there is some problem in passing the the value in session as i am checking the code manually it is working perfectly:
use print_r($_SESSION) for testing and post the output;If possible provide all the forms:
Try Below Code:
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
try this example separetly
Make a page test.php as:
<?php
session_start();
?>
<form methos='post' method='post' action='test1.php'>
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> /><br>
Name:<input type='text' name='f1' value='<?php echo $_SESSION['f1'] ?>' ><br>
Select:<select name='s1'>
<option value="" <?php echo $_SESSION['s1']=="" ? "selected" : ""; ?>></option>
<option value="1" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>1</option>
<option value="2" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>2</option>
</select><br>
Test:
<textarea name='tex1'><?php echo $_SESSION['tex1']; ?></textarea>
<input type='submit' value='submit'>
</form>
Make a second page test1.php as:
<?php
session_start();
print_r($_POST);
echo $_SESSION['afficher_ddn']=$_POST['afficher_ddn'];
echo $_SESSION['f1']=$_POST['f1'];
echo $_SESSION['s1']=$_POST['s1'];
echo $_SESSION['tex1']=$_POST['tex1'];
?>
<form methos='post' action='third.php'>
<a href='test.php'>Step1</a>
</form>
and Check
My question was about the browser matter as far as I see.
I asked is there any solution to tell browser to move checked="checked" between radio buttons. I thought there will be a quick solution for this but this case is just confusing. I changed my code now. I use select list element now.
I have a form, and I get the actual settings with php, but the matter is this:
When the radio button 1 comes checked="checked" if that is actual, after it when I change to other option in post I get checked="checked"'s value, I was wondering is there a solution for this or Javascript is needed something like onclick"this.checked otherone uncheck" ?
if($type==1) { $like = 'checked="checked"'; } else { $ref='checked="checked"'; }
<form method="post" action="process.php" id="fbform" name="fbform">
<div class="radio">
<p>Like button: </p>
<input type="radio" name="tipi" <?=$like?> value="1" />
</div>
<div class="radio1">
<p>Link:</p>
<input type="radio" name="tipi" <?=$ref?> value="2" />
</div>
</form>
Process.php file code
<?php
$mysql->update('settings'," tipi='".$_POST['tipi]."' ");
?>
checked is a marker tag - it does not care , as long if the word 'checked' is there.
Here is what I would do:
<div class="radio">
<p>Like button: </p>
<input type="radio" name="tipi" id="tipi1" value="1"<?php if ($type == 1) echo ' checked="checked"'; ?> />
</div>
<div class="radio1">
<p>Link:</p>
<input type="radio" name="tipi" id="tipi2" value="2"<?php if ($type != 1) echo ' checked="checked"'; ?> />
</div>
I have changed the id's because you had two elements with the same id, which will never work, even if those elements are part of the same group.
You can do better with this. Also, IDs are used only for CSS or JS, and moreover must be UNIQUE.
<div class="radio">
<p>Like button: </p>
<input type="radio" name="tipi" id="tipi" <?php echo ($type == '1')?'checked="checked"' : '';?> value="1" />
</div>
<div class="radio1">
<p>Link:</p>
<input type="radio" name="tipi" id="tipi2" <?php echo ($type=='2')?'checked="checked"' : '';?> value="2" />
</div>
Then you access them in php
$tipi = $_POST['tipi']; //either 1 or 2
you need very simple changes in your code..See below code:
first of all you need to define variables at very top of the page/Code:
$like = "";
$ref = "";
Now your code will working good as per my knowledge..
Thanks.
Ghostology: Not sure about your query ... But all I can do is explain you the working of the radio button.
suppose there are two radio buttons inside a form:
<input type="radio" name="radioTest" value="1" checked />
<input type="radio" name="radioTest" value="0" />
Here checked specifies the button which will be checked(or turned on) during page load.
Now suppose we check the second button and submit this form, now we get the value 0 against the parameter 'radioTest' as radio button with value 0 is checked.
Now when page loads again with some value of radio buttons. You would want the proper radio button to be checked.
For this case I guess u can use if/else condition like:
<% if(radioTest.value == 1){ %>
<% }else{ %>
<% } %>
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>