Appending HTML form value with updates PHP variable - php

I am wondering if there is a way I can pass an updated PHP counting variable to an HTML form before submission. My PHP reads in a CSV file and generates an HTML table based on the number of rows in the CSV file. The counting variable then reflects the number of rows in the HTML table. Is there anyway I can update the HTML form "request form" with the PHP variable $formvar ?
<?php
$csvFile = $_POST['myfile'];
$formvar =1;
$row = 0;
echo "<form id=\"requestform\" action=\"picklist-submit.php\" method=\"post\">";
echo "<table>";
echo "<input value=\"Submit\" type=\"submit\">";
echo "<input type=\"hidden\" name=\"formvar\" value=\"$formvar\">";
echo "<td>ISBN</td>";
echo "<td>Quantity</td>";
echo "<td>Comments</td>";
echo "<td>Initials</td>";
echo "</tr>";
if (($handle = fopen($csvFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
$var = $data[0];
$var2 = $data[1];
$var3 = $data[2];
$var4 = $data[3];
}
echo "<tr>";
echo "<td><input type=\"text\" name=\"upc1\" value=\"$var\"></td>";
echo "<td><input type=\"text\" name=\"quantity1\" value=\"$var2\"></td>";
echo "<td><input type=\"text\" name=\"comment1\" value=\"$var3\"></td>";
echo "<td><input type=\"text\" name=\"initials1\" value=\"$var4\"></td>";
$formvar++;
}
echo "</tr></table>";
echo "</form>";
fclose($handle);
}
?>

After echo "</tr></table>";, add:
echo "<input type=\"hidden\" name=\"formvar\" value=\"$formvar\">";
Then when your form is submitted, you can retrieve the value from $_POST['formvar']

You can add a field outside of the loop.
$formvar++;
}
echo "<input type=\"hidden\" name=\"formvar\" value=\"".$formvar."\" />";
echo "</tr></table>";
You'll also want to remove the hidden field above the loop.

Related

PHP dynamic table

I have to make a dynamic table n*n , the user first gives the number n and the program makes a 5*5 table with check box this part I have make it, the second part is the user checks same of the checkbox and clicks on submit and the program makes again a table 5*5 but in the place of check box which checks is colored. I have uploaded and image.
Sorry for my bad English, thanks for your time.
enter image description here
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Give value">
</form>
<?php
$rows = $cols = $name = "";
if(isset($_GET['subject']))
$rows = $cols = $_GET['subject'];
if(isset($_POST['check_list']))
$name = $_POST['check_list'];
if(isset($_GET['subject'])){
echo "<form action='my.php' method='post'>";
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td><input type='checkbox' name='check_list[]' value='value ".$td."'></td>";
}
echo "<tr>";
}
echo "</table>";
echo "<input type='submit' />
</form>";
}
// this part of code is not make the third excecution the number 3 image
echo $cols;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
foreach($_POST['check_list'] as $value){
if($tr == $value[td])echo "<td bgcolor='#FF0000'></td>";
else
echo "<td> </td>";
}
echo "</tr>";
}
echo "</table>";
?>

Get values of radio selected values of entire row to next file

