Pre populated radio button in php - php

I have radio buttons for Yes and No and want to pre-populate them from a value in a mysql database. I am able to show the radio buttons but they always show the Yes checked even when the returned value is No. I am also not able to change the pre-populated value eg Yes to No.
<input type="radio" id="etag" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etag" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No

Note : id = should b unique as mentioned by Phil in his comments
This is how you can achieve ur task!
<input type="radio" id="etag_1" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etag_2" name="etag" value="No" <?php echo ($d_etag == 'No') ? 'checked="checked"' : ''; ?> />No
To find out Which Radio button is clicked use
$("input:radio[name=etag]").click(function() {
var value = $(this).val();
});
or try this
$('input:radio[name=etag]:checked').val();

Can you var_dump($d_etag); and check the data type of that variable. it should be a string with Case-sensitive.
Can you try this:
<input type="radio" id="etagYes" name="etag" value="Yes" <?php echo ($d_etag == 'Yes') ? 'checked="checked"' : ''; ?> />Yes
<input type="radio" id="etagNo" name="etag" value="No" <?php echo ($d_etag == 'Yes') ? '' : 'checked="checked"'; ?> />No

Related

Update database values for checkboxes using PHP PDO not updating all checkboxes

I have 3 rows and each row has a checkbox, when I uncheck all boxes and click save I receive the error in my else statement. If I uncheck on box at a time and click save, when I get to the last checkbox and click save I also receive the error in the else statement.
Here is my question - How do I allow all checkboxes to be unchecked without receiving an error?
Here is my PHP code:
//Update Social Preferences with new value for display on homepage
$settingsArray = array('myfacebook', 'mytwitter', 'mygoogle');
if(isset($_POST['btn-save']))
{
if(isset( $_POST['mysocial']))
{
$values = array();
foreach($_POST['mysocial'] as $selection )
{ if(in_array($selection, $settingsArray))
{ $values[ $selection ] = 1; }
else
{ $values[ $selection ] = 0; }
} // end of foreach.
$user_id = $_SESSION['user']['id'];
try // save user selection to the database
{
$stmt = $db->prepare("UPDATE social_preferences SET my_facebook = :myfacebook, my_twitter = :mytwitter, my_google = :mygoogle WHERE user_id = :userID");
$stmt->bindParam(":userID", $userid, PDO::PARAM_INT);
$stmt->bindParam(':myfacebook', $values['myfacebook']);
$stmt->bindParam(':mytwitter', $values['mytwitter']);
$stmt->bindParam(':mygoogle', $values['mygoogle']);
$stmt->execute();
header("Location: admin-social-test.php");
die("Redirecting to admin-social-test.php");
} catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
}
else
{ $noCheckbox = 'No checkbox selection made...'; }
} // End of, if statement from the button check
Here is my HTML Code:
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
I added a hidden input for each one.
So I changes this:
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
To this:
<input type="hidden" name="mysocial[]" value="myfacebook1" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mytwitter1" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mygoogle1" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
This seems to have got it working.

