Keeping checkboxes checked in PHP form - php

I know how to keep the checkboxes checked on form submit when there is an error, but I have a different issue now. I am using check boxes of the same name where people can click 1 or multiple. I want it to only keep the check boxes that the user checked filled if there is a problem like if they check q1 A q1B but not q1c I want only q1 A and Q1b to show checked when reloaded on an error. Right now on an error all checkboxes of the same name are checked. I have tried changing the name to q1[] but that did not work. Can you please take a look and let me know how to fix this?
Here is my code.
<tr>
<td style="width: 124px" class="style15">Tape Recorder<?php if(isset($problems['tape[]'])) {?><font color="red">*</font><?php } ?></td>
<td class="style9">
<input name="tape[]" id="tape1" type="checkbox" value="used before," <?php if(isset($_POST['tape[]'])) echo "checked"; ?>
</td>
<td class="style9">
<input name="tape[]" id="tape2" type="checkbox" value="helpful in past," <?php if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9">
<input name="tape[]" id="tape3" type="checkbox" value="requesting from DACC" <?php if(isset($_POST['tape[]'])) echo "checked"; ?> </td>
<td class="style9">
<input name="tape[]" id="tape4" type="checkbox" value="NA" <?php if(isset($_POST['tape[]'])) echo "checked"; ?></td>
</tr>
<tr>
<td style="width: 124px">Note Taker <?php if(isset($problems['note'])) {?> <font color="red">*</font><?php } ?></td>
<td class="style9">
<input name="note" type="checkbox" value="used before," <?php if(isset($_POST['note'])) echo "checked"; ?>
</td>
<td class="style9">
<input name="note" type="checkbox" value= "been helpful in the past," <?php if(isset($_POST['note'])) echo "checked"; ?>
<td class="style9">
<input name="note" type="checkbox" value= "requesting from DACC" <?php if(isset($_POST['note'])) echo "checked"; ?>
<td class="style9">
<input name="note" type="checkbox" value="NA" <?php if(isset($_POST['note'])) echo "checked"; ?>
</tr>

A quick and dirty solution would be:
<input name="tape[]" id="tape[]" type="checkbox" value="NA" <?php if(isset($_POST['tape']) && is_array($_POST['tape']) && in_array('NA', $_POST['tape'])) echo 'checked="checked"'; ?> />
For that you need to change the 'NA' part for each different answer obviously. Though I would look at something like having a loop for repeated checkboxes or a callback function to determine whether or not to echo checked=checked.

Thank you Mr. Code
Here's my final code that I was able to figure out thanks to you
$mailBody .= "They requested additional information on ...\n\n";
$mailBody .= $moreinfo = join("\n", $_REQUEST["moreinfo"]);
$mailBody .= "\n\n";
$mailBody .= "They also had this to say...\n\n";
$mailBody .= "$comments\n\n";
//code on page
<input type="checkbox" name="moreinfo[selection1]" value="selection1" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection1', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 1<br>
<input type="checkbox" name="moreinfo[selection2]" value="selection2" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection2', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 2<br>
<input type="checkbox" name="moreinfo[selection3]" value="selection3" <?php if(isset($_POST['moreinfo']) && is_array($_POST['moreinfo']) && in_array('selection3', $_POST['moreinfo'])) echo 'checked="checked"'; ?> /> Selection 3<br>
I do not care that this looks crazy because it works.

You dont need to have the same name for checkboxes, only for radio buttons.
Radio buttons belong to a group so they have to have the same name but different values. Checkboxes however, can have different names for each checkbox! Therefore just change each checkbox to a differnent name

Related

Html table with data from mysql

I'm making a form like this one:
<?php
echo '<tr>
<td><strong>Kamp</strong></td>
<td width="80px"><strong>1X2</strong></td>
<td><strong>Resultat</strong></td></tr>';
$no = 1;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['kamp'].'</td>
<td><input type="radio" name="k1" id="k11" value="1" />1<input type="radio" name="k1" id="k1x" value="X" />X<input type="radio" name="k1" id="k12" value="2" />2</td>
<td><input name="k1r" type="text" id="k1r" placeholder="X-X" /></td>
</tr>
';
$no++;
}?>
And it echo a nice form with 3 rows from my database in the first <td>. It is like a betting-game, so I have the mathes in my database. But as it is right now, the user will check the radiobuttons at 3 different mathes, but submit the same, if you understand. How can I make it 3 different inputs?
I usually use counter in my loop
$no = 1;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['kamp'].'</td>
<td><input type="radio" name="k1_'.$no.'" id="k11_'.$no.'" value="1" />1<input type="radio" name="k1_'.$no.'" id="k1x_'.$no.'" value="X" />X<input type="radio" name="k1_'.$no.'" id="k12_'.$no.'" value="2" />2</td>
<td><input name="k1r_'.$no.'" type="text" id="k1r_'.$no.'" placeholder="X-X" /></td>
</tr>
';
$no++;
if you want multiple input fields with the same name you can create an array of input values with brakets
<input type="radio" name="k1[]" id="k11" value="1" />
See: http://www.php.net/manual/en/faq.html.php#faq.html.arrays for more infomation.

