uncheck a checkbox when checking another - php

<?php
session_start();
if (count($_POST) > 0) {
$_SESSION['link'] = $_POST['link'];
}
?>
<form method="post">
Gmail: <input type="checkbox" name="link" value="gmail" id="gmail" <?php if ($_SESSION['link'] == 'gmail') echo "checked"; ?>>
Hotmail: <input type="checkbox" name="link" value="hotmail" id="hotmail" <?php if ($_SESSION['link'] == 'hotmail') echo "checked"; ?>>
<input type="submit" value="Spara">
</form>
Problem is if a box is checked you have to uncheck that then check another to change.
Is there a way so it unchecks the checked one when i check another? that sounds weird...
Thanks

You could just use Radio buttons instead, e.g:
<input type="radio" name="rdGroup1" value="John"> John
<input type="radio" name="rdGroup1" value="Jane"> Jane

There is a reason God created radio buttons. :)

Related

Getting the value of a radio button

When I add a new record to my database the value of the radio button is inserted successfully. However, when I go to edit the record on my edit.php page, the radio buttons are all unchecked.
How I ensure that the corresponding radio button is checked depending on what I selected.
I've tried many different things and none work. This is the latest attempt:
<?php $prohibition = $row['prohibition']; ?>
Yes: <input type="radio" name="prohibition" value="Yes" <?php if($row['prohibition'] == "Yes") print "checked";?> /><br>
No: <input type="radio" name="prohibition" value="No" <?php if($row['prohibition'] == "No") print "checked";?> />
Based on the small snippet of code...
<?php if ($prohibition == 'Yes') { echo 'checked'; } ?> should work.
You've already defined the $prohibition variable so just check against that There's no reason to use the database output directly for the if statements.
Of course it depends entirely on the output of $row['prohibition']
What is the output of $row['prohibition']? try to replace print by echo :
<?php $prohibition = $row['prohibition']; ?>
Yes: <input type="radio" name="prohibition" value="Yes" <?php if($row['prohibition'] == "Yes") echo "checked";?> /><br>
No: <input type="radio" name="prohibition" value="No" <?php if($row['prohibition'] == "No") echo "checked";?> />

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>

Checkbox checked with PHP form post?

How do I check a checkbox?
I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
A checkbox will only be a successful control if it is checked.
Controls that are not successful are not submitted as data.
Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.
E.g.
if ($_POST['chk']['newmsg2'] == 1) {
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />
Here is the code;
<form action="test.php" method="POST">
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
<input type="submit">
</form>
<?php
$check = $_POST['chk']['newmsg2'];
echo "***$check****"
?>
If it is checked $check will show 1.

Why is PHP not allowing comparison?

I'm using PHP to read if an entry in my table on the database is set to "yes" or "no" and auto check the radio button that corresponds:
<?php include 'file.php';
$query = "SELECT * FROM TABLE";
$runquery = odbc_exec($connect,$query);
$status= odbc_result($runquery,"status");
odbc_close($file);
?>
<form>
<div class="formContainer">
<fieldset>
<legend>Campus Alert<span class="tooltip">Turn campus alert on and off.</span></legend>
<?php echo $status; ?>
Yes <input type="radio" name="alertStatus" id="alertStatus" value="yes" <?php if($status== "yes") echo "checked";?>>
No <input type="radio" name="alertStatus" id="alertStatus" value="no" <?php if($status== "no") echo "checked";?>>
</fieldset>
</div>
the <?php echo $status; ?> is for debugging so I can make sure what the database says and the form's reaction is correct. It prints "yes" (no quotes). However, the if statement will not respond. Any idea why it's doing this?
Have you tried changing your if statements to something like
<?php if(strtolower(trim($status)) == "yes") echo "checked";?>
It's not very good practise to use "yes/no" for your $status, you're better off using an int or boolean value.
Wow, that's really weird... the problem isn't with your PHP. First of all, You should remove the id attributes from those fields. But the real issue is that Firefox doesn't seem to want to check the second field when it has the name alertStatus. if you change the name to something else, it seems to be working. I'm not really sure why this is though.
Here's my test code:
<?php //include 'file.php';
//$query = "SELECT * FROM TABLE";
//$runquery = odbc_exec($connect,$query);
//$status= odbc_result($runquery,"status");
//odbc_close($file);
$status='no';
?>
<form>
<div class="formContainer">
<fieldset>
<legend>Campus Alert<span class="tooltip">Turn campus alert on and off.</span></legend>
<?php echo $status; ?>
Yes <input type="radio" name="alertStatu" value="yes" <?php if($status== "yes") echo "checked";?>>
No <input type="radio" name="alertStatu" value="no" <?php if($status== "no") echo "checked";?>>
</fieldset>
</div>
If you look at the page source you'll see that the check is actually there. However, you have a duplicate ID and the browser is getting confused. Replace:
Yes <input type="radio" name="alertStatus" id="alertStatus" ....>
No <input type="radio" name="alertStatus" id="alertStatus" .....>
with
Yes <input type="radio" name="alertStatus" id="alertStatus:yes" ....>
No <input type="radio" name="alertStatus" id="alertStatus:no" .....>
and it'll fix.

Categories