HTML PHP check box - php

How can I style my code so that the check box can be aligned beside the table.
All of the check boxes are appearing above the table, but not beside it.
Consider the following snippet of code as well as the picture attached at the end.
<html>
<head>
<title> Furni checker </title>
</head>
<body>
<h1>Furniture Lookup / Furniture Checker</h1> <br />
<br /><p>This tool is used for removal or check furniture on a user account.</p> <br />
<h2> Instructions : </h2> <br />
<p> Lookup User ID and Lookup for the Furniture! </p>
<?php
require_once "global.php";
echo '<h1>Furniture Lookup / Furniture Checker</h1>
<br /><p>This tool is used for removal or check furniture on a user account.</p>';
echo '<br />
<form method="post">
Username:
<input type="text" name="user">
<input type="submit" value="Lookup">
</form>';
if (isset($_POST['user'])) {
$user = filter($_POST['user']);
$getUser = dbquery("SELECT * FROM users WHERE users.username = '" . $user . "' ");
while($show1 = mysql_fetch_array($getUser)) {
echo ''. $show1['id']. '';
}
}
?>
<br />
<?php
echo '
<form method="post">
ID:
<input type="text" name="id">
<input type="submit" value="Lookup">
</form>';
if (isset($_POST['id'])) {
$id = filter($_POST['id']);
$getFurniture = dbquery("SELECT public_name FROM furniture JOIN items ON items.base_item = furniture.id WHERE items.user_id = '" . $id . "' ORDER BY public_name ASC ");
echo '<table border=1 cellpadding=1 width="10%">';
while($show = mysql_fetch_array($getFurniture)) {
//echo ''. $show['public_name'].'' ;
echo '<tr class="t_row">';
echo '<input type="checkbox"> <td>' .$show['public_name']. '</td>';
echo '</tr>';
}
echo '</input>';
echo '</table>';
}
?>
Image:

Try this:
<?php
if (isset($_POST['submit']))
{
$id = filter($_POST['id']);
$getFurniture = dbquery("SELECT public_name FROM furniture JOIN items ON items.base_item = furniture.id WHERE items.user_id = '" . $id . "' ORDER BY public_name ASC ");
echo '<table border=1 cellpadding=1 width="10%">';
while($show = mysql_fetch_array($getFurniture)){
//echo ''. $show['public_name'].'' ;
echo '<tr class="t_row">';
echo '<td>' .$show['public_name']. '</td><td><input type="checkbox" /> </td>';
echo '</tr>';
}
echo '</table>';
}
?>
Let me know if it's not working.

Related

using form textfield name from dynamic table outside whileloop

I got another problem with my Code.
I generate a dynamic table from SQL-content and use textfields in the table to, maybe someday, change the content.
The problems is, I cann't access the textfields from outside the whileloop to save the content, all I get is Undefined index error for every field.
<form method="POST" enctype="text/html">
<?php
require_once ('config.php');
$sql = " SELECT * FROM kassen ORDER BY name ASC ";
$db_erg = mysql_query( $sql );
echo "<tr>";
echo "<td>";
echo '<table border="1" width="80%" align="center">';
echo "<tr> <th>Name</th><th>Stand</th><th>Verbrauch</th><th>Einzahlungen</th></tr>";
while ($zeile = mysql_fetch_assoc($db_erg))
{
echo '<tr>';
echo '<td>'. $zeile['name'] . '</td>';
echo '<td><center>'. $zeile['bier_stand'] . '€</td>';
echo '<td>';
/* in the text below, i set the name to verbrauch"'.$zeile['id'] and
ergebnis"'.$zeile['id'] which should generate a new unique name for every
single text*/
echo '<center><input type="text" name="verbrauch"'.$zeile['id'].' value="0" size="10" />';
echo '</td>';
echo '<td>';
echo '<center><input type="text" name="einzahlung"'.$zeile['id'].' value="0" size="10" />';
echo '</td>';
echo '</tr>';
} echo '</table>';
?>
<center><input type="hidden" name="aktion" value="speichern" />
<center><input type="Submit" name="" value="speichern"/>
</form>
<?php
if (isset ($_POST['aktion']))
{
if ($_POST['aktion'] == "speichern" )
{
require_once ('config.php');
$sql = " SELECT * FROM kassen ORDER BY name ASC ";
$db_erg = mysql_query( $sql );
while ($zeile = mysql_fetch_assoc($db_erg))
{
$standalt = $zeile["bier_stand"];
/* now I try to put the value of the text to the DB, but all i get is
Undefined Index error */
$verbrauch = $_POST['verbrauch'.$zeile['id']];
$einzahlung = $_POST['einzahlung'.$zeile['id']];
$stand = $zeile["bier_stand"] - $verbrauch + $einzahlung;
$id = $zeile["id"];
$sql = "UPDATE kassen SET ";
$sql .= " bier_stand_alt = '$standalt', ";
$sql .= " bier_stand = '$stand', ";
$sql .= " bier_verbrauch = '$verbrauch', ";
$sql .= " bier_einzahlungen = '$einzahlung' ";
$sql .= " WHERE id='$id'";
}
echo '<h2>Änderungen übernommen</h2>';
echo 'zurück zur Bierkasse';
exit;
}
}
?>
Any idea what I'm messing up?
Sorry for questioning,
realy stupid mistake
echo '<center><input type="text" name="einzahlung"'.$zeile['id'].' value="0" size="10" />';
should have been
echo '<center><input type="text" name="einzahlung'.$zeile['id'].'" value="0" size="10" />';
so actually just the " was in wrong place ... stupid stuff that happens at 3am

