I need to display an HTML table with the data that i get from a PhP form. I did a thing like this but It doesn't work properly. It shows the data row on top of the title of the columns. Here's the code:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Nome: <input type="text" name="nome">
Ragione sociale: <input type="text" name="rag">
Indirizzo: <input type="text" name="ind">
Partita IVA: <input type="text" name="iva">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_REQUEST['nome']);
$rag = htmlspecialchars($_REQUEST['rag']);
$ind = htmlspecialchars($_REQUEST['ind']);
$iva = htmlspecialchars($_REQUEST['iva']);
}
?>
<table>
<tbody>
<tr>
<?php
$a = array($name,$rag,$ind,$iva);
for ($i=0; $i<count($a); $i++){
print_r($a[$i]);
echo " ";
}
?>
</tr>
<tr>
<td>|Nome|</td>
<td>Ragione Sociale|</td>
<td>Indirizzo|</td>
<td>Partita IVA|</td>
</tr>
</tbody>
</table>
</body>
</html>
You print your data in the first row, without outputting any <td> for your data. Then in the second row you print your titles/headers. Change your table like so, see if that's what you want:
<table>
<thead>
<tr>
<th>|Nome|</th>
<th>Ragione Sociale|</th>
<th>Indirizzo|</th>
<th>Partita IVA|</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$a = array($name,$rag,$ind,$iva);
for ($i=0; $i<count($a); $i++){
echo "<td>";
echo $a[$i];
echo "</td>";
}
?>
</tr>
</tbody>
</table>
More on tables: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table
Edit: I'd also suggest to place your table inside the if ($_SERVER["REQUEST_METHOD"] == "POST") { so it only gets printed after you submitted the form
Related
I have a form tag in which I have an input name and on click of a button, the value is displayed in table form. Now I want to update that value. How do I do it? This code does not have any database.
<form>
<input type="text" name="fname" value="" />
<input type="submit" value="submit" name="btn_submit" />
</form>
<br>
<br>
<table border="1">
<th>Name</th>
<th>Delete</th>
<th>Update</th>
<?php
session_start();
$na = array();
if (isset($_GET['btn_submit']))
{
if (isset($_SESSION['name']))
{
$na = $_SESSION['name'];
}
$na[] = $_GET['fname'];
$_SESSION['name'] = $na;
for ($i = 0; $i < count($na); $i++) {
?>
<tr>
<td><?php echo $na[$i]; ?></td>
<td>DELETE</td>
<?php
if(isset($_GET['del']))
{
}
?>
<td>UPDATE</td>
</tr>
<?php
}
}
?>
I fetch total_amount value and insert into another table but i want to insert total_amount in one field and seperated by ",". How to do that?
I know Mysqli is latest version . but here mysql is working properly.
<?php
include('database/db.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$sql1="Insert into test(`total_amount`) values ('{$_POST['i']}')";
$result1=mysql_query($sql1);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Page</title>
</head>
<body>
<form name="" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table>
<tr>
<td>Total Amount</td>
</tr>
<?php
$sql="Select * from tes1";
$result=mysql_query($sql);
while($dtset=mysql_fetch_array($result))
{
?>
<tr>
<td><input type="hidden" name="i" value="<?php echo $dtset['total_amount'];
?>"><?php echo $dtset['total_amount']; ?></td>
</tr>
<?php } ?>
</table>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<?php
$sql="Select * from tes1";
$result=mysql_query($sql);
// Empty array
$amounts = [];
while($dtset=mysql_fetch_array($result))
{
// Push the amounts to the array.
$amounts[] = $dtset['total_amount'];
}
// Implode will create a string from your array and seperates it with
// the chosen character
$combined = implode(",",$amounts);
?>
<tr>
<td>
<input type="hidden" name="i" value="<?php echo
$combined; ?>">
<?php echo $combined; ?>
</td>
</tr>
The while loop is now not part of the table row, but instead creates the array with all values first and then you can add the string created by implode to your hidden form field.
I have two column name varchar and area text
name area
abc 12a
dfg test
Now I want to update each of them from my page where I input some text to the textarea fetched from tow rows.
<?
if(isset($_POST['submit'])) {
$i = 0;
foreach($_POST['txt'] as $textarea) {
#$val[$i] = $val[$i].$textarea;
$i++;
}
foreach($val as $value){
$q= mysql_query("UPDATE table_name SET name = '$value' WHERE `area` = '??'");
echo "Success"; }
$sql="select * from table_name";
$res=mysql_query($sql);
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table border="1">
<thead>
<tr>
<th>NAME</th>
<th>AREA</th>
</tr>
<?php while($row=mysql_fetch_assoc($res))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><textarea rows="4" cols="40" name="txt[]"><?php echo $row['area']; ?> </textarea><br/>
</td>
</tr>
<tr>
</tr>
<?php } ?>
<tr>
<td><input type="submit" name="submit" id="submit" value="Update" /></td>
</tr>
</table>
</form>
I am getting the name but could not update/insert it in area for that name. How do I accomplish this?
the most easy fix is to take a hidden input field and pass the $row['name']
<input type="hidden" name="name[]" value="<?php echo $row['name'];?>"/>
then
if(isset($_POST['submit'])) {
$area=$_POST['txt'];
$name=$_POST['name'];
$count=count($area);
for($i=0; $i<$count; $i++)
{
if(mysql_query("UPDATE table_name SET area = '".$area[$i]."' WHERE `name` = '".$name[$i]."'"))
{
NB:-if u have a id column then take hidden input field with $row['id'] and do the same
You can embed actual keys in PHP's array-naming hack:
<textarea name="txt[foo]">...</textarea>
<textarea name="txt[bar]">...</textarea>
Which allows you to directly associate a particular form field with the value it represents in the database. In your case:
while($row = fetch_from_db()) {
output: <texarea name="txt[$row[id]]">$row[area]</textarea>
}
And then, after submitting:
foreach($_POST['txt'] as $key => $value) {
update database : UPDATE ... SET area=value WHERE id=$key
}
Note that your code is vulnerable to sql injection attacks.
I have a form with check box values i want to arrange the checkbox in two columns instead of
one single long column.
How can i split it to two columns ?
Here is the code :
<form id="form" name="form" method="post" action="">
<table width="502" border="0">
<tr>
<td>
Name :
<input type="textbox" name="rcv_group_name" value="">
<input type="hidden" name="add" value="add" />
</td>
</tr>
<tr>
<td align="left">
<strong>HEADING</strong>
<?php
$q = "select * from Config_RCV";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) > 0)
{
$k=0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM))
{
?>
<br> <input type="checkbox" name ="rcv_val[]" value ="<? echo $row[0];?>" /> <? echo $row[1];?>
<?
$k++;
}
}
?>
</td>
</tr>
<tr>
<td align="right"><input type="submit" name="Submit" id="Submit" value="Submit" style="background-color:#999" />
< /td>
<td height="26" align="left"> </td>
<td> </td>
</tr>
</table>
<form>
add another table and make 2 columns by splitting 2'nd result:
if (mysqli_num_rows($r) > 0) {
$k=0;
echo '<table><tr>';
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
?>
<td><input type="checkbox" name ="rcv_val[]" value ="<?php echo $row[0];?>" /> <?php echo $row[1];?> </td>
<?php
$k++;
if($k%2 == 0){
echo '</tr><tr>';
}
}
echo '</table>';
}
use your $k
if ($k%2==0){
echo "<br />";
}
instead of the lone now
Using tables like above will probably be better for looks
You could just add a second column to your table:
echo "<tr>";
$i = 0;
while ($row = mysqli_fetch_array($r,MYSQLI_NUM)) {
if ($i++%2==0) {
echo "</tr><tr>";
}
echo "<td>".$CELL_CONTENT_HERE."</td>";
}
echo "</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']))