Pass two variables through checkbox in php

Through checkbox I want to pass & values. how can I do that. I can only pass one value. I am trying but its not working
while($row_tbl = mysqli_fetch_array($query))
{
<tr class="success" style="font-size:12px;">
<td > <input type="checkbox" name="check[]" class="chk_val" value=" <?php echo $row_tbl['Course_ID'] ?> " id="in" onclick="test()" /></td>
<td > <?php echo $row_tbl['Course_ID'] ?> </td>
<td> <?php echo $row_tbl['Course_Title'] ?> </td>
<td> <?php echo $row_tbl['Section'] ?> </td>
<td> <?php echo $row_tbl['Time'] ?> </td>
<td> <?php echo $row_tbl['Day'] ?> </td>
<td> <?php echo $row_tbl['Dept'] ?> </td>
<td> <?php echo $row_tbl['Capacity'] ?>&nbsp/&nbsp 0 </td>
<?php
}
To pass multiple values, you need to create multiple checkbox with the same name (as an array)
<input type="checkbox" name="check[]" class="chk_val" value="value1"/>
<input type="checkbox" name="check[]" class="chk_val" value="value2"/>
<input type="checkbox" name="check[]" class="chk_val" value="value3"/>
<input type="checkbox" name="check[]" class="chk_val" value="value4"/>
In your controller,
$values = $request->check; //the array of checked inputs.
Then you can loop through it
foreach($values as $value) {
...
}
Update
Accodring to the chat discussion you want to send 2 different fields per checkbox. In your checkboxes, put both fields like this
#foreach($courses as $course)
<input type="checkbox"
name="check[]"
class="chk_val"
value="{{ $course->id }}-{{ $course->section }}"/>
#endforeach
Pay attention to the value here. I added a delimiter - that will be useful in the controller. In controller now
public function selectedCourses(Request $request)
{
... //whatever you do for validation
//loop through selected courses
foreach( $request->check as $values ) {
$values = explode("-", $values); //split where we added the dash
$id = $values[0]; //the course ID
$section = $values[1]; // the course section
... do what you want here with that information
}
}
And voila!

How to capture different values for the items belong to the same group