Handling a form that is filled using a while loop assigning a check box to each div

Here is the code from my submit.php file that has the form mentioned in the title. The if and while statements work and all of this code works the way it looks.
<form action="file.php" method="post">
<?php //database connection stuff
...
if($result->num_rows > 0){
echo '<div class="pure-g">';
while($row = $result->fetch_assoc()){
echo '<div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
<fieldset id="' . $row[Name] . '">
<h3 class="content-subhead">'
. $row[Name] . '
</h3>
<img src="' . $row[Picture] . '" width="100" height="100">
<input type="checkbox" name="check" id="check' . $row[Name] . '" value="' . $row[Name] . ',">
</fieldset>
</div>';
}
echo '</div>';
}
else{
echo "0 results";
}
?>
</div>
<input type="submit" value="Add Selected Games">
</form>
The problem I'm getting is when I use the submit.php:
$conn = mysql_connect($host, $name, $pass);
if(!$conn){
echo "cannot connect to server" . mysql_error();
}
$select = mysql_select_db($db);
if(!$select){
echo "cannot select db" . mysql_error();
}
$list = $_REQUEST['check'];
echo " success $list";
There are currently 3 checkboxes showing on the file.php page that will list the fieldset objects. How should I name the checkbox to have it echo the right $row[Name]? It will echo one $row[Name] based on the a checkbox being checked and newest checkbox (created later in the while loop).
When you just want to see an unformatted list or an array you must convert the array to text.
$list = var_export($_REQUEST,false);
echo "sucess $list";
The checkbox value should "1"
The checkbox name should include the identifier
<input type="checkbox" name="check' .$row[Name] . '" id="check' .$row[Name] . '" value="1">
When the form is submitted you get the check value like this:
$list = "<ul>\n";
foreach $_POST as $key => $val){
if (substr($key,0,5)=='check'){
$list .= "\n<li>" . substr(($key,5);
}
}
echo "sucess\n$list\n</ul>";
The Browser only posts the check boxes that are checked. Some Browsers do not pass the Value. The value that is passed with the checkbox key name varies from Browser to Browser.
I also cleaned up the form routine:
<?php
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=50, max=100');
header('Cache-Control: max-age=120');
echo <<<EOT
<!DOCTYPE HTML>
<html lang="en"><head><title>Title</title><style type="text/css"></style></head><body><div>
<form action="file.php" method="post">
EOT;
if($result->num_rows > 0){
echo '<div class="pure-g">';
while($row = $result->fetch_assoc()){
$name=$row[`Name`];
$pic = $row[`Picture`];
echo <<<EOT
<div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
<fieldset id="$name">
<h3 class="content-subhead">$name</h3>
<img src="$pic" width="100" height="100">
<input type="checkbox" name="check$name" id="check$name" value="1">
/fieldset>
</div>
EOT;
}
echo '</div>';
}
else{
echo '<p>0 results</p>';
}
echo '</div><input type="submit" value="Add Selected Games"></form></body></html>';
?>

Create checkboxes in php that correspond to MySQL data

I have a php script that pulls information out from my database.
$result = mysqli_query($con,"SELECT * FROM menuitem WHERE id='$id' LIMIT 1");
if (!$result) {
printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
exit();
}
//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="150"></tr>
<tr>
<td width="30% valign="top" align="center"><img style="border: #66cc33 5px solid;" src=" ' .$picturepath . '" height="200" width="200" border="1"/></td>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br /><font>Description:</font><br /> ' . $description . ' <br /><br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="keywords" id="keywords" value=" '. $keywords .'"/>
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' "/>
<input type="submit" name="button" id="button" value="Add to Order"/>
</form>
</td>
</tr>
<tr align="center"><td>View Your Order</td></tr>
</table>';
echo $dynamiclist;
}
mysqli_close($con); //close the db connection
?>
And then I have another page where I want checkboxes created for each keyword listed. I'm not sure if I have my db colum set up correctly, but I have a column set up with multiple keywords: first, second, fifty, none, and more.
I've tried creating this, but it only displays actual "checkboxes" with no text next to any of them.
<?php
require("database.php"); //connect to the database
if(isset($_POST['keywords'])){
$pid = $_POST['keywords'];
}
$result = mysqli_query($con,"SELECT * FROM menuitem ");
if (!$result) {
printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
exit();
}
//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
$option = '';
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist ="<input type='checkbox' name='checkbox' value='". $keywords ."'>";
echo $dynamiclist;
}
The goal is to have a user select an item from the first page, and on the second page, list several checkbox options for that specific item. Any help would be greatly appreciated.
If you want text next to the checkbox, you have to put it in the HTML. And if the column contains a comma-separated list, you need to split it up.
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword ";
}
Try this:
<?php
$keywords = explode(',', $keywords);
if(!empty($keywords))
{
foreach($keywords as $keyword) {
$keyword = trim($keyword);
echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword ";
}
}
?>
Thanks!

