PHP MySql: Checked Dynamic radio Box [closed] - php

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 = '';
}

Related

Setting a radio button into session to retain checked state

I am trying to store the input value of a radio button and storing that intoa session so that if the user roams around the site the radio remains checked unless they switch it themselves and then the new selection remains checked.
<form action="" method="POST" id="reportSwitch">
<input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>
<?php
$_SESSION['reportType'] = $_POST['reportType'];
$reportType = $_SESSION['reportType'];
if(isset($reportType)){
} else{
$reportType = 1;
}
?>
I cannot seem to to get it to remain in a checked state...
Put the value in session and use the session variable to populate value in radio button in place of using extra variable.
By populating from session It will help to retain in all pages.
<?php
$_SESSION['reportType'] = $_POST['reportType'];
?>
<form action="" method="POST" id="reportSwitch">
<input type="radio" name="reportType" id="leadership" value="1" <?php if($_SESSION['reportType'] == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($_SESSION['reportType'] == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>
check this code
<?php
session_start();
$_POST['reportType'] = 1; // for testing it is set define value , you can change
if(isset($_POST['reportType'])){
$_SESSION['reportType'] = $_POST['reportType'];
$reportType = $_SESSION['reportType'];
} else {
$reportType = $_SESSION['reportType'];
}
if(!isset($reportType)){
$reportType = 1;
}
?>
<form action="" method="POST" id="reportSwitch">
<input checked type="radio" name="reportType" id="leadership" value="1" <?php if($reportType == 1){
echo 'checked';} ?>>
<label for="leadership">Leadership</label>
<input type="radio" name="reportType" id="fundementals" value="2" <?php if($reportType == 2){
echo 'checked';} ?>>
<label for="fundementals">Fundementals</label>
</form>

Dynamic radio button with self-post using PHP & MySQL

I'm drawing data from a MySQL database that dynamically places a question with 4-5 radio button choices for the answer. These radio buttons all belong to the same group, $quest_name. The first pass of the while statement will create 4 radio buttons belonging to radio group "question_1".
It creates 30-40 of these questions on a page, each with 4 radio buttons. I want the user to fill in all there answers and the page to post back to itself with the users answers still selected and then display if they were correct or not (functionality I still have to add).
I'm trying to follow http://www.w3schools.com/php/php_form_complete.asp as an example, but use a dynamically created radio button name instead.
This is what I have thus far:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$quest_num = $row["id"];
$question = $row["question"];
$option_1 = $row["option_1"];
$option_2 = $row["option_2"];
$option_3 = $row["option_3"];
$option_4 = $row["option_4"];
$option_5 = $row["option_5"];
$answer = $row["answer"];
$quest_name = "question_" . $row["id"];
echo "(" . $quest_num . ") " . $question . "<br>";
?>
<label>
<input type="radio" name=<?php echo $quest_name ?>
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
value=<?php echo $option_1 ?>><?php echo $option_1 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_2 ?>><?php echo $option_2 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_3 ?>><?php echo $option_3 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_4 ?>><?php echo $option_4 ?>
</label>
<br>
<br>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
<input type="submit">
</form>
The part causing me grief so far is:
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
I have also tried:
<?php if (isset($quest_name) && $quest_name == $option_1) echo "checked"; ?>
and:
<?php echo (isset($quest_name) && $quest_name == $option_1) ? "checked" : ""; ?>
How do you post back to the same page what they've selected? Like in this case I'm trying to say if "question_1" is set and "question_1" is equal to "converter" (the first radio button option) then have it checked when submit button is clicked.
I'm not that good at web development, but I'm trying to create a website to help my fellow electrical technician classmates.
Thanks for any help.
EDIT :
Using this line of code fixed the issue:
<?php if(isset($_POST[$quest_name]) && $_POST[$quest_name]==$option_1) { echo 'checked="checked"'; } ?>
What you need is called Radio Group. In HTML layer it is created with same name for all and different values for each like this:
<p>
<label>
<input type="radio" name="RadioGroup1" value="Value1" id="RadioGroup1_0">
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="Value2" id="RadioGroup1_1">
Radio</label>
<br>
</p>
And when you want to get the user input in php layer you go like this:
<?php
//check if Radio Group 1 is set
if(isset($_POST['RadioGroup1'])) {
// print the value of Radio Group 1 choice
echo $_POST['RadioGroup1'];
}
?>
When you want to create a Selected Radio in HTML layer you go like this:
<input name="RadioGroup1" type="radio" id="RadioGroup1_1" value="radio" checked="checked">
So you have to check if user inputs the value of which radio like this:
<label>
<input type="radio" name="RadioGroup1" value="value1" id="RadioGroup1_0" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value1') { echo ' checked="checked"'; } ?>>
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="value2" id="RadioGroup1_1" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value2') { echo ' checked="checked"'; } ?> >
Radio</label>
You could use the following:
<input type="radio" name="<?php echo $quest_name ?>" value="<?php echo $option_1 ?>"
<?php if (isset($quest_name) && ($quest_name == $option_1)) echo "checked"; ?> />

Pre populated radio button in 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

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" />

Categories