I have a table to display Addon items from database.
The user must check the checkbox for the first two items namely: gps and babyseat.
So the deposit and priceperday are captured inside add_item[] of the checkbox.
But the third item which is Driver, there's no deposit or priceperday but price per hour.
So I gave out two radio buttons namely: one for per8thhour rate and the other one for per16thhour rate.
The problem is it captures both rate but I only want either one value of the radio button to be recorded.
Here's the table:
<table border="1" style="border-collapse: collapse;margin-top: 20px;">
<tr>
<th>AddOns</th>
<th>Deposit</th>
<th>PricePerDay</th>
<th>Price per 8th Hour</th>
<th>Price per 16th Hour</th>
<th></th>
</tr>
<?php
while($row_addon=mysql_fetch_array($result_addon))
{
$add_on_id=$row_addon['addOns_id'];
$add_on=$row_addon['addOns'];
$deposit=$row_addon['Deposit'];
$ppd=$row_addon['PricePerDay'];
$pp8=$row_addon['PricePer8thHour'];
$pp16=$row_addon['PricePer16thHour'];
$status=$row_addon['status'];
?>
<tr>
<td><input type="hidden" name="add_name" value=""><?php echo $add_on;?></td>
<td><input type="hidden" name="add_on"><?php echo $deposit;?></td>
<?php
if(isset($add_on)&&($add_on!=='Driver'))
{
?>
<td><input type="hidden" name="add_on"><?php echo $ppd;?></td>
<td><input type="hidden" name="add_on"><?php echo $pp8;?></td>
<td><input type="hidden" name="add_on"><?php echo $pp16;?></td>
<?php
}
else
{
?>
<td><input type="hidden" name="add_on"><?php echo $ppd;?></td>
<td><input type="radio" name="add_on"><?php echo $pp8;?></td>
<td><input type="radio" name="add_on"><?php echo $pp16;?></td>
<?php
}
?>
<td><input type="checkbox" name="add_item[]" value="<?php echo $add_on_id;?>"/></td>
</tr>
<?php
}
?>
<input type="hidden" name="submit_add">
<tr><td colspan="6"></td></tr>
</table>
I'm recording the value like this:
if(isset($_POST['submit_add']))
{
$add_item=$_POST['add_item'];
echo $add_item_1=implode(',',$add_item);
}
How do I make it possible to catch only the value selected by user for the item driver.
At the same time group them all under one name which is addon so that I can store them as one group in session later?
FULL FORM:
<form action="" method="POST">
<?php
//display pickup location
mysql_select_db($database_bumi_conn, $bumi_conn);
$q="SELECT * FROM tbl_pickup_location WHERE pickup_id='$location'";
$r=mysql_query($q);
$row_show= mysql_fetch_array($r);
?>
<input type="hidden" name="lo" value="<?php echo $location_name=$row_show['pickup_location'];?>">
<?php
echo 'Pickup Location :'.$location_name=$row_show['pickup_location'].'<br/>';
//display dropoff location
mysql_select_db($database_bumi_conn, $bumi_conn);
$q_1="SELECT * FROM tbl_dropoff WHERE dropoff_id='$d_location'";
$r_1=mysql_query($q_1);
$row_show_1= mysql_fetch_array($r_1);
?>
<input type="hidden" name="d_lo" value="<?php echo $location_name_1=$row_show_1['dropoff_location'];?>">
<?php
echo 'Return Location : '.$location_name_1=$row_show_1['dropoff_location'].'<br/>';
?>
<input type="hidden" name="val_1" value="<?php echo $date_value; ?>"/>
<?php
echo 'Pickup date : '.$date_value.'<br/>';
?>
<input type="hidden" name="val_2" value="<?php echo $date_value_2; ?>"/>
<?php
echo 'Return date : '.$date_value_2.'<br/>';
//$days=0;
$days=$diff->format("%a Days");
echo 'Total Rental for : '.$days.'<br/>';
//echo"hdssssssssssssfviodrhfvuhgudfhghdfhijswdjiahsdhsndjfhzsnhio";
$total_days=(int)$days;
?>
<input type="hidden" name="t_days" value="<?php echo $total_days; ?>"/>
<?php
include'calculation.php';
?>
<input type="hidden" name="sum" value="<?php echo $sum;?>">
<?php
mysql_select_db($database_bumi_conn, $bumi_conn);
$q_addon="SELECT * FROM tbl_addons";
$result_addon= mysql_query($q_addon)or die(mysql_error());
echo "<hr width=540>";
?>
<table border="1" style="border-collapse: collapse;margin-top: 20px;">
<tr><th>AddOns</th><th>Deposit</th><th>PricePerDay</th><th>Price per 8th Hour</th><th>Price per 16th Hour</th><th></th></tr>
<?php
while($row_addon=mysql_fetch_array($result_addon))
{
$add_on_id=$row_addon['addOns_id'];
$add_on=$row_addon['addOns'];
$deposit=$row_addon['Deposit'];
$ppd=$row_addon['PricePerDay'];
$pp8=$row_addon['PricePer8thHour'];
$pp16=$row_addon['PricePer16thHour'];
$status=$row_addon['status'];
?>
<tr>
<td><input type="hidden" name="add_name" value=""><?php echo $add_on;?></td>
<td><input type="hidden" name="add_item[]"><?php echo $deposit;?></td>
<td><input type="hidden" name="add_item[]"><?php echo $ppd;?></td>
<td><input type="radio" name="add_item[]" value="eight"><?php echo $pp8;?></td>
<td><input type="radio" name="add_item[]" value="six"><?php echo $pp16;?></td>
<td><input type="checkbox" name="add_item[]" value="<?php echo $add_on_id;?>"/></td>
</tr>
<?php
}
?>
<input type="hidden" name="submit_add">
<tr><td colspan="6"></td></tr>
</table>
<input type="image" src="submit.png" name="submit" value="submit" onclick="return confirm('Do you really want to proceed to checkout?');"href="checkout.php" style="padding-left:20px;" title="Add to cart"/>
</form>
To select one radio button at once, you have to give the all radio button's name same.
Otherwise you have to use javascript/jquery to deselect others when one is selected.
if you give all radio button name same, then when you post it will just post the value of the radio which is selected. You can get it by $_POST['radio_name'] .
Here it is $_POST['add_item'] .
If you are interested to use jquery, you can do that in this way
<script>
$(document).ready(function() {
$('input:radio').click(function(event) {
$('input:radio').not(this).attr('checked', false);
$(this).attr('checked', true);
});
});
</script>

$_REQUEST issue Undefined offset

