PHP get $list to display each value in seperate columns - php

What I need to do is get the values in $list to display in separate columns and for the titles of the columns to be inside the table on its own row.
if (isset($_POST['check_list'][0])) {
// form was submitted, checkbox was checked
?>
<table>
<table border='1'>
<tr>
<th>Artist</th>
<th>Composer</th>
<th>Genre</th>
<th>Title</th>
<th>Album</th>
<th>Label</th>
<th>Price</th>
<th>Description</th>
</tr>
<tr>
<?php
foreach ($_POST['check_list'] as $item) {
echo '<td>' . $item . '</td>'; // here we add the table row and fill there all data from $getColumn array, each one has own table cell
}
echo '</table>';
} else if (isset($_POST['submit'])) {
// form was submitted, checkbox wasn't checked
echo 'Form was submitted and checked wasn\'t checked';
}
?>
</table>
database page:
print '<input type="hidden" name="checkbox1" value="'. $getColumn[1].'" />';
print '<input type="hidden" name="checkbox2" value="'. $getColumn[2].'" />';
print '<input type="hidden" name="checkbox3" value="'. $getColumn[3].'" />';
print '<input type="hidden" name="checkbox4" value="'. $getColumn[4].'" />';
print '<input type="hidden" name="checkbox5" value="'. $getColumn[5].'" />';
print '<input type="hidden" name="checkbox6" value="'. $getColumn[6].'" />';
print '<input type="hidden" name="checkbox7" value="'. $getColumn[7].'" />';
print '<input type="hidden" name="checkbox8" value="'. $getColumn[8].'" />';
print '<input type="hidden" name="checkbox8" value="'. $getColumn[9].'" />';
print '<td><input type="checkbox" name="check_list[]"value="'. $getColumn[0].'"</td>';
connection:
$conn = pg_connect("host=**** port=****
dbname=teaching user=csguest password=****);
$res = pg_query ($conn, "SELECT ref, artist, composer, genre, title, album, label, price, description FROM music");
print "<table border='1'>";
print "<th>Check box</th><th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th><th>Price</th><th>Description</th></tr>";
while($getColumn = pg_fetch_row($res))

Okay, regarding your comments below I prepare you example with PHP and HTML too.
You can try to send the form. If you check the checkbox, table with values is rendered, in the other case you receve message that checkbox wasn't checked. It's just for you to understand how it works.
<?php
// this array you have rendered and filled elsewhere, it's just for this exaple
$getColumn = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
if (isset($_POST['check_list'][0])) {
// form was submitted, checkbox was checked
?>
<table>
<tr>
<th>Artist</th>
<th>Composer</th>
<th>Genre</th>
<th>Title</th>
<th>Album</th>
<th>Label</th>
<th>Price</th>
<th>Description</th>
<th>Last one</th><!-- in your question you have only these 8 THs, in form you have 9 values. In this case it's just to have the same THs and TDs -->
</tr>
<tr>
<?php
foreach ($_POST['check_list'] as $item) {
echo '<td>' . $item . '</td>'; // here we add the table row and fill there all data from $getColumn array, each one has own table cell
}
echo '</table>';
} else if (isset($_POST['submit'])) {
// form was submitted, checkbox wasn't checked
echo 'Form was submitted and checked wasn\'t checked';
}
// and the simply form for testing
echo '<form method="post">';
echo '<input type="checkbox" name="check_list[0]" value="' . $getColumn[0] . '">';
echo '<input type="hidden" name="check_list[1]" value="' . $getColumn[1] . '">';
echo '<input type="hidden" name="check_list[2]" value="' . $getColumn[2] . '">';
echo '<input type="hidden" name="check_list[3]" value="' . $getColumn[3] . '">';
echo '<input type="hidden" name="check_list[4]" value="' . $getColumn[4] . '">';
echo '<input type="hidden" name="check_list[5]" value="' . $getColumn[5] . '">';
echo '<input type="hidden" name="check_list[6]" value="' . $getColumn[6] . '">';
echo '<input type="hidden" name="check_list[7]" value="' . $getColumn[7] . '">';
echo '<input type="hidden" name="check_list[8]" value="' . $getColumn[8] . '">';
echo '<input type="submit" name="submit">';
echo '</form>';

