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
Related
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"; ?> />
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 = '';
}
I'm trying to check a checkbox based on its database value, i have tried the following but no joy, any suggestions?
I am using Kohana framework.
public static function defaultdistance(){
$result = DB::select('value')->from('mytable')->where('key', '=', 'default-distance')->execute();
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
echo 'Value: '.$distancestore['value'];
}
}
<label class="shortLabel"><input type="radio" name="distance" value="10" <?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> /> 10 <?php echo $dict->transl('distance_km'); ?></label>
<label> </label><label class="shortLabel"><input type="radio" name="distance" value="15" <?php if ($distancestore['value'] == '15') echo "checked='checked'"; ?> /> 15 <?php echo $dict->transl('distance_km'); ?></label>
May be you need like this
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
?>
<label class="shortLabel">
<input type="radio" name="distance" value="10"
<?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> />
10 <?php echo $dict->transl('distance_km'); ?>
</label>
<?php
}
try this i think you have to use ternary operator to resolve this issue.
<input type="radio" name="distance" value="10"
<?php $select = $distancestore['value']=='10'?'checked':'';
echo $select; ?>
10 <?php echo $dict->transl('distance_km'); ?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im wanting to repeat radio buttons using php. Below is the html form
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
Basically, i have 4 groups of questions with rating 1-6 (how would i do it so i dont need to write out all this html code over and over, is there a shorthand method using php so that the selected one also goes into a mysql database too?
Completely confused and new to php, any help would be great.
Look at loop for
//$i => groups (/4)
//$a => radio buttons (/6)
for($i = 1; $i <= 4; $i++){
for($a = 1; $a <= 6; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
And the function
function loopMe($group, $answer){
for($i = 1; $i <= $group; $i++){
for($a = 1; $a <= $answer; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
}
For use it
loopMe(3, 4);
As others have suggested, the FOR loop is your best bet here however, you can expand a little by adding simplicity and expanding the commands.
$rating = 6; //This sets the highest rating number
for($i=0 $i<$rating; $i++){
//This echos the input by rating, if rating is 6, it'll repeat 6 times.
echo "<input type=radio name=RadioGroup1 value='" . $i . "'>\r\n";
}
Now for the other half of the problem.
When posting information to a database, you need to send it to another page for processing and storage (or the same page if you handle it correctly).
This is a basic way to store THIS radio group (this is in procedural style):
$link = mysqli_connect('localhost', 'username', 'password', 'dbname');
$query = "INSERT INTO myTable (RadioGroup1) VALUES ($_POST['RadioGroup1'])";
mysqli_query($link, $query); //replace link with your database connections variable
Try a FOR loop function...as below:
<?php
for ($x=0; $x<=6; $x++)
{
echo '<input type="radio" name="RadioGroup1" value="'.$x.'">'.$x;
}
?>
To learn more about this function, visit:
http://php.net/manual/en/control-structures.for.php
see this example below... hope this helps you...
page1.php
<html>
<body>
<form id="frmQuestion" method="post" action="page2.php">
<p>Question 1</p>
<input type="radio" name="question1" value="1" checked="checked" />1
<input type="radio" name="question1" value="2" />2
<input type="radio" name="question1" value="3" />3
<input type="radio" name="question1" value="4" />4
<input type="radio" name="question1" value="5" />5
<input type="radio" name="question1" value="6" />6
<br/>
<p>Question 2</p>
<input type="radio" name="question2" value="1" checked="checked" />1
<input type="radio" name="question2" value="2" />2
<input type="radio" name="question2" value="3" />3
<input type="radio" name="question2" value="4" />4
<input type="radio" name="question2" value="5" />5
<input type="radio" name="question2" value="6" />6
<br/>
<?php
for($i=3; $i<=20; $i++)
{
echo "<p>Question " . $i . "</p>";
for ($j=1; $j<=6; $j++)
{
if($j == 1)
{
echo "<input type='radio' name='question". $i ."' value='". $j."' checked='checked' />" . $j;
}
else
{
echo "<input type='radio' name='question". $i ."' value='". $j."' />". $j;
}
}
echo "<br/>";
}
?>
<input type="submit" value="Send" />
</form>
</body>
</htm>
page2.php
<?php
echo "question 1:" . $_POST["question1"] . "<br/>";
echo "question 2:" . $_POST["question2"] . "<br/>";
echo "question 3:" . $_POST["question3"] . "<br/>";
echo "question 4:" . $_POST["question4"] . "<br/>";
// for more.
for ($n = 5; $n<=20; $n++)
{
$question = "question" . $n;
echo "question". $n . ":" . $_POST[$question] . "<br/>";
}
?>
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" />