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.
Related
I am trying to set up a form on my page using php and am looping through database query results to generate it.
For a bit of understanding, this is for an ecommerce site selling products with different attributes at different prices in different countries. For example, we could have a product like a t-shirt for example. This can have up to 15 different attributes (in this case colours). Different colours have different prices, and we need to adjust prices depending on what country we are selling to (depending on shipping costs etc). For this example say there is only two countries we are selling to (although this could increase).
So what I'm looking for is something like:
<table>
<tr><td>Colour 1</td><td>Price country 1</td><td>Price country 2</td></tr>
<tr><td>Colour 2</td><td>Price country 1</td><td>Price country 2</td></tr>
<tr><td>Colour 3</td><td>Price country 1</td><td>Price country 2</td></tr>
</table>
My php is as follows:
$attributeResult = DB::run("SELECT * FROM attributes WHERE id='$new_product_attribute_id'");
$countryResult = DB::run("SELECT * FROM countries");
foreach ($attributeResult as $value) {
for ($i = 1; $i <= 15; $i++) {
$attributeColumn = "attribute".$i;
$checkbox = "checkbox".$i;
$priceValue = "priceValue".$i;
$priceCurrency = "priceCurrency".$i;
$priceAttribute = "priceAttribute".$i;
if($value[''.$attributeColumn.''] != ""){
$price_form .= '<tr><td><input type="checkbox" name='.$checkbox.' id='.$checkbox.' value='.$checkbox.'>'.$value[''.$attributeColumn.''].'</td>';
foreach ($countryResult as $countryVal) {
$price_form .= '<td>';
$price_form .= '<input type="text" placeholder='.$countryVal['currency'].' name='.$priceValue.'_'.$countryVal['id'].' size="10">';
$price_form .= '<input type="hidden" value='.$countryVal['id'].' name='.$priceCurrency.'_'.$countryVal['id'].'>';
$price_form .= '<input type="hidden" value='.$i.' name='.$priceAttribute.'_'.$countryVal['id'].'>';
$price_form .= '</td>';
}
$price_form .= '</tr>';
}
}
}
This is what is output on screen:
<tbody>
<tr>
<td><input type="checkbox" name="checkbox1" id="checkbox1" value="checkbox1">Black</td>
<td>
<input type="text" placeholder="€" name="priceValue1_1" size="10">
<input type="hidden" value="1" name="priceCurrency1_1">
<input type="hidden" value="1" name="priceAttribute1_1">
</td>
<td>
<input type="text" placeholder="£" name="priceValue1_2" size="10">
<input type="hidden" value="2" name="priceCurrency1_2">
<input type="hidden" value="1" name="priceAttribute1_2">
</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox2" id="checkbox2" value="checkbox2">White</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox3" id="checkbox3" value="checkbox3">Purple</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox4" id="checkbox4" value="checkbox4">Yellow</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox5" id="checkbox5" value="checkbox5">Red</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox6" id="checkbox6" value="checkbox6">Orange</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox7" id="checkbox7" value="checkbox7">Blue</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox8" id="checkbox8" value="checkbox8">Green</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox9" id="checkbox9" value="checkbox9">Pink</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox10" id="checkbox10" value="checkbox10">Grey</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox11" id="checkbox11" value="checkbox11">Brown</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox12" id="checkbox12" value="checkbox12">Spearmint</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox13" id="checkbox13" value="checkbox13">Lime Green</td>
</tr>
</tbody>
As you can see it is entering the second for each loop correctly the first time around but not again.
I have been playing around with this for some time now and cannot figure out why this is happening. (but it is monday and I am gone a bit braindead trying to figure it out) Is there something I'm missing? Any help with this would be greatly appreciated.
I changed my php to the following and I got the result I was looking for:
$attributeResult = DB::run("SELECT * FROM attributes WHERE id='$new_product_attribute_id'");
foreach ($attributeResult as $value) {
for ($i = 1; $i <= 15; $i++) {
$attributeColumn = "attribute".$i;
$checkbox = "checkbox".$i;
$priceValue = "priceValue".$i;
$priceCurrency = "priceCurrency".$i;
$priceAttribute = "priceAttribute".$i;
if($value[$attributeColumn] != ""){
$price_form .= "<tr><td><input type='checkbox' name='$checkbox' id='$checkbox' value='$checkbox'>$value[$attributeColumn]</td>";
$countryResult = DB::run("SELECT * FROM countries");
while ($row = $countryResult->fetch(PDO::FETCH_ASSOC)){
$country_id = $row["id"];
$country_currency = $row["currency"];
$price_form .= '<td>';
$price_form .= '<input type="text" placeholder='.$country_currency.' name='.$priceValue.'_'.$country_id.' size="10">';
$price_form .= '<input type="hidden" value='.$country_id.' name='.$priceCurrency.'_'.$country_id.'>';
$price_form .= '<input type="hidden" value='.$i.' name='.$priceAttribute.'_'.$country_id.'>';
$price_form .= '</td>';
}
}
$price_form .= '</tr>';
}
}
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!
i want to set a default value to radio buttons for my form. the default value should match the value from database. i know how to set default value that is my by using checked="checked". in my case, the problem lies in how to use database value as default in radiobuttons.
<tr>
<td>ASP.NET</td>
<td><input type="radio" name="asp" value="not at all competent" ></td>
<td><input type="radio" name="asp" value="little competent" ></td>
<td><input type="radio" name="asp" value="moderately competent"</td>
<td><input type="radio" name="asp" value="extremely competent"></td>
</tr>
You can do it like the following:
<?php
//Retrieve your value here
$query="select * from technical_skills where student_id='$student_id'";
$result=mysqli_query($conn,$query);
$row=mysqli_fetch_array($result);
// Assign the column to the variable
$dbValue = $row['columnname'];
?>
<tr>
<td>ASP.NET</td>
<td><input type="radio" name="asp" value="not at all competent"<?php echo ($dbValue=='not at all competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="little competent"<?php echo ($dbValue=='little competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="moderately competent"<?php echo ($dbValue=='moderately competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="extremely competent"<?php echo ($dbValue=='extremely competent'?' checked=checked':''); ?> ></td>
</tr>
You can define an empty variable for each option and set them to 'checked=checked' according to the DB returned value.
example:
$not_competent = ''
if($dbResult == 'not_competent') $not_competent = 'checked=checked';
Do the same with the other options and then print the table.
Write the table between "..." so PHP can pharse the variables and show
the proper info
print "<tr>
<td>ASP.NET</td>
<td><input type='radio' name='asp' value='not at all competent' $not_competent></td>
<td><input type='radio' name='asp' value='little competent' $little competent></td>
<td><input type='radio' name='asp' value='moderately competent' $moderately_competent></td>
<td><input type='radio' name='asp' value='extremely competent' $extremely_competent></td>
</tr>";
Cheers!
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