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>';
}
}
Related
Issue, how can I POST needed data (id,username, etc.) only for one row, from while loop, when submit?
Code (I am using POST as call "function" delete_member, with if condition, what is better way?).
if(isset($_POST["delete_member"]))
{
# Delete member
echo $id=$_POST["id"];
$sql="delete from members where id = $id";
mysqli_query($db,$sql) ? $status = true : $status = false;
message($status);
}
$select_members = mysqli_query($db,"select * from members");
while($row = mysqli_fetch_array($select_members))
{
echo('
<tr>
<td><input type="text name="id" value="'.$row['id'].'" readonly></td>
<td><input type="text name="username" value="'.$row['username'].'" readonly></td>
<td><input type="text name="password" value="'.$row['password'].'" readonly></td>
<td><input type="text name="active" value="'.$row['active'].'" readonly></td>
<td><input type="submit" name="delete_member" value="Smazat"></td>
<td><input type="submit" name="send_email" value="Odeslat email"></td>
<tr>
');
}
Nothing stored in echo $id=$_POST["id"];. Is possible send id from input type="submit"? For example like this.
<input type="submit" name="delete_member" id=".$row['id']." value="Smazat">
Thanks.
if(isset($_POST["delete_member"]))
{
# Delete member
echo $id=$_POST["id"];
$sql="delete from members where id = $id";
mysqli_query($db,$sql) ? $status = true : $status = false;
message($status);
}
$select_members = mysqli_query($db,"select * from members");
while($row = mysqli_fetch_array($select_members))
{
echo('
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['username'].'</td>
<td>'.$row['password'].'</td>
<td>'.$row['active'].'</td>
<td><form method="post" action="HERE_FILL_IN_SCRIPT_URL"><input type="hidden" name="id" value="'.$row['id'].'"><input type="submit" name="delete_member" value="Smazat"><input type="submit" name="send_email" value="Odeslat email"></form></td>
<tr>
');
}
at the end, best method is change input to button, where value is not displayed, so value can be $row['id'] and name is between tags<button>NAME</button>.
if(isset($_POST["deleteMember"]))
{
# Delete member
$id=$_POST["deleteMember"];
$sql="delete from members where id = $id";
mysqli_query($db,$sql) ? $status = true : $status = false;
message("Smazání člena",$status);
}
while($row = mysqli_fetch_array($select_members))
{
echo('
<tr>
<td><input type="text name="id[]" value="'.$row['id'].'" readonly></td>
<td><input type="text name="username[]" value="'.$row['username'].'" readonly></td>
<td><input type="text name="password[]" value="'.$row['password'].'" readonly></td>
<td><input type="text name="active[]" value="'.$row['active'].'" readonly></td>
<td><input type="text name="sentEmailAll" value="'.$row['email_sent'].'" readonly></td>
<td><button type="submit" name="deleteMember" value="'.$row['id'].'">Vymazat člena</button></td>
<tr>
');
}
But I thing this is not elegant, I believe there is better way to solve similar things.
I'm creating a form using HTML and PHP. I have created a form which I want to submit and save that data in database.
I'm trying to submit a form with data that comes from a while loop. All input values are getting generated by while loop.
The code looks like this.
<table width="1348" border="0" class="table table-striped" >
<tr>
<td width="106"> </td>
<td width="332"><strong>Product Code</strong></td>
<td width="375"><strong>Product Name</strong></td>
<td width="211"><strong>QTY</strong></td>
</tr>
<?php
$i = 0;
$rowset = mysql_query("select * from product_detail where productID='".$data['productCode']."'");
while($stuff = mysql_fetch_array($rowset)){
?>
<tr>
<td><input type="text" name="code[<?php echo $i?>]" value="<?php enter code hereecho $stuff['code'];?>"/></td>
<td><input type="text" name="name[<?php echo $i?>]" value="<?php echo $stuff['name'];?>" size="50"/></td>
<td><input type="text" name="qty[<?php echo $i?>]" value="<?php echo $stuff['qty'];?>" size="10"/></td>
</tr>
<?php $i++; }?>
<tr id="last">
</table>
<input type="submit" name="save id="save" class="btn btn-primary btn-lg"/>
This is the code to add the data to database.
$code=$_POST['code'.$i];
$name=$_POST['name'.$i];
$qty=$_POST['qty'.$i];
$query = mysqli_query($con,"insert into stock(productCode, productName, qty) values ('".$code."', '".$name."','".$qty."')") or die(mysqli_error($con));
First, use prepared statement with bind_param as your script is totally exposed to sql injection.
Second, you can add input type hidden for the number of rows
<form action="" method="POST">
<table width="1348" border="0" class="table table-striped" >
<tr>
<td width="106"> </td>
<td width="332"><strong>Product Code</strong></td>
<td width="375"><strong>Product Name</strong></td>
<td width="211"><strong>QTY</strong></td>
</tr>
<?php
$data['productCode'] = "1"; // sample data
$stmt = $con->prepare("SELECT * FROM product_detail WHERE productID = ?");
$stmt->bind_param("i", $data['productCode']);
$stmt->execute();
$result = $stmt->get_result();
$i = 0;
while($stuff = $result->fetch_assoc()) {
?>
<tr>
<td></td>
<td><input type="text" name="code[<?php echo $i; ?>]" value="<?php echo $stuff['code'];?>"/></td>
<td><input type="text" name="name[<?php echo $i; ?>]" value="<?php echo $stuff['name']; ?>" size="50" /></td>
<td><input type="text" name="qty[<?php echo $i; ?>]" value="<?php echo $stuff['qty']; ?>" size="10" /></td>
</tr>
<?php
$i++;
}
?>
<input type="hidden" name="count" value="<?php echo $i; ?>" />
<tr id="last">
</table>
<input type="submit" name="save" id="save" class="btn btn-primary btn-lg"/>
</form>
post count with the form
<?php
if (isset($_POST['save'])) {
$count = $_POST['count'];
for ($i = 0; $i < $count; $i++) {
$code = $_POST['code'][$i]; // check empty and check if interger
$name = $_POST['name'][$i]; // check empty and strip tags
$qty = $_POST['qty'][$i]; // check empty and check if interger
$stmt = $con->prepare("INSERT INTO stock (productCode, productName, qty) VALUES (?, ?, ?)");
$stmt->bind_param("iss",$code,$name,$qty);
$stmt->execute();
}
}
?>
You may also want to check if post values are empty with other necessary validation before insert
Since the table is dynamically filled, you need to use an array as the name attribute
<table>
<tr>
<th>Name</th>
<th>Present</th>
<th>Excused</th>
<th>Unexcused</th>
<th>Ext</th>
</tr>
<?php
$query = "select * from TbCard";
$sql = mysqli_query($connect, $query);
$count = 0;
while ($data = mysqli_fetch_array($sql)) {
?>
<tr>
<td>
<input name="tableRow[<?php echo $count; ?>]['dataName']" id='name' type='text' value="<?php echo $data['Name'];?>" readonly style='border:none;width:350px'></input>
</td>
<td>
<input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Present"> Present
</td>
<td>
<input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Excused"> Excused
</td>
<td>
<input name="tableRow[<?php echo $count; ?>]['status']" type="radio" value="Unexcused"> Unexcused
</td>
</tr>;
<?php
$count++;
}
?>
</table>
The php would be something like this, assuming that the data has values in it:
$tableRow = $_POST['tableRow'];
foreach($tableRow as $row){
/* here insert data from post */
echo $row['dataName'].' '.$row['status'].'<br/>';
}
To see the content of the array, use print_r($tableRow)
in this case i use a name tableRow
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.
Cannot find out what problems,for example at the beginning i add patient details, i selected allergies and asthma and store into database,but i want to edit the details, it only checked asthma (last value)...please help me find out the answer Thank you.
<tr>
<td>Past Medical Records</td>
<td><?php
$DiseaseSplit = $medicalRec['Diseases'];
$array = explode(", ",$DiseaseSplit);
foreach ($array as $item) {
echo "<li>$item</li>";
}
?></td></tr>
<tr><label class="q" for="q1"></label>
<td><input name="q1[]" type="checkbox" value="NONE" <?php if($item == "NONE") { echo 'checked=\"checked\"';}?>>None</td></tr>
<tr>
<td><input name="q1[]" type="checkbox" value="ALLERGIES" <?php if($item == "ALLERGIES") { echo 'checked=\"checked\"';}?>>Allergies</td>
<td align="left"><input name="q1[]" type="checkbox" value="BLOOD DYSCRASIAS" <?php if($item == "BLOOD DYSCRASIAS") { echo 'checked=\"checked\"';}?>>Blood Dyscrasias</td></tr>
You're overwriting $item for every iteration of the loop.
I would try making a hash map to see which values should be shown/hidden:
<tr>
<td>Past Medical Records</td>
<td><?php
$data = array();
$DiseaseSplit = $medicalRec['Diseases'];
$array = explode(", ",$DiseaseSplit);
foreach ($array as $item) {
$data[$item] = true;
echo "<li>$item</li>";
}
?></td></tr>
<tr><label class="q" for="q1"></label>
<td><input name="q1[]" type="checkbox" value="NONE" <?php if(isset($data["NONE"])) { echo 'checked=\"checked\"';}?>>None</td></tr>
<tr>
<td><input name="q1[]" type="checkbox" value="ALLERGIES" <?php if(isset($data["ALLERGIES"])) { echo 'checked=\"checked\"';}?>>Allergies</td>
<td align="left"><input name="q1[]" type="checkbox" value="BLOOD DYSCRASIAS" <?php if(isset($data["BLOOD DYSCRASIAS"])) { echo 'checked=\"checked\"';}?>>Blood Dyscrasias</td></tr>
I've a doubt. I've 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:
<html>
<head>
</head>
<body>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
</tr>
<tr>
<td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
</tr>
<tr>
<td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
and below is the PHP part.
<?php
$a=$_POST['a1'];
$b=$_POST['b1'];
$c=$_POST['c1'];
$facilityArray = $_POST['facility'];
$facility = "";
if(count($facilityArray) > 0)
{
foreach($facilityArray as $fac)
{
$facility .= " " . $fac;
}
}
echo $facility; echo "<br>";
echo $a; echo "<br>";
echo $b; echo "<br>";
echo $c;
?>
With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance... :(
Your textboxes should also be in an array post to achieve this.
To achieve this change the input lines as:
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
From php you'll be getting the posted textboxes in an array as:
$textbox=$_POST['textboxes'];
You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
I've also added a name to your submit button so you only check the form when it is submitted.
Your page should now look something like this:
<?php
if(isset($_POST['submit']))
{
$textbox=$_POST['textboxes'];
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
?>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
UPDATE:
To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['textboxes']))
{
$textbox=$_POST['textboxes'];
if(isset($_POST['facility']))
{
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
}
}
?>
Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully
if(isset($_POST['textboxes']))
AND
if(isset($_POST['facility']))