Related

submit ID using multiple checkboxes in PHP HTML Mysql

I am using multiple HTML checkboxes to submit student attendance. The checkbox once clicked submit '1' and if not checked, will submit '0'. I am able to submit '1' when checked but cant submit '0'. Below is my code:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
foreach ( $student as $attendance ) {
echo "<tr>";
echo "<td>$attendance->id</td>";
echo "<td>$attendance->name</td>";
echo "<td>
$attendance->classroom_id
</td>";?>
<input type="hidden" name="date[]" value="<?php echo $date;?>" />
<td><input type="checkbox" name="status[]" value="<?php echo $attendance->id;?>"></td>
<td><input type="text" name="reason[]" ></td>
<?php
}
?>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td>
This might give you some ideas:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
$out = '<table id="tblAttendance"><thead><th>ID</th><th>Name</th><th>Room</th><th>Status</th><th>Reason</th></thead><tbody>';
foreach ( $student as $attendance ) {
$out .= '<tr>';
$out .= '<td>' .$attendance->id. '<input type="hidden" name="studentID[]" value="' .$attendance->id. '"></td>';
$out .= '<td>' .$attendance->name. '<input type="hidden" name="name[]" value="' .$attendance->name. '"></td>';
$out .= '<td>' .$attendance->classroom_id. '<input type="hidden" name="classroomID[]" value="' .$attendance->classroom_id. '"></td>';
$out .= '<td><input type="checkbox" name="status[]" value="yes"></td>';
$out .= '<td><input type="text" name="reason[]" ></td>';
$out .= '</tr>';
}
$out .= '<tr><td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td></tr>';
$out .= '</tbody></table>';
$out .= '<input type="hidden" name="date" value="' .$date. '" />';
echo $out;
?>
Since you are using a type="submit" button, I presume you have this inside a form construct?
Recall how checkboxes work in HTML forms: if the box is checked, the value received on the PHP side will be the value="yes" value. In other words, the variable $_POST['status'][n] will have the value yes.
HOWEVER, if the checkbox is not set, then $_POST['status'][n] will not be set.
Reference:
http://www.html-form-guide.com/php-form/php-form-checkbox.html

Why is this variable not being posted and giving an error?

Why is bakeryid variable not being posted in my form? The error I am receiving is
"Notice:Undefined variable bakeryid"
I have two pages, one displays the form and the second is the action for the form. The second form kept saying that it was undefined also. The bakeryid is the ID for each cakes order.
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes");
$bakeryid = $_POST['bakeryid'];
?>
<table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['order'] . "</td>";
echo '<td><input type="hidden" name="bakeryid" value="' . $bakeryid . '"/></td>';
echo '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
echo "</tr>";
}
echo "</form>";
echo "</table>";
Answer:
echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>';
You set $bakeryid with $_POST['bakeryid'], but then define bakeryid with $bakeryid.
Please try:
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes c INNER JOIN members m ON c.memid = m.memid");
$bakeryid = $_POST['bakeryid']; // this line is unnecessary
?> <table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['order'] . "</td>";
echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>'; // this line changed
echo '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
echo "</tr>";
}
echo "</form>";
$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes c INNER JOIN members m ON c.memid = m.memid");
$bakeryid = $_POST['bakeryid']; ?>
<table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo '<tr>
<td>' . $row['firstname'] . '</td>
<td>' . $row['order'] . '</td>
<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>
<td><input type="hidden" name="memid" value="' . $memid . '"/>
<input type="submit" name="takeorder" value="Take Order" >
</td>
</tr>';
}
echo '</form></table>';
The reason bakeryid isn't appearing in the form, is when setting the fields value you are using $bakeryid which is set to a post that hasn't happened yet. You want to set the value to $row['bakeryid'] as above.

