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
}
}
?>
Related
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
My problem is a submit button renders under each row of table data. I have searched all over the web to find out, how I can code to use only one submit for the entire form. As it is I can only submit $_POST for one row whereas I need to submit the entire form for processing.
Here is my Code:
<?php
require("config.inc.php");
session_start();
$usrname = $_POST["uname"]; //variable passed from validateuser.html
global $usrname;
//echo $usrname;
// initial query
$query = "SELECT * FROM dfd_usr_profiles WHERE username= '".$usrname."'";
/*$query2 = "UPDATE dfd_usr_profiles SET filters = :fltrs,
regions = :regns
WHERE $usrname = :username"; */
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$rows = $stmt->fetchAll();
if ($rows) {
$response["success"] = 1;
$response["message"] = "Profile Information Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["userID"] = $row["userID"];
$post["username"] = $row["username"];
$post["filters"] = $row["filters"];
$post["regions"] = $row["regions"];
//update our repsonse JSON data
array_push($response["posts"], $post);
$endi = count($post);
//echo $endi;
?>
<!DOCTYPE html>
<html>
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<body>
<form name="updatefilters" action="update_action.php" method="post" enctype="multipart/form-data>" id="update">
<!-- <input type="submit" name="submit" id="update" value="Update" /> -->
<fieldset>
<table border="1" >
<legend>Update Filters and Regions</legend>
<tr>
<td><input type="checkbox" name="pID[]" value="<?php echo $post['userID']; ?>" onclick="getRow(this)" /></td>
<td><input type="input" name="uname" value="<?php echo $usrname;?>" /></td>
<td><?php echo $post['filters']; ?></td>
<td><label valign="top" for="" id="mfiltervals">Select Deal Type(s) you want:</label></p></td>
<td><?php require("dtypelist.php");?></td>
<td><?php echo $post['regions']; ?></td>
<td><label valign="top" for="" id="mregionvals">Select Region(s) you want:</label></td>
<td><?php require("regionslist.php");?></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
<?php
echo '<button type="submit" name="submitupdate" form="update">Update</button>';
}
// echoing JSON response
// echo json_encode($response); // Commented out: Displays profile unformatted data. TC 070516.
} else {
$response["success"] = 0;
$response["message"] = "No information for this Username is available!";
die(json_encode($response));
}
// session_start(); place-holder
?>
Please help.
You need to move the foreach and the submit button that you have in the code below. All the other code is not relevant for this change. I have marked the 3 parts that need to be moved with // **** Move:
// **** Move this block to just before the `<tr>` tag
foreach ($rows as $row) {
$post = array();
$post["userID"] = $row["userID"];
$post["username"] = $row["username"];
$post["filters"] = $row["filters"];
$post["regions"] = $row["regions"];
//update our repsonse JSON data
array_push($response["posts"], $post);
$endi = count($post);
//echo $endi;
?>
<!DOCTYPE html>
<html>
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<body>
<form name="updatefilters" action="update_action.php" method="post" enctype="multipart/form-data>" id="update">
<!-- <input type="submit" name="submit" id="update" value="Update" /> -->
<fieldset>
<table border="1" >
<legend>Update Filters and Regions</legend>
<tr>
<td><input type="checkbox" name="pID[]" value="<?php echo $post['userID']; ?>" onclick="getRow(this)" /></td>
<td><input type="input" name="uname" value="<?php echo $usrname;?>" /></td>
<td><?php echo $post['filters']; ?></td>
<td><label valign="top" for="" id="mfiltervals">Select Deal Type(s) you want:</label></p></td>
<td><?php require("dtypelist.php");?></td>
<td><?php echo $post['regions']; ?></td>
<td><label valign="top" for="" id="mregionvals">Select Region(s) you want:</label></td>
<td><?php require("regionslist.php");?></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
<?php // **** Move this up to just after the `</table>` tag
echo '<button type="submit" name="submitupdate" form="update">Update</button>';
// **** Move this closing brace just after the `</tr>` tag
}
After the 3 moves, it looks as follows -- see comments with ****MOVED****:
?>
<!DOCTYPE html>
<html>
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<body>
<form name="updatefilters" action="update_action.php" method="post" enctype="multipart/form-data>" id="update">
<!-- <input type="submit" name="submit" id="update" value="Update" /> -->
<fieldset>
<table border="1" >
<legend>Update Filters and Regions</legend>
<?php // ****MOVED****
foreach ($rows as $row) {
$post = array();
$post["userID"] = $row["userID"];
$post["username"] = $row["username"];
$post["filters"] = $row["filters"];
$post["regions"] = $row["regions"];
//update our repsonse JSON data
array_push($response["posts"], $post);
$endi = count($post);
//echo $endi;
?>
<tr>
<td><input type="checkbox" name="pID[]" value="<?php echo $post['userID']; ?>" onclick="getRow(this)" /></td>
<td><input type="input" name="uname" value="<?php echo $usrname;?>" /></td>
<td><?php echo $post['filters']; ?></td>
<td><label valign="top" for="" id="mfiltervals">Select Deal Type(s) you want:</label></p></td>
<td><?php require("dtypelist.php");?></td>
<td><?php echo $post['regions']; ?></td>
<td><label valign="top" for="" id="mregionvals">Select Region(s) you want:</label></td>
<td><?php require("regionslist.php");?></td>
</tr>
<?php // ****MOVED****
}
?>
</table>
<?php // ****MOVED****
echo '<button type="submit" name="submitupdate" form="update">Update</button>';
?>
</fieldset>
</form>
</body>
</html>
<?php
Make sure to add <?php and ?> where necessary to switch correctly between PHP and normal output.
I need help on how to retain the entered values in the text fields where user entered the value after submit. I'm having difficulty trying to figure out how to retain the values, because when I click on the submit button, the page refreshes and then values gone, and I have to retype them again.
Below is my form:
<?php $count_name = count($x); ?>
<table>
<thead>
<tr>
<th colspan="<?php echo $count_name; ?>"><strong>MR</strong></th>
<th colspan="<?php echo $count_name; ?>"><strong>MS</strong></th>
</tr>
</thead>
<tbody>
<?php $_college = mysql_query("SELECT * FROM college");
if(mysql_num_rows($_college)) {
$i=0;
while($row_college=mysql_fetch_array($_college)) { ?>
<tr>
<?php for($j=0;$j<$count_name;$j++) { ?>
<td>
<input type="text" name="mr<?php echo $j; ?>[]" value=""/>
<td>
<?php } for($k=0;$k<$count_name;$k++) { ?>
<td>
<input type="text" name="ms<?php echo $k; ?>[]" value=""/>
<td>
<?php } ?>
</tr>
<?php } $i++;} ?>
</tbody>
<table>
<input type="hidden" value="<?php echo $count_name; ?>" name="totrows"/>
<input type="submit" value="Submit" name="submit"/>
Here's my code if button submit is click
<?php
if(isse($_POST['submit'])) {
$y = $_POST['totrows'];
$count_totcriteria = $y;
for($ab=0;$ab<$count_totcriteria;$ab++) {
$mr = 'mr_'.$ab;
$ms = 'ms_'.$ab;
$mr_score = $_POST[$mr];
$ms_score = $_POST[$mr];
foreach($mr_score as $key1 => $val1) {
if(is_numeric($val1) && !empty($val1)) {
$mr_val[] = $val1;
} else {
$msg = 'All fields are required and must be a valid score';
}
}
foreach($ms_score as $key2 => $val2) {
if(is_numeric($val2) && !empty($val2)) {
$ms_str[] = $val2;
} else {
$msg = 'All fields are required and must be a valid score';
}
}
}
}
I know I have to put some code in the 'value=""' in order to display back the entered values when form is submitted but I am not sure what code to use. Not sure how to catch each array values.
I think instead of
<input type="text" name="mr<?php echo $j; ?>[]" value=""/>
you are looking for something like this (assuming $i is your new outer loop)
<input type="text" name="mr_<?= $j ?>[<?= $i ?>]" value="<?= #$_POST['mr_'.$j][$i] ?>"/>
and the same change for the ms line.
Does that work?
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']))
I have this piece of PHP/HTML, wrapped in a POST form. How do I pass only the ID for the row where the delete button is clicked back to the server?
<table>
<tr>
<th>File Path</th>
<th>Expiration</th>
</tr>
<?php
$result = mysql_query ($list_query);
$row_count = mysql_numrows ($result);
for ($i = 0; $i < $row_count; $i++) {
$id = mysql_result ($result, $i, "id");
$path = mysql_result ($result, $i, "path");
$expiration = mysql_result ($result, $i, "expires");
?>
<tr>
<td width="60%">
<?php echo $path; ?>
</td>
<td>
<?php echo $expiration; ?>
</td>
<td>
<input type="submit" value="Delete Expiration" />
</td>
</tr>
<?php
}
?>
</table>
Use a hidden field within the form.
<input type="hidden" name="id" value="<?php echo $id ?>">
You should also start a new form for each new row too.
for ($i = 0; $i < $row_count; $i++) {
$id = mysql_result ($result, $i, "id");
$path = mysql_result ($result, $i, "path");
$expiration = mysql_result ($result, $i, "expires");
?>
<tr>
<td width="60%">
<?php echo $path; ?>
</td>
<td>
<?php echo $expiration; ?>
</td>
<td>
<form method="POST" action="?">
<input type="hidden" name="id" value="<?php echo $id ?>">
<input type="submit" value="Delete Expiration" />
</form>
</td>
</tr>
<?php
}
?>
I agree with the solution Extrakun proposes, however, for completeness I would like to point you to the option of using JavaScript and DOM. You could use JQuery as explained in this question:
jquery + table row edit - String problem
You should set a hidden field containing the id somewhere in your row, and wrap the hidden field and the submit button in a form per row:
<form>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" value="Delete" />
</form>
After clicking, get the id with $_POST['id'].
Greetz,
XpertEase