Saving data from a checkbox using PHP and MySQL

Here is my code:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="20"></tr>
<tr>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' - "checkbox"/>';
echo $dynamiclist;
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword <br /><br />";
}
echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection
The code pulls in an item using the items id, and then creates checkboxes and keywords for that item. A user is to click on the checkbox options they want and then submit the form which sends the information to the shopping cart. The problem is that when the form is submitted, only the name and price of the item is sent over, but not the checkbox options that were clicked. Can you help me identify why? Side note. In my database I have a column "keywords" where multiple keywords are stored. // medium, well, rare, milk, apple juice, fries. Also, on the cart.php the I have:
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
which posts the data from the script using the checkboxes. I'm just stuck and can't get any further.
You should have different name for your checkbox:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="20"></tr>
<tr>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' - "checkbox"/>';
echo $dynamiclist;
$i=0;
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
$chkname = "checkbox{$i}";
$i = $i+1;
echo "<input type='checkbox' name='$chkname' value='$keyword'>$keyword <br /><br />";
}
echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection
You need to get checkbox data in the cart.php like this:
if($_REQUEST['checkbox0']) echo $_REQUEST['checkbox0'];
if($_REQUEST['checkbox1']) echo $_REQUEST['checkbox1'];
//so on foreach checkbox
Your checkboxes need to have different names, otherwise the values are overwritten:
<input type="checkbox" name="keyword1" value="keyword1">Keyword1
<input type="checkbox" name="keyword2" value="keyword2">Keyword2
<input type="checkbox" name="keyword3" value="keyword3">Keyword3
You then access them in PHP like so:
if ( isset($_GET['keyword1']) ) {
}
if ( isset($_GET['keyword2']) ) {
}
if ( isset($_GET['keyword3']) ) {
}
A more logical way to manage the data is to pass the checkbox values as an array:
<input type="checkbox" name="keyword[]" value="keyword1">Keyword1
<input type="checkbox" name="keyword[]" value="keyword2">Keyword2
<input type="checkbox" name="keyword[]" value="keyword3">Keyword3
It then becomes much easier to work with the data in PHP;
if ( isset($_GET['keyword']) && is_array($_GET['keyword']) ) {
foreach ( $_GET['keyword'] as $v ) {
}
}

How to use arrays for form input and update respective database records