PHP displays tags in different order

I have following problem, I have list of products in a database and want to display them in table unfortunately it plays some tricks for me because it displays one td before table even begins.
Here are the PHP functions:
<?php
function displayProduct($code,$url)
{
echo '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
echo '<input type="hidden" name="return_url" value="' . $url . '" />';
echo '<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
}
function displayItem($obj,$url)
{
echo '<tr>';
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>' . displayProduct($obj->code,$url) .'</td>';
echo '</tr>';
if(strlen($obj->description) > 2)
{
echo '<tr><td colspan="4" style="font-size: 10px;">' . $obj->description . '</td></tr>';
}
}
?>
And here is the HTML output that I get:
Could someone help me ?
The echo call from displayProduct happens before the echo call of displayItem occurs.
I can see two solutions.
1: displayProduct should return the things to write and not echo them.
2:
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>';
displayProduct($obj->code,$url);
echo '</td>';
displayProduct($code,$url) should return the string instead of printing it out:
function displayProduct($code,$url)
{
$result = '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
$result .='<input type="hidden" name="return_url" value="' . $url . '" />';
$result .='<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
return $result
}
[Edit] I should read better the questions...
But this still applies:
Also as Adrian stated, you should not echo the lines in "displayProducts", but return a string.

How to submit an HTML form into a PHP array

I've looked through some posts around here, but am still struggling to get my code to work correctly....
I've got a form that is being dynamically created so I never know how many rows it will have, the number of columns is static though. Here is my current code...
echo '<form name = "confirmPlayers" enctype="multipart/form-data" action="page.php" method="POST">';
echo '<input type="hidden" name="confirm" value="1"/>';
echo '<input type="hidden" name="pg_function" value="match"/>';
echo '<table border = "1">';
echo '<tr> <td colspan = "7"> <b>Matched Members</b></td> </tr>';
echo '<tr> <td> Match </td> <td>Division</td> <td>Number</td> <td>Upload Number</td> <td>KDGA Name</td> <td>Upload Name</td> <td>Score</td></tr>';
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[]" value="1" checked>';
echo '<td> ' . $row['division'] . ' </td>';
echo '<td> ' . $row['mplayer_number'] . ' </td>';
echo '<td> ' . $row['tplayer_number'] . ' </td>';
echo '<td> ' . $row['mfirst_name'] . ' ' . $row['mlast_name'] . ' </td>';
echo '<td> ' . $row['tfirst_name'] . ' ' . $row['tlast_name'] . ' </td>';
echo '<td> ' . $row['score'] . ' </td>';
echo "</tr>";
}
echo '<tr> <td>';
echo '<input type="submit" value="Submit" />';
echo '</td> </tr>';
echo '</table> </form>';
I've tried several variations on creating the array but just seem to be creating a mess. I basically need all of the information from each cell returned in order to pass on to the database for further processing. I was hoping to return an array with a row for each corresponding row in the generated table, and then keys/indexes for each column displayed. Right now if I use a print_r($_POST['match_chkBox']); then it is returning correctly with the number of rows I left checked before submitting, but any other changes.. not so pretty.
Would appreciate any help you can give... this is driving me nuts!
try giving value unique id
echo '<td> <input type="checkbox" name="match_chkBox[]" value=value=\"".$row['id']."\"checked>';
if i were you i will post $row[id] (as defined by angel) only and fetch respective data at my php code from the database.
but if you want to take all data with you you can simply do the following..
<pre>
$i=0;
while($row = mysql_fetch_array($result)){
$i++;
echo "<tr>";
echo '<td> <input type="checkbox" name="match_chkBox[$i]" value="'.$row['id'].'" checked>';
echo '<td> ' . $row['division'] . ' <input type="hidden" name="division['.$i.']"> </td>';
echo '<td> ' . $row['mplayer_number'] . ' <input type="hidden" name="mplayer_number['.$i.']"> </td>';
// and so on....
echo "</tr>";
}
</pre>
in since html form sent only checked checkbox to the server you can have the following code to check user selections
<pre>
foreach($_POST['match_chkBox'] as $k=>$value) {
$divi = division[$k];
$mplay = mplayer_number[$k];
//now insert into your database.
}
</pre>
hope this will help
Set the value of the checkbox to the unique key of that row, there by when the form is posted, match_chkBox[] will contain the unique keys.
echo"<input type='checkbox' name='match_chkBox[]' value=\"$row[unique_id]\" />";
Use view source to see if each rows value is set to the unique_id of that row

