I have table which is dynamically generated. I am getting all correct data. I want to store html data to database table. So how do I store it? following is the code for html table
foreach($res->result() as $row ){
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$row->product_id."'
name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$product_name."'
name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' style='width:30%;' id='packing' name='packing[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:80%;' readonly=''
value='".$row->quantity."' name='quantity[]'/></td>";
echo "<td><input type='text' name='rate' style='width:80%;' class='rate'
name='rate[]'/></td>";
echo "<td><input type='text' style='width:100%;' class='amount' readonly=''
name='amount[]'/></td>";
echo "</tr>";
}
On form submit I have done this..
$data['cart']=array(
'product_id'=>$this->input->post('product_id'),
'product_name'=>$this->input->post('product_name'),
'packing'=>$this->input->post('packing'),
'quantity'=>$this->input->post('quantity'),
'rate'=>$this->input->post('rate'),
'amount'=>$this->input->post('amount'),
);
print_r($data);
$i=0;
foreach($data['cart'] as $row){
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$packing=$row['packing'];
$quantity=$row['quantity'];
$rate=$row['rate'];
$amount=$row['amount'];
$query = $this->db->query("insert into
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
$i++;
}
But it displaying only last record in table..Anybody has any idea about this?? I want total table records to save in another table.
$product_id =$this->input->post('product_id'),
$product_name =$this->input->post('product_name'),
$packing =$this->input->post('packing'),
$quantity =$this->input->post('quantity'),
$rate =$this->input->post('rate'),
$amount =$this->input->post('amount'),
$total = count ($product_id);
for($i=0;$i<$total;$i++){
$query = $this->db->query("insert into
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
("$product_id[$i]","$product_name[$i]","$packing[$i]","$quantity[$i]","$rate[$i]","$amount[$i]")");
}
But it will gives error when no data found for any data So please validate all fields before this code
The trick here is to use the [] notation in your HTML form elements.
So instead of this:
<input type='text' name='someVar'/>
You have:
<input type='text' name='someVar[]'/>
If you have specific keys you can do:
<input type='text' name='someVar[myKey1]'/>
In your case I would do this (HTML generation):
foreach($res->result() as $row ){
echo "<tr>";
echo "<td><input type='hidden' value='".$row->product_id."' name='product_id[]'/></td>";
echo "<td><input type='hidden' value='".$product_name."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' name='packing[]'/></td>";
echo "<td><input type='text' class='quantity' readonly='' value='".$row->quantity."' name='quantity[]'/></td>";
echo "<td><input type='text' name='rate' class='rate' name='rate[]'/></td>";
echo "<td><input type='text' class='amount' readonly='' name='amount[]'/></td>";
echo "</tr>";
}
Notice the [] after every input name? And this would be the code that responds to the form submission:
foreach ($_GET['product_id'] as $index => $product_id) {
$product_id = $_GET['product_id'][$index];
$product_name = $_GET['product_name'][$index];
$packing = $_GET['packing'][$index];
$quantity = $_GET['quantity'][$index];
$rate = $_GET['rate'][$index];
$amount = $_GET['amount'][$index];
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values ('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
}
http://php.net/manual/en/faq.html.php#faq.html.arrays
Related
Help me guys. I have a problem on how to sum all records of students
in each rows. this is my php code
while ($students = mysql_fetch_array($result)) {
echo '<tr>';
echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
echo "<td>{$students['fld_name']}</td>";
echo "<td><input type='text' size='5' name='fld_quiz1[$i]' value='{$students['fld_quiz1']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz2[$i]' value='{$students['fld_quiz2']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz3[$i]' value='{$students['fld_quiz3']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz4[$i]' value='{$students['fld_quiz4']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz5[$i]' value='{$students['fld_quiz5']}' /></td>";
echo '<td> total here </td>';
echo '</tr>';
++$i;
}
This is my output
you can just add all values and store it in one variable and show it in last column. refer below,
while ($students = mysql_fetch_array($result)) {
$total = $students['fld_quiz1'] + $students['fld_quiz2'] + $students['fld_quiz3'] + $students['fld_quiz4'] + $students['fld_quiz5'];
echo '<tr>';
echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
echo "<td>{$students['fld_name']}</td>";
echo "<td><input type='text' size='5' name='fld_quiz1[$i]' value='{$students['fld_quiz1']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz2[$i]' value='{$students['fld_quiz2']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz3[$i]' value='{$students['fld_quiz3']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz4[$i]' value='{$students['fld_quiz4']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz5[$i]' value='{$students['fld_quiz5']}' /></td>";
echo '<td>'.$total.'</td>';// Show it here
echo '</tr>';
++$i;
}
$total will make a sum of all the quiz like 1 to 5 and print that variable to last td as below
while ($students = mysql_fetch_array($result)) {
$total = ($students['fld_quiz1'] + $students['fld_quiz2'] + $students['fld_quiz3'] + $students['fld_quiz4'] + $students['fld_quiz5']);
//if marks are from 100 then
$avg = ($total * 100) / 500;
echo '<tr>';
echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
echo "<td>{$students['fld_name']}</td>";
echo "<td><input type='text' size='5' name='fld_quiz1[$i]' value='{$students['fld_quiz1']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz2[$i]' value='{$students['fld_quiz2']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz3[$i]' value='{$students['fld_quiz3']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz4[$i]' value='{$students['fld_quiz4']}' /></td>";
echo "<td><input type='text' size='5' name='fld_quiz5[$i]' value='{$students['fld_quiz5']}' /></td>";
echo '<td> ' .$total. ' </td>';
echo '</tr>';
++$i;
}
I am trying to make a simple program where the user can multiply their inputs. But it always return a 0.
I already did:
$price = (int)$_POST['txtPrice'];
$quan = (int)$_POST['quantity'];
$ans = $quan * $price;
echo "$ans";
But returns 0 always.
This is the page where all the textboxes came from:
echo "<tr>";
echo "<td><input type='text' name='quantity' placeholder='How Many?'></td>";
echo "<td><input type='text' name='txtName' value='$name'></td>";
echo "<td><input type='text' name='txtPrice' value='$price'></td>";
Using your same code and just adding the form tag and a submit works for me:
<?php
if(!empty($_POST)) {
print_r($_POST);
$price = (int)$_POST['txtPrice'];
$quan = (int)$_POST['quantity'];
$ans = $quan * $price;
echo "$ans<br>";
}
echo "<tr>";
echo "<form action='index.php' method='post'>";
echo "<td><input type='text' name='quantity' placeholder='How Many?'> </td>";
echo "<td><input type='text' name='txtName' value='$name'></td>";
echo "<td><input type='text' name='txtPrice' value='$price'></td>";
echo "<input type='submit'>";
echo "</form>";
Execute it with php -S 127.0.0.1 index.php, $ans contains the value from the multiplication.
Please help, each row in the table has save button for editing/adding content. I have 3 textboxes in each row,PersoninCharge,PIC_Comments and Status. The user can add/edit these textboxes whenever they click the save button on that particular row. The problem is, whenever I add/edit data in one row, the save button can't read what particular row I edited.
The save button is perfectly running if I searched the invoice number first, but what I want is to directly edit/add the data in each row without searching it first.
Here's the code:
For the table:
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo"<tr class=output2>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "<td>$row[4]</td>";
echo "<td>$row[5]</td>";
echo "<td>$row[6]</td>";
echo "<td>$row[7]</td>";
echo "<td>$row[8]</td>";
echo "<td>$row[9]</td>";
echo "<td>$row[10]</td>";
echo "<td>$row[11]</td>";
echo "<td>$row[12]</td>";
echo "<td><input type='text' name='pic' value='$row[17]'></td>";
echo "<td><input type='text' name='comt' value='$row[18]'></td>";
echo "<td><input type='text' name='stat' value='$row[19]'></td>";
echo "<td><form name='update' method='POST'><input type='submit' name='save_btn' value='SAVE' style='font-size:1em;'/></form></td>";
echo "<td><input type='hidden' name='idtxt' value='$row[0]'/></td>";
echo "</tr>";
}
}
else
{
echo '<h3>No result found! </h3><br>';
}
$con->close();
For Save button:
if(isset($_POST['save_btn']))
{
$query2="UPDATE invalid_invoice SET UpdateBy='".$_SESSION['login_user']."', UpdateDateTime=NOW(), PersoninCharge='".$_POST['pic']."', PIC_Comments='".$_POST['comt']."', Status='".$_POST['stat']."' WHERE ID='".$_POST['idtxt']."'";
$con->query($query2);
$con->close();
echo '<h3 class="datasuccess">Data successfully added!</h3>';
}
I have edited your form. please have a look
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo"<form name='update' method='POST'><tr class=output2>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "<td>$row[4]</td>";
echo "<td>$row[5]</td>";
echo "<td>$row[6]</td>";
echo "<td>$row[7]</td>";
echo "<td>$row[8]</td>";
echo "<td>$row[9]</td>";
echo "<td>$row[10]</td>";
echo "<td>$row[11]</td>";
echo "<td>$row[12]</td>";
echo "<td><input type='text' name='pic' value='$row[17]'></td>";
echo "<td><input type='text' name='comt' value='$row[18]'></td>";
echo "<td><input type='text' name='stat' value='$row[19]'></td>";
echo "<td><input type='submit' name='save_btn' value='SAVE' style='font-size:1em;'/></td>";
echo "<td><input type='hidden' name='idtxt' value='$row[0]'/></td>";
echo "</tr></form>";
}
}
else
{
echo '<h3>No result found! </h3><br>';
}
$con->close();
I have an array of Textbox, by default they are all disabled... Some Textbox are empty some are not based on the return values of my query.
while ($imps_row1 = sqlsrv_fetch_array($stmt_line_util3,SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type='text' disabled='disabled' class='txtDis' name='txt1[]' value='".$imps_row1['qualified_borrower']."'></td>";
echo "<td><input type='text' disabled='disabled' class='txtDis' name='txt2[]' value='".$imps_row1['allowed_borrower']."'></td>";
echo "</tr>";
}
Is there any way that the moment the page loads, all the textbox that are not empty will not be disabled?
Something this might work using just php:
while ($imps_row1 = sqlsrv_fetch_array($stmt_line_util3,SQLSRV_FETCH_ASSOC))
{
echo "<tr>";
echo "<td><input type='text' ".(empty($imps_row1['qualified_borrower'])?"disabled='disabled'":'')." class='txtDis' name='txt1[]' value='".$imps_row1['qualified_borrower']."'></td>";
echo "<td><input type='text' ".(empty($imps_row1['allowed_borrower'])?"disabled='disabled'":'')." class='txtDis' name='txt2[]' value='".$imps_row1['allowed_borrower']."'></td>";
echo "</tr>";
}
Just uses a ternary statement to check if the variable you are echo'ing to the value is empty and if so echo's "disabled...".
I am just trying to get the values from a table, but for some reason GET isn't working for me, or I am doing something wrong. Here is how I create my table in one php file:
<?php
.
.
.
.
echo "<tr>";
echo "<td>Login ID</td>";
$j=1;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
$j=2;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password</td>";
$j=3;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Birthday</td>";
$j=4;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Address</td>";
$j=5;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email</td>";
$j=6;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone Number</td>";
$j=7;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
?>
The names should be the number 1-7 right?
In another php file I attempt to access the values in those fields with he following code:
<?php
.
.
.
.
$login_id = $_GET['1'];
$name = $_GET['2'];
$pw = $_GET['3'];
$bday = $_GET['4'];
$address = $_GET['5'];
$email = $_GET['6'];
$phno = $_GET['7'];
echo "new: $login_id, $name, $pw, $bday, $address, $email, $phno";
?>
Here is what I end up getting back: new: , , , , , ,
So what is it that I am doing wrong? I can't seem to find anything wrong with my code. I know I should probably be using $_POST for the password.
<?php
echo "<form action='otherfile.php' method='get'><tr>";
echo "<td>Login ID</td>";
$j=1;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
$j=2;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password</td>";
$j=3;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Birthday</td>";
$j=4;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Address</td>";
$j=5;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email</td>";
$j=6;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone Number</td>";
$j=7;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Submit Data</td>";
echo "<td><input name='submitdata' type='submit' value='submit' size=20/></td>";
echo "</tr><form>";
?>
if you want submit then get the all value in otherfile.php file
try its working...
You have not put the <form> tag. Add tag in begining
<form action='another_page.php' method='get'>
and in the end write
<input type='submit' name='submit>
</form>
What it will do on submit it will forward the values to the another page.
<form name="login" method="get" action="youraction.php">
echo "<td><input name=$j type='text' value=$j /></td>";
</form>
youraction.php
---------------
<?php
if($_GET)
{
print_r($_GET);
}
?>