PHP MySql: Checked Dynamic radio Box [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code for dynamic checked radio box:
PHP:
if ($author === 0) {$checked = 'checked';} else {$checked == '';}
if ($author === 1) {$checked = 'checked';} else {$checked == '';}
if ($author === 2) {$checked = 'checked';} else {$checked == '';}
HTML:
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
This way is true? What’s is a better/optimized Way?
Can I write any PHP function or class for save code and checked any radio box?
It can save you some lines and unnecessary extra variables if you look forward using the shorten if statement form. Example with the first input :
<input type="radio" name="test" <?php echo $author === 0 ? 'checked' : '' ?> />
Ideally you want to check a button if the author value relates to that input, for example:
<input type="radio" name="test" value="0" <?php echo ($author == 0 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="1" <?php echo ($author == 1 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="2" <?php echo ($author == 2 ? 'checked="checked"' : ''); ?> />
Your currently checking all if the value is any.
The HTML markup is wrong. With that HTML the user is able to select all of those radios. To make it so that the user can only select one of the radio buttons out of that group of options, change the names to all match, like this:
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
Something you can do:
<?php
$checked = ($author >= 0 && $author <= 2 ? 'checked' : '');
?>
HTML
<input type="radio" name="test" <?php echo $checked; ?> />
<input type="radio" name="test1" <?php echo $checked; ?> />
<input type="radio" name="test2" <?php echo $checked; ?> />
<?php
switch($author) {
case 0:
case 1:
case 2:
$checked = "checked";
break;
default:
$checked = '';
}

issue having radio button checked in session [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
i want radio buttons remain selected across all pagination. For taht
I have collected the checked value in pagination at the top of page.
But it is not working well.
What is wrong with this code ?
This is how i get selected value of radio button in session
session_start();
if(isset($_POST['answer'])) {
$_SESSION['answer'] = $_POST['answer'];
}
This is my php code
<input type="radio" name="answer" value="yes" <?php if(isset($_SESSION['answer'])=='yes') {echo "checked"; }?> />
<input type="radio" name="answer" value="no" <?php if(isset($_SESSION['answer'])=='no') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes1" <?php if(isset($_SESSION['answer'])=='yes1') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes2" <?php if(isset($_SESSION['answer'])=='yes2') {echo "checked"; }?> />
It gives me error(notice) like answer is undefined index.
isset() returns true or false. Your if statement should be, for example:
if (isset($_SESSION['answer']) && $_SESSION['answer'] == 'yes')
Try with:
$values = array('yes', 'no', 'yes1', 'yes2');
foreach ( $values as $value ) {
$checked = isset($_SESSION['answer']) && $_SESSION['answer'] == $value ? 'checked="checked"' : '';
echo '<input type="radio" name="answer" value="' . $value . '" ' . $checked . '/>';
}
<input type="radio" name="answer" value="yes" <?php if($_SESSION['answer']=='yes') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="no" <?php if($_SESSION['answer']=='no') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes1" <?php if($_SESSION['answer']=='yes1') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes2" <?php if($_SESSION['answer']=='yes2') { ?> checked="checked" <?php }?> />
please use this html

Checkbox is always "off" or unchecked

When I checked my checkbox and click submit, it doesn't work and is still unchecked, mo matter if is checked or not!
<label for="checkbox-4" tabindex="4">food</label>
<input type="checkbox" <?php $ch1=strpos($f_type,"1"); if($ch1 >= 0 && $ch1 != ""){echo('checked="checked"');} ?> name="foodtype1" id="checkbox-4" value="1" />
Can anyone help me?
<input type="checkbox" <?php $ch1=strpos($f_type,"1"); if($ch1 >= 0 && $ch1 != ""){echo('checked="checked"');} ?> name="foodtype1" id="checkbox-4" value="1" />
You should put a space between <?php ... ?> and name="foodtype1".
EDIT:
Easier test below
<input type="checkbox" <?php echo (strpos($f_type,"1") !== FALSE) ? 'checked="checked" : ''; ?> name= "foodtype1" id="checkbox-4" value="1" />
<?php
$_POST['foodtype1'] = 1;
//$_POST['foodtype1'] = 'a';
?>
<label for="checkbox-4" tabindex="4">food</label><input type="checkbox"
<?php if(isset($_POST['foodtype1']) AND $_POST['foodtype1'] == 1) { echo('checked="checked"');} ?>
name="foodtype1" id="checkbox-4" value="1" />

How do i go about echoing the selected check box? tried the below. any ideas

<input type="checkbox" value="Administrative" name="industry_sector1" <?php echo ($industry_sector1a == 'Administrative' ? "checked='checked'" : ''); ?>> Administrative
</td>
<td>
<input type="checkbox" value="Customer Service" name="industry_sector2" <?php echo ($industry_sector2a == 'Customer Service' ? 'checked="checked"' : ''); ?>> Customer Service
How do i go about echoing the selected check box? tried the above. any ideas
<input type="checkbox" value="Administrative" name="industry_sector1" <?php if(isset($_POST['industry_sector1'])) echo 'checked="checked"'; ?>> Administrative
<input type="checkbox" value="Customer Service" name="industry_sector2" <?php if(isset($_POST['industry_sector2'])) echo 'checked="checked"'; ?>> Customer Service
if (isset($_POST['industry_sector1']))
echo "First checkbox checked";
if (isset($_POST['industry_sector2']))
echo "Second checkbox checked";
Or what are you trying to do exactly?

Categories