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'] ?> /  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!
Related
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.
this is my html code:
<input name="dependencies[]"
and in php i do this:
$dependencies = $_POST['dependencies'];
and when I do this:
print_r($dependencies);
I can see the values like this:
Array ( [0] => [1] => )
My question
I want to add each value from that array to another array:
I didn't know how to do that
I tried:
foreach ($dependencies as $number){
echo $number;
}
but nothing has been printed
Update
this is the html
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
and I can see the check boxes checked or not when I run the page
Update2
the whole code
<form action="../DB/addDependencies.php" method="post">
<input type="hidden" name="questionID" />
<table>
<tr>
<th>Porgugese Name</th>
<th>Englisn Name</th>
<th>Dependent</th>
</tr>
<?php
foreach ($questions as $question) {
?>
<tr>
<td>
<?php echo $question->ptName; ?>
</td>
<td>
<?php echo $question->enName; ?>
</td>
<td>
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
</td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Save" name="submit" />
</form>
You need to change your HTML to this:
<input name="dependencies[<?php $question->id; ?>]" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
Then to test if a checkbox if checked you just need to do something like
function isQuestionChecked($question_id) {
return isset($_POST['dependencies']) && isset($_POST['dependencies'][$question_id]);
}
try to use
foreach ($dependencies as $key=>$val){
var_dump($key);
var_dump ($val);
}
instead of
foreach ($dependencies as $number){
echo $number;
}
;-) hope that will bring some ideas to your mind in this case
I have a form with multiple checkboxes that can be selected. If a user wanted they could go in and change the value in the checkbox. I want to know if there is a way I can not accept the form or display an error if the value submitted was not one of the original choices displayed. Here is my code:
HTML Code:
<form action="" method="post">
<table>
<tr>
<td>
<input type="checkbox" name="checkbox[]" value="1">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="2">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="3">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="4">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="5">
</td>
<td>
<input type="checkbox" name="checkbox[]" value="6">
</td>
</tr>
</table>
<input type="submit" name="submit" value="submit">
</form>
PHP Code:
<?php
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo $check;
}
}
If you want, since html is static (which I dont know why), you could initialize some original values, then compare it. Consider this example:
<?php
$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
foreach($submitted_values as $value) {
if(!in_array($value, $original_values)) {
// this particular submitted value does not belong to the original values that is set
echo 'not good';
exit;
}
}
// all good if it goes down here
echo 'all good';
}
?>
<form method="POST" action="">
<table border="0" cellpadding="10">
<tr>
<?php foreach($original_values as $value): ?>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $value; ?>" /></td>
<?php endforeach; ?>
</tr>
<tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
</table>
</form>
Note: To test, try to change the values thru the browser [most likely F12], change the dom values. Example: Try to change a checkbox value attribute to 7 or 100, check it the submit.
Also another way:
$original_values = array(1, 2, 3, 4, 5, 6);
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
$differences = array_diff($submitted_values, $original_values);
if(count($differences) > 0) {
echo 'not good';
exit;
}
// all good if it goes down here
echo 'all good';
}
Print all the values inside a table:
if(isset($_POST['submit'], $_POST['checkbox'])) {
$submitted_values = $_POST['checkbox'];
$count = count($submitted_values);
echo "<table border='1' cellpadding='10'>";
echo "<tr><td colspan='$count'>Values</td></tr>";
echo "<tr>";
foreach($submitted_values as $value) {
echo "<td>$value</td>";
}
echo "</tr>";
echo "</table>";
}
Store values in a separate PHP file, checkbox_values.php:
<?php
$checkbox_values = array(1, 2, 3, 4, 5, 6);
?>
And your form, another PHP file, main.php:
<?php
require 'checkbox_values.php';
?>
<form method="POST" action="checkbox_process.php">
<table border="0" cellpadding="10">
<tr>
<?php foreach ( $checkbox_values as $check ) { ?>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $check; ?>" /></td>
<?php } ?>
</tr>
<tr><td colspan="6"><input type="submit" name="submit" value="submit" /></td></tr>
</table>
</form>
And checkbox_process.php:
<?php
require 'checkbox_values.php';
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
if ( in_array($check, $checkbox_values) ) { echo $check; }
}
}
?>
my form data im listed my table sample 10 records.
<td class='table-checkbox'><input type="checkbox" name="product[]" value="1" class='selectable-checkbox'></td>
<td><div class="input-mini">
<input type="text" id="spinnn" name="quantity[]" value="5" class='spinner'/>
</div>
</td>
</tr>
.....
.....
my php code
$carino = $_POST['carino'];
$quantity = $_POST['quantity'];
$product = $_POST['product'];
foreach($product as $product){
foreach($quantity as $quantity ){
$sql = "INSERT INTO mytable (product,quantity,cno) VALUES ('$product','$quantity','$carino')";
mysql_query($sql);
}}
i want to be insert this data my table. but my foreach is wrong how can i do ?
product is unique
my table
product - quantity - cno
1 5 2
2 10 2
http://pastie.org/private/nwrrinnxlrumt6p3kuyq#11,13-14,19
This:
<?php
if(isset($_POST['submit'])) {
$carino = "2";
$adet = $_POST['adet'];
$urunno = $_POST['urunno'];
$total = count($_POST['adet']);
echo '<hr>';
foreach ($urunno as $checked)
{
$value = $checked - 1;
echo "Value of Urunno: $checked: Adet: $adet[$value] <br>";
echo "INSERT INTO member_interests
(`urun`,`adet`,'carino')
VALUES
('$checked','$adet[$value]','$carino')<br>";
}
}
?>
<tr>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="1">Product One</td>
<input type="text" id="spinnn" name="adet[]" class='spinner'/>
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="2">Product Two</td>
<input type="text" id="spinnn" name="adet[]" class='spinner'/>
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="3">Product Three</td>
<input type="text" id="spinnn" name="adet[]" class='spinner'/>
<td><div class="input-mini">
<input type="submit" name="submit" value="run" />
</form>
</div>
</td>
</tr>
</tbody>
</table>
</div>
Output:
Value of Urunno: 2: Adet: 2
INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('2','2','2')
Value of Urunno: 3: Adet: 3
INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('3','3','2')
The logic can be as follows (think of the code yourself, or search on the internet):
Make sure that all POST arrays are the same length (they should be)
Loop over the count from 0 to the length of the arrays
Insert the value at that point in each of the arrays into the database.
A quick tip: you are very susceptible to SQL injection. If this is production code, either use prepared queries, or use a PHP database wrapper like PDO to do it for you. The mysql_... functions are deprecated.
Try this one
<?php
//add the database connection
?>
<form name="myform" action="">
<table>
<?php
$length=//how many time below check box repeat in this page
for($i=0;$i<$length;$i++){?>
<tr>
<td class='table-checkbox'>
<input type="checkbox" name="urunno[]" value="<?php echo $i;?>" class='selectable-checkbox'>
</td>
<td>c1k</td>
<td>2147483647</td>
<td>520</td>
<td>435345345 A.Ş.</td>
<td>
<div class="input-mini">
<input type="text" id="spinnn" name="adet<?php echo $i;?>" class='spinner'/>
</div>
</td>
</tr>
<?php
}?>
</table>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_post['submit']))
{
$carino = $_POST['carino']; //here this element not present in question, i am also follow this
$adet = $_POST['adet'];
$urunno = $_POST['urunno'];
$length=sizeof($urunno);
for($i=0;$i<$length;$i++)
{
$urun1=$urunno[$i];
$adet1=$adet[$urun1];
$sql = "INSERT INTO table (urunno,adet,cno) VALUES ('$urun1','$adet1','$carino')";
mysql_query($sql);
//the above code insert sql work in every time //otherwise use batch insert
//refrer this link http://stackoverflow.com/questions/12960569/mysql-bulk-insert-via-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