<?php
$ans=$_REQUEST['ans'];
$qes=$_REQUEST['qes'];
$ra=$_REQUEST['right_op'];
$count=0;
for($i=0; $i<count($ans); $i++)
{
echo "Question".$qes[$i]"<br>";
echo "Ans".$ans[$i]"<br>";
echo "Right Option".$ra[$i]."<br>";
if(isset($ans[$i]) == isset($ra[$i]))
{
$count++;
}
}
?>
when submit the first page, then it shows
Notice: Undefined offset: 1 in C:\xampp\htdocs\result.php on line 9
The first page where the data is posted.
<?php
$i=1;
$x=0;
$y=0;
$z=0;
do{
?>
<tr>
<td width="30"></td>
<td width="30" height="27"><?php echo "$i";?></td>
<td width="493"><?php echo $row_question['question']; ?>
<input type="hidden" name="q_id[<?php// echo $y; ?>]" id="q_id" value=" <?php echo $row_question['q_id']; ?>" />
<input type="hidden" name="qes[<?php echo $y; ?>]" id="qes" value="<?php echo $row_question['question']; ?>" /><input name="right_op[<?php echo $z; ?>]" type="hidden" id="right_op" value="<?php echo $row_question['right_op']; ?>" /></td>
</tr>
<tr>
<td> </td>
<td height="59" align="right"><h3>A)</h3>
<h3>B)</h3>
<h3>C)</h3>
<h3>D)</h3></td>
<td><h3>
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="A" id="ans_0" />
<font color="#FFFFFF"><?php echo $row_question['op_a']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="B" id="ans_1" />
<font color="#FFFFFF"><?php echo $row_question['op_b']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="C" id="ans_2" />
<font color="#FFFFFF"><?php echo $row_question['op_c']; ?></font> </label>
<br />
<label>
<input type="radio" name="ans[<?php echo $x; ?>]" value="D" id="ans_3" />
<font color="#FFFFFF"><?php echo $row_question['op_d']; ?></font> </label>
<br />
</h3></td>
</tr>
<tr>
<td height="17" colspan="3"><hr /></td>
</tr><?php
$i++;
$x++;
$y++;
$z++;
} while ($row_question = mysql_fetch_assoc($question)); ?>
What is wrong in this code. Please tell me in details.
Thank You.
Always use isset() before access
if (isset($ra[$i]) && isset($ans[$i]) && isset($qes[$i])){
// your code
}
or
array_key_exists($i, $ra);
or
It looks like you expect all arrays to be the same size, which is not the case for $ra which is snorter than other arrays, therefore
echo "Right Option".$ra[$i]."<br>";
causes the notice. Looks like you need to fix your $ra size.
$ra doesn't have as many members in the array as $ans does, so when you hit the 2nd iteration of the loop there's nothing in $ra[1] to display. You need to figure out why the length of $ra and $ans are different - without more information about what you're doing it's difficult for us to know.
As #Nikola indicates, you should always check to make sure there's something in the index you're accessing before trying to output it using isset(). Whether or not it's valid for there not to be something in $ra[1] when there is something in $ans[1] is something only you can answer.
Inferring a lot from your code, it looks as though $ra is intended to indicate the right answer. In that case there's only going to be one, so you should use $ra[0] instead of $ra[$i], which will always show the first element in the $ra array. Of course I may be misinterpreting the intent of your code, so YMMV.

How to retrieve value from the Check boxes?

I'm trying to get the emails corresponding to the checkbox using the following codes. But, I'm not getting the correct checked emails in the new variable. Can anyone please check ??
<?php
include("connection.php");
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM contacts WHERE username='$username'");
$num=mysql_num_rows($query);
$info=mysql_fetch_array($query);
$i=0;
$msg='';
?>
<table width="672" border="0">
<?php
$i=0;
while($info)
{
?>
<form action="compose.php" method="post">
<tr style="font-size:14px;">
<td width="21" bgcolor="#f2f2f2"> <input type="checkbox" name="add" onSelect="<?php $msg=$msg.$info['email'].", ";?>"/> </td>
<td width="229" bgcolor="#f2f2f2"> <?php echo $info['email']; ?> </td>
<td width="408" bgcolor="#f2f2f2"> <?php echo $info['name']; ?> </td>
</tr>
<?php
$info=mysql_fetch_array($query);
$i++;
}
$_SESSION['contacts']=$msg;
?>
<tr><td></td><td></td><td><br />
<input class="new-button" type="submit" value="Insert & Compose" name="submit" /></td>
</tr>
</form>
</table>
To get any value back for checkboxes they must have a value=. In your case you probably would want the value to be the according email address.
One problem with your code is using onSelect= instead of value=, and second you didn't print the actual value into the page. Rewrite it to:
<td width="21" bgcolor="#f2f2f2">
<input type="checkbox" name="add"
value="<?php print $info['email']; ?>"/> </td>
If you need the $msg variable to do something, assemble it after the output.
<input type="checkbox" name="add" value="<?php echo $msg.$info['email'];?>"/>
checkbox does not have onSelect event probobly you got value in mind and in PHP code you should echo and what .", " is for?

Categories