Values from dynamically produced checkboxes

I am trying to get the values of a dynamically created set of checkboxes in PHP but apparently I couldn't get it. The source codes are below.
The "managestaff.php" page would allow searching for staff via their names and throws out a list of names with checkboxes for the admin to check them and click on a "delete" button at the bottom to delete the staff whom are being checked.
The deletion would be done on "deletestaff.php" as the "delete" button on "managestaff.php" simply forwards these values to "deletestaff.php" to do deletion work of the staff.
"managestaff.php" page codes:
<b><h3>Manage Staff</h3></b><br/>
<form action="managestaff.php" method="POST">
<input name="form" type="hidden" id="form" value="true">
<table width=300>
<tr>
<td width=112>Staff Name: </td>
<td width=188><input type="text" class="textfield" name="sname" /><br/></td>
</tr>
</table><br/>
<input type="submit" value="submit" />
</form>
<?php
if (isset($_POST['form']) && (isset($_POST['sname'])) && $_POST['form'] == 'true') {
$space = ' ';
$staffname = mysql_real_escape_string($_POST['sname']);
$query = 'SELECT * from staff where staffname like \'%' . $staffname . '%\'';
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo '<br><br>';
echo '<table>';
echo '<tr><th>Staff ID' . $space . '</th><th>Staff Name' . $space . '</th></tr>';
echo '<form action="deletestaff.php" method="POST">';
echo '<input name="delstaffform" type="hidden">';
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $row['staffid'] . '</td><td>' . $row['staffname'] . '</td>';
// :Begin - dynamic checkbox generation for deleting staff
echo '<td>';
echo '<input type="checkbox" name="delstaff" value="' . $row['staffid'] . '" />';
echo '</td>';
// :End
echo '</tr>';
}
echo '<tr align="right"><td colspan="3"><input type="submit" value="delete"/></td></tr>';
echo '</form>';
echo '</table>';
}
}
?>
"deletestaff.php" page codes:
<?php
print_r('POST: ' . $_POST);
echo '<br>';
if (isset($_POST['delstaffform']) && isset($HTTP_POST_VARS)) {
echo 'Submission of delstaffform FOUND !';
echo 'Staff to delete' . $HTTP_POST_VARS['delstaff'];
}
else{
echo 'Submission of delstaffform NOT FOUND !';
}
?>
The "deletestaff.php" doesn't do delete for now as it's a test page.
The current output I get is "Submission of delstaffform NOT FOUND !".
Thanks for the solutions.
Try this:
<input type="checkbox" name="delstaff[]" value="' . $row['staffid'] . '"/>';
print_r your $_POST and you'll see it sticks your submissions nicely into an array for you.
<?php
if (isset($_POST['delstaff']) && is_array($_POST['delstaff'])) {
echo 'Submission of delstaffform FOUND !';
$array = $_POST["delstaff"];
foreach($array as $value){
echo "<br>Value: ".$value."<br>";
}
} else {
echo 'Submission of delstaffform NOT FOUND !';
}
?>
Found the answer on my own but nevertheless you are helpful :D . Thanks a lot.

Categories