I'm new to PHP... I want to know how to populate a form from mySQL. I'm stumped at the input section where I am trying to allow the user to select choices for radio button /checkbox for each record. thanks in advance for anyone's help!
TC
Here's a snippet of my code:
<?php
$mQuantity = 1;
$con = mysql_connect("localhost","t","c");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("tc", $con);
/*
if request.form("itemname")<>" " then
cSQLAcct = "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%' ORDER BY FOODTYPE, ITEM"
else
cSQLAcct = "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' ORDER BY FOODTYPE, ITEM"
end if
*/
// retrieve form data
$input = $_POST['itemname'];
echo $_POST['ITEM'];
$mItem = $_POST['ITEM'];
$mPrice = $_POST['PRICE'];
$mQuantity = $_POST['QUANTITY'];
$mOrderTotal = 0;
$mSide0 = '1';
$mOil = '1';
$mStarch = '1';
$mSalt = '1';
// use it
echo "You searched for: <i>$input</i>";
echo $mSessionID;
mysql_query("INSERT INTO OrderQueue (SessionID,item,quantity,price,no_oil,no_starch,no_salt,side0) VALUES ('$mSessionID','$mItem','$mQuantity','$mPrice','$mOil','$mStarch','$mSalt','$mSide0')");
/*
$sql="SELECT * FROM Restaurant_Menus
WHERE
Item like '%$input%'";
*/
$sql="SELECT * FROM OrderQueue
WHERE
SessionID = '$mSessionID'";
$result = mysql_query($sql);
//('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
//echo $input;
//echo $sql;
/*
if ($input!=" "){
$result = mysql_query($sql);
}
// cSQLAcct = "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%' ORDER BY
else
{ $result = mysql_query("SELECT * FROM Restaurant_Menus");
}
*/
$c=1;
$class[1] = 'odd';
$class[2] = '';
$array_no_oil = array();
$array_no_salt = array();
$array_no_starch = array();
$array_rice = array();
echo "<table border='1' width=500px>
<tr>
<th></th>
<th align=left>Item</th>
<th align=right>Price</th>
<th align=right>Quantity</th>
</tr>";
//<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">
while($row = mysql_fetch_array($result))
{
//echo '<tr class="'.$class[$c].'" onMouseOver='#F3EB49' onMouseOut ='#DDDDDD' bgcolor="#DDDDDD" >';
//echo "<tr class='odd'>";
//echo '<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">'
?>
<TR onMouseover="this.bgColor='#FFC000'"onMouseout="this.bgColor='#DDDDDD'">
<?php
/*
<form action="Orders.asp" method="post" target="_top" name="LogonForm">
<td><font size = 2>
<!--<%= mQuantity %>-->
<INPUT TYPE="TEXT" NAME="QUAN" VALUE="1" SIZE=2>
</font></td>
<td width=50px><font size = 2>
<INPUT TYPE=HIDDEN NAME=ITEM VALUE="<% =mItem %>">
<INPUT TYPE=HIDDEN NAME=PRICE VALUE=<% =mPrice %>>
<INPUT TYPE=HIDDEN NAME=RID VALUE= <% =mRestaurant_ID %>>
<INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
<input id="Choices" class="findit" type="submit" value ="Order" />
</form>
*/
?>
<?php
// Obtain list of images from directory
//$img = getRandomFromArray($imgList);
}
?>
<?php
if ($row['Picture']!=" "){
echo "<td><a><img src='images/".$row['Picture'].".JPG' height=50px></a></td>";
}
else{
echo "<td></td>";
}
echo "<td width=200><b>" . $row['Item'] . "</b><br>
<input class='dropwidth' type='radio' name='$array_rice' value='1' selected>White Rice<br>
<input type='radio' name='$array_rice' value='2'>Pork Fried Rice<br>
<input type='radio' name='$array_rice' value='3'>Brown Rice<br>
<input type='checkbox' name='$array_no_oil' value='1' />No Oil
<input type='checkbox' name='starch' value='no oil' />No Starch
<input type='checkbox' name='salt' value='no salt' />No Salt
</td>";
echo "<td width=50 align=right>" . number_format($row['Price'],2) . "</td>";
$mQuantity = "'" . number_format($row['Quantity'],0) . "'";
$mPrice = "'" . number_format($row['Price'],2) . "'";
$mLineItemTotal = $row['Quantity'] * $row['Price'];
$mOrderTotal = (number_format($mOrderTotal,2) + number_format($mLineItemTotal,2));
echo $mOrderTotal;
$mLineItemTotal2 = "'". number_format($mLineItemTotal,2) . "'";
//echo "<td>" . $mQuantity. "</td>";
?>
<form action="orders.php" method="post" target="_top" name="LogonForm">
<td width="50" align=right><font size = 2>
<!--<%= mQuantity %>-->
<!--<INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php $mQuantity; ?>>-->
<INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php echo $mQuantity.";" ?>/>
</font></td>
<?php echo "<td width=50 align=right>" . $mLineItemTotal . "</td>";?>
<!--<td width=50px><font size = 2>-->
<!--<INPUT TYPE="TEXT" NAME="LINEITEMTOTAL" VALUE=<?php echo $mLineItemTotal.";" ?> WIDTH=10/>-->
<INPUT TYPE=HIDDEN NAME=ITEM VALUE=<?php $mItem ?> />
<INPUT TYPE=HIDDEN NAME=PRICE VALUE=<?php $mPrice ?>/>
<INPUT TYPE=HIDDEN NAME=RID VALUE=<?php $mRestaurant_ID ?>/>
<INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
<!--<input id="Choices" class="findit" type="submit" value ="Order" />-->
</form>
<?php
echo "</tr>";
if($c==2) $c=0;
$c++;
}
echo "</table>";
echo "<div>".$mOrderTotal."</div>";
mysql_close($con);
?>
You should name your HTML form elements as such: MyElement[] when using them as arrays. For example:
<form method="post" name="myForm" action="myForm.php">
<select multiple name="myFormSelectMultiple[]">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
In this select multiple example, when the first two items are selected and then posted this form would produce similar output to the following:
array('myFormSelectMultiple' => array(1,2));
Hope this helps!

Categories