I have displayed the values from mysql table along with radio buttons with different values. On submit button it should redirect to next page and display the selected values. My code on first file is as follows :
$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";
$count = 0;
while($row = $result1->fetch_row())
{
echo "<tr>";
echo "<td width=".'10'.">";
$count = $count + 1;
#$sr=$sr+1;
echo "<input type='radio' name='select' value='".$count."' />";
#$row = mysqli_fetch_row($result1);
#echo "<tr>";
echo "<td width=".'10'.">";
$rooms = $row[0];
$srrooms = $rooms;
echo "<input type='text' name='srrooms' value='".$srrooms."' hidden='hidden'>";
#echo $srrooms;
echo $rooms;
echo "</td>";
echo "<td width=".'10'.">";
$no_roomss = $row[1];
$nosrrooms = $no_roomss;
echo "<input type='text' name='nosrrooms' value='".$nosrrooms."' hidden='hidden'>";
echo $no_roomss;
echo "</td>";
echo "<td width=".'10'.">";
$no_dayss = $row[2];
$nodays = $no_dayss;
echo "<input type='text' name='nodays' value='".$nodays."' hidden='hidden'>";
echo $no_dayss;
echo "</td>";
echo "<td width=".'10'.">";
$name = $row[3];
echo $name;
echo "</td>";
echo "</tr>";
#$count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";
The javascript code is :
$(function(){
$('input[name="select"]').change(function(){
$('#count').val($("input[name='select']:checked").val());
});
});
edit.php code is :
<?php
if(isset($_POST['submitRooms'])){
#$count = $_POST['count'];
#$srrooms=$_POST['srrooms'.$count];
#$nosrrooms=$_POST['nosrrooms'.$count];
#$nodays=$_POST['nodays'.$count];
echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>
You can use counter for submitting the values of the selected radiobutton's row as:
<?php
$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";
$count = 0; // counter variable
while($row = $result1->fetch_row())
{
echo "<tr>";
echo "<td width=".'10'.">";
echo "<input type='radio' name='select' value='".$count."' />";
echo "<td width=".'10'.">";
$rooms = $row[0];
$srrooms = $rooms;
echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >"; // add counter value to every name of the input fields
echo $rooms;
echo "</td>";
echo "<td width=".'10'.">";
$no_roomss = $row[1];
$nosrrooms = $no_roomss;
echo "<input type='hidden' name='nosrrooms".$count."' value='".$nosrrooms."'>";
echo $no_roomss;
echo "</td>";
echo "<td width=".'10'.">";
$no_dayss = $row[2];
$nodays = $no_dayss;
echo "<input type='hidden' name='nodays".$count."' value='".$nodays."' >";
echo $no_dayss;
echo "</td>";
echo "<td width=".'10'.">";
$name = $row[3];
echo $name;
echo "</td>";
echo "</tr>";
$count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";
?>
and then use jquery for saving the active radiobuttons count in the hidden count input field [after the above php code]
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type='text/javascript'>
$(function(){
$('input[name="select"]').change(function(){
$('#count').val($("input[name='select']:checked").val());
});
});
</script>
then on your edit.php fetch the posted data as:
<?php
if(isset($_POST['submitRooms'])){
#$count = $_POST['count'];
#$srrooms = $_POST['srrooms'.$count];
#$nosrrooms = $_POST['nosrrooms'.$count];
#$nodays = $_POST['nodays'.$count];
echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>
You didn't have added the whole form code so please add the above mentioned form code and the reason why your code is not working is that you haven't added the count with the input field names. And their is no use of fetching the posted data with count in the edit.php till you don't have the input fields named with the $count variable
echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >";

Multiplying $_POST

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.

display quantity field in table in php

I'm trying to get my table to look like this
http://gyazo.com/6a134b723c30e97fa99559158cde4b1e
but when I use my code looks like this
http://gyazo.com/1c08f83e744b3a20c99b55eee5313045
I can't get the quantity field or add field to work for the life of me; the quantity field is just blank. I would greatly appreciate some help because I can't get it to work.
<?php require_once("include/db_connect.php"); ?>
<html>
<head><title>Displaying Image files using PHP</title></head>
<body>
<h1>Displaying Images from an Image table using PHP</h1>
<?php
$db_link = db_connect("project");
// Retrieve table properties
$fields = mysql_list_fields("project", "bookstore");
$num_columns = mysql_num_fields($fields);
// Make a simple database query to select all columns and rows
$query = "SELECT * FROM bookstore";
$result = mysql_query($query) or die("SQL query failed");
// Display results as an HTML table. Note how mysql_field name
// uses the $fields object to extract the column names
echo '<table border="1" cellpadding = "5" >';
// Display the column names
echo "<tr>";
for ($i = 0; $i < $num_columns; $i++)
{
echo "<th>", mysql_field_name($fields, $i), "</th>";
}
echo "<th>"Quantity "</th>";
echo "</tr>";
// Loop over the rows of the table.
// $row contains the information for each row
// Refer to the names of the fields in the table
// Must ahow the path where the image files are held
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>
<form action='somethingToHandleForm.php' method='post'>";
echo "<td>". $row['isbn']. "</td>";
echo "<td>". $row['title']."</td>";
echo "<td>". $row['author']."</td>";
echo "<td>". $row['pub']."</td>";
echo "<td>". $row['year']."</td>";
echo "<td>". $row['price']."</td>";
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
// Free the resources and close the connection
mysql_free_result($result);
mysql_close($db_link);
?>
</body>
</html>
One thing I noticed is you're missing a single quote in this line after value=add:
<input type='submit' value='add>
You have some formatting problems in your HTML, which look to be the source of your problems when rendering the table. Specifically the lines:
echo "<td><input type='text' name='quantity' value=".$row['quantity']."/></td>";
echo "<td><input type='submit' value='add></td>";
The value attribute for the first line is not properly enclosed in quotes, and there is no end single-quote for the value add in the second line (nor are you closing the input tag), try the following:
echo "<td><input type='text' name='quantity' value='".$row['quantity']."' /></td>";
echo "<td><input type='submit' value='add' /></td>";

Restore radio button selection when revisiting a page

I have a PHP script that allows users to enter grades by selecting a radio button that corresponds to the students grade. It allows them to view the selected grades before they can be finally submitted. I also want the page to have the ability to go back to the selection page and remember the radio buttons that were selected so that the user doesn't have to set all of them again when going back. Here is what I have coded so far, it takes the user back to selection page but doesn't restore the radio button selection.
<?php
session_start();
$script_name = $_SERVER["PHP_SELF"];
if(!isset($_SESSION["course"]) || !isset($_SESSION["course"])) {
$_SESSION["course"] = $_POST["coursename"];
$_SESSION["section"] = $_POST["section"];
}
if(($_SESSION["authenticated"] == true || isset($_POST["back"])) && !isset($_POST["continue"])) {
$course = $_SESSION["course"];
$section = $_SESSION["section"];
$file_name = $course.$section.".txt";
$_SESSION["filename"] = $file_name;
// Open file containing student names.
$fp = fopen($_SESSION["filename"], "r") or die("Could not open file");
$students = array();
$i = 0;
echo "<h2>Grades Submission Form</h2>";
echo "<h2>Course: $course, Section: $section</h2>";
echo "<form action=\"$script_name\" method='post'>";
echo "<table border='1'>";
while (!feof($fp)) {
$line = trim(fgets($fp));
$students[$i++] = $line;
echo "<tr><td>$line</td>";
echo "<td><input type='radio' name=\"$line\" value='A'/>A</td>";
echo "<td><input type='radio' name=\"$line\" value='B'/>B</td>";
echo "<td><input type='radio' name=\"$line\" value='C'/>C</td>";
echo "<td><input type='radio' name=\"$line\" value='D'/>D</td>";
echo "<td><input type='radio' name=\"$line\" value='F'/>F</td>";
echo "</tr>";
}
echo "</table><br>";
echo "<input type='submit' name='continue'/>";
echo "</form>";
} elseif($_SESSION["authenticated"] == true && isset($_POST["continue"]) && !isset($_POST["back"])) {
unset($_POST["continue"]);
$keys = array_keys($_POST);
$values = array_values($_POST);
echo "<h2>Grades to Submit</h2>";
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Grade</th></tr>";
for($i = 0; $i < count($keys); $i++) {
echo "<tr><td>{$keys[$i]}</td><td>{$values[$i]}</td></tr>";
}
echo "</table><br>";
echo "<form action='confirmation.php' method='post'>";
echo "<input type='submit' value='Submit Grades'/>";
echo "</form>";
echo "<form action=\"$script_name\" method='post'>";
echo "<input type='submit' value='Back'/>";
echo "</form>";
} else {
header("Location: main.php");
}
?>
You could serialize() an array containing the radio button states and store it in your session. When you go back, all you have to do is unserialize it and set the data again.

Categories