I got a dillema, I'm trying to find a solution for my code. How do I make it so that when the user inputs a given quantity in the text box from the index.php it will transfer that input quantity to the invoice.php. I've tried doing the post method but it seems like it's not working :/ As always, any help or suggestions will be greatly appreciated! I hope it's something simple:( Here's my code:
This code holds my products array and also includes my text box.
//The following arrays contain our products and their information, one product per array.
$hulkhamburger = array('Product' => 'Hulk Hamburger', 'Description' => '...', 'Price' => '$1.00', 'Quantity' => "<input type='text' name='quantity1'>");
$atomichotdog = array('Product' => 'Atomic Hot Dog', 'Description' => '...', 'Price' => '$2.00', 'Quantity' => "<input type='text' name='quantity2'>");
$friedchicken = array('Product' => 'Fantastic 4 Fried Chicken', 'Description' => '...', 'Price' => '$3.00', 'Quantity' => "<input type='text' name='quantity3'>");
$psyonicpizza = array('Product' => 'Psyonic Pizza', 'Description' => '...', 'Price' => '$4.00', 'Quantity' => "<input type='text' name='quantity4'>");
$marvelmeatloaf = array('Product' => 'Marvel Meatloaf', 'Description' => '...', 'Price' => '$5.00', 'Quantity' => "<input type='text' name='quantity5'>");
//The following array takes our previous five arrays and puts them into one array for easier coding and reading.
$allfood = array ($hulkhamburger, $atomichotdog, $friedchicken, $psyonicpizza, $marvelmeatloaf);
?>
index.php
<html>
<style>
body{
background-image: url('URL HERE');
font-family: "Helvetica";
font-size:15px;
}
h1{
color:black;
text-align:center;
}
p{
font-size:15px;
}
</style>
<h1>
STORE TITLE HERE
</h1>
<body>
<form action="login.php" method="post">
<?php
//Include products info.inc (Which holds all our product arrays and info)
//Credit: Tracy & Mark (THank you!)
include 'products_info.inc';
/*The following code centers my table on the page, makes the table background white,
makes the table 50% of the browser window, gives it a border of 1 px,
gives a padding of 2 px between the cell border and content, and gives 1 px of spacing between cells.
*/
echo "<table align=center bgcolor='FFFFFF' width=50% border=1 cellpadding=1
cellspacing=2>";
//Credit: Tracy & Mark (THank you!)
echo '<th>Product</th> <th>Description</th> <th>Price</th> <th>Quantity</th>';
//The following code loops through the whole table body and then prints each row.
for($i=0; $i<count($allfood); $i++)
{
//Credit: Tracy & Mark (THank you!)
echo "<tr align=center>";
echo "<td>{$allfood[$i]['Product']}</td>";
echo "<td>{$allfood[$i]['Description']}</td>";
echo "<td>{$allfood[$i]['Price']}</td>";
echo "<td>{$allfood[$i]['Quantity']}</td>";
echo "</tr>";
}
//This code ends the table.
echo "</table>";
echo "<br>";
?>
<br><center><input type='submit' name='purchase' value='Purchase'></center>
</form>
</body>
</html>
And here's my invoice.php
<html>
<style>
body{
background-image: url('URL HERE');
font-family: "Helvetica";
font-size:15px;
}
h1{
color:black;
text-align:center;
}
p{
font-size:15px;
}
</style>
<h1>
Invoice
</h1>
</html>
<?php
//Include products info.inc (Which holds all our product arrays and info)
//Credit: Tracy & Mark (Thank you!)
include 'products_info.inc';
//Display the invoice & 'WELCOME USER. THANK YOU FOR USING THIS DAMN THING msg'
/*The following code centers my invoice table on the page, makes the table background white,
makes the table 50% of the browser window, gives it a border of 1 px,
gives a padding of 2 px between the cell border and content, and gives 1 px of spacing between cells.
*/
echo "<table align=center bgcolor='FFFFFF' width=50% border=1 cellpadding=1cellspacing=2>";
echo "<tr>";
echo "<td align=center><b>Product</b></td>";
echo "<td align=center><b>Quantity</b></td>";
echo "<td align=center><b>Price</></td>";
echo "<td align=center><b>Extended Price</b></td>";
echo "</tr>";
for($i=0; $i<count($allfood); $i++)
{
//Credit: Tracy & Mark (Thank you!)
$qty= #$_POST['Quantity']['$i'];
// This calculates the price if the user orders more than 1 item.
$extendedprice = $qty*$allfood[$i]['Price'];
echo "<tr>";
echo "<td align=center>{$allfood[$i]['Product']}</td>";
echo "<td align=center>$extendedprice</td>";
echo "<td align=center>{$allfood[$i]['Price']}</td>";
echo "</tr>";
}
// The goal here was to make it so that if the user selected a certain quantity from index.php, it would carry over and display on the invoice.php
if ($qty = 0)
{
echo "please choose 1";
}
elseif ($qty > 0)
{
echo $qty;
}
/*echo "<tr>";
echo "<td align=center>Subtotal</b></td>";
echo "<td align=center></td>";
echo "<td align=center></td>";
echo "<tr>";
echo "<td align=center>Tax at 5.75%</td>";
echo "<td align=center></td>";
echo "<td align=center></td>";
echo "<tr>";
echo "<td align=center><b>Grand total</b></td>";
echo "<td align=center></td>";
echo "<td align=center></td>";
*/
echo "</table>";
?>
<br>
<center>
<form action="index.php" method="post">
<input type="submit" name="home" value="Back to homepage">
</form>
</center>
A form is like a container which handles transfer of data. The action attribute tells where your data will be sent.
Here you got
<form action="login.php" method="post">
Which means the form data will be sent to login.php page. Not what you want i think.
Then, in the target page (defined by action attribute), you can get your parameters values in $_GET or $_POST arrays, depending of the method attribute of your form.
I don't see any textbox in the code you gave us. If you add something like
<input type="text" name="my_textbox"/>
You will be able to get the value of the textbox in your target page by using
$_POST['my_textbox']
Hope this helps.
BTW, i notices some mistakes into your code :
index.php :
Style should be in a <head>...</head> markup
You should not have html output like your <h1> outside of your <body>...</body>
Tables headers (<th>) should be in a <tr> as <td> are.
invoice.php :
$allfood is not declared
Better than $qty= #$_POST['Quantity']['$i'] (# removes errors), test if your key exists (if isset($_POST['Quantity']) && isset($_POST['Quantity'][$i]) $qty = $_POST['Quantity'][$i];
if ($qty = 0) should be if ($qty == 0) : = is affectation operator, == is comparison operator
same thing for html, all your output should be in <body>...</body> markups, and styles should be in an html header (<head>...</head>)
In index.php, your form action is "login.php", not invoice.php, so it will not pass anything to the latter.
Also, in index.php, there is no textbox named "quantity", so $_POST['quantity] will have nothing.
As in index.php inside form you have created table. There is no form elements like text box, hidden field. Form does not post table values hence you are not getting any posted value in invoice.php.
Related
I'm loading data from an xml file into a html table then adding a check box column at the end with the view of saving the data from each row where the check box is checked, for now I just want to display the rows with the check box checked.
In my first page I have the code below which successfully loads the xml data in to the html table and displays it as I wanted, forgive my code, it is a bit messy as I'm new to PHP.
<?php
<form method="POST" action="saved.php">
echo "<input type='submit' name='save' value='Save Checked Rows' />";
$all = simplexml_load_file('list.xml');
echo "<div style='height:200px; overflow-y: scroll;'>";
echo "<table border=\"1\">";
echo "<th>Title</th><th>Description</th><th>Link</th><th>Save</th>\n";
echo "<tr>";
$c=0;
foreach ($all as $current) {
$title=$current->title;
$description=$current->description;
$link=$current->link;
echo "<td>{$title}</td><td>{$description}</td><td><a href=$link>$link</a></td><td><input type='checkbox' name='save[]' value='$c' /></td>";
$c++;
echo "</tr>\n"; }
echo "</table>";
echo "</div>";
echo "</form>"
?>
On my second page I have the code below which displays the value of every check box that is checked when I submit the first page
<?php
Session_start();
?>
<?php
foreach($_POST['save'] as $key){
echo $key;}
}
?>
I need to get the data from the other table cells on the checked rows, the only idea that worked so far was putting an input in to the cell but I don't want that because the cells have to be uneditable. Thanks for the help !
For that there is an atribute in html which you can use i,e read only
I am using the book "PHP and SQL for dummies 4th edition" to learn web programming. My PetCatalog interface is meant to retrieve and display all petTypes from my Database. It is retrieving the pets in the database but it is doing so without the radio buttons. I do not know where the error is from because everything seems to be in place in the code.
Note: Everything works perfectly except that the radio buttons symbol are not being displayed. PS: There need to be a fictional database for this program to make any sense to anyone trying to help because the database is in my system. Thanks.
<?php
/* Program: PetCatalog.php
* Desc: Displays a list of pet categories from the
* PetType table. Includes descriptions.
* Displays radio buttons for user to check.
*/
?>
<html>
<head><title>Pet Types</title></head>
<body>
<?php
$user="root";
$host="localhost";
$password="";
$database="PetCatalog";
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
/* Select all categories from PetType table */
$query = "SELECT * FROM PetType ORDER BY petType";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");
/* Display text before form */
echo "<div style='margin-left: .1in'>\n
<h1 style='text-align: center'>Pet Catalog</h1>\n
<h2 style='text-align: center'>The following animal
friends are waiting for you.</h2>\n
<p style='text-align: center'>Find just what you want
and hurry in to the store to pick up your
new friend.</p>
<h3>Which pet are you interested in?</h3>\n";
/* Create form containing selection list */
echo "<form action='ShowPets.php' method='POST'>\n";
echo "<table cellpadding='5' border='1'>";
$counter=1;
while($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'\n";
echo "<input type='radio' name='interest' value='$petType'\n";
if( $counter == 1 )
{
echo "checked='checked'";
}
echo ">$petType</td>";
echo "<td>$typeDescription</td></tr>";
$counter++;
}
echo "</table>";
echo "<p><input type='submit' value='Select Pet Type'>
</form></p>\n";
?>
</div>
</body>
</html>
The first thing that jumps out at me is the fact that your line with
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'\n";
does not close the <td> tag. This could interfere with the creation of your <input> tags.
Try
echo "<tr><td valign='top' width='15%'
style='font-weight: bold;
font-size:1.2em'>\n";
and see if that works.
Like justathoughtor2 said close the table data and table row </td></tr> i have tested your without closing the table data and table row
after closing the table data and table row
Current Grid View structure with View Anchor - I want to view a specific row after clicking on View anchor, data should be displayed in popup - javascript
Below is my code. I have already implemented functionality for PHP Grid View, Delete option is implemented at the top with querystring
Now what i want is, After clicking view, it should display javascript popup with all the details of that specific row, and close option
The part which am not getting is
how to transfer data from php/mysql to javaScript and display it in popup
`
if(isset($_GET['id'])){
$id = $_GET['id'];
//$x = 'confirm("Are you sure you want to delete this product")';
//echo $x;
mysql_query("DELETE FROM users WHERE id = '$id'");
//echo "alert('Row Deletion Successful')";
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Table Display</title>
<style>
table, td, th
{
border:1px solid green;
}
th
{
background-color:green;
color:white;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<?php $result = mysql_query("SELECT id, CONCAT(title, ' ', name) as FullName, email, mobile FROM users") or die(mysql_error());
$row_count = 1;
$row = mysql_fetch_assoc($result);
echo '<td><input type="checkbox" /></td>';
echo "<th> Sr. No </th>";
foreach($row as $col=>$value)
{
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>EDIT</th>
</tr>
</thead>
<tbody>
<?php
mysql_data_seek($result, 0);
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo '<td><input type="checkbox" /></td>';
echo "<td>" . $row_count ."</td>";
foreach($row as $key=>$value)
{
echo "<td>";
echo $row[$key];
echo "</td>";
}
$row_count++;
?>
<td>
VIEW |
DELETE |
EDIT
</td>
<?php
echo "</tr>";
}
echo "</table>";
?>
</tbody>
</table>
</body>
</html>`
you can use this code to display in your records in popup window
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>your code to display in table format</p>");
myWindow.focus();
}
<a onclick="openWin();">Edit/Delete/View(any one)</a>
try this out in document.write() method enter your code inside a table tag
render specific view in a page (e.g. viewpageaddress.php?id=7)
and then get it's content in js by:
$.get('viewpageaddress.php?id=7', function(cnt){/* show cnt in your popup */})
Currently I have a script that displays the data which is editable and can update the database. I have tried to enter row counts and nothing seem to work. I really like the script to make 3 columns (10 rows per column), please help.
$sql = "SELECT id, pounds FROM price_list ORDER BY id";
$i = 0;
$result = mysql_query($sql);
echo "<form name='prices' method='post' action='updateA.php'>";
while($rows = mysql_fetch_array($result))
{
echo "<body bgColor='#5F5F6B'>";
echo "<table><table border=2 cellspacing=0 cellpadding=1>";
echo "<input type='hidden' name='id[$i]' value='{$rows['id']}' >";
echo "<td><font color='#FFFFFF'><font size='2'>DAYS {$rows['id']}: </font><font size='2'><font color='#000000'>PRICE:<input type='text' size='1' name='pounds[$i]' value='{$rows['pounds']}' ></tr>";
++$i;
}
echo "</table>";
echo "<input type='submit' value='Update Prices Band A' />";
echo "</form>";
?>
The above is the original code.
I don't really know what you're trying to do, but this code will generate a list of all the entries in the database with the ability to change them. Note that you'll have to remake your update_a.php file:
<style>
body {
background:#5F5F6B;
color:#fff;
}
</style>
<?php
$result = mysql_query("SELECT id, pounds FROM price_list ORDER BY id");
if (!$sql || mysql_num_rows($result)==0)
echo "Price list is empty";
else {
echo '<form name="prices" method="GET" action="update_a.php">'; // Change your filename!
$i = 0;
while ($rows = mysql_fetch_array($result)) {
echo 'Day '.$rows['id'].' costs ';
echo '<input type="text" name="'.$rows['id'].'" value="'.$rows['pounds'].'"/> pounds'
echo '<br/>'
$i++;
}
echo '<input type="submit" value="Update Prices Band A"/>';
echo "</form>";
}
?>
First of all many thanks to Leonard Pauli, the code worked perfectly in displaying the data but, it wouldn't update the database using my update.php. Below is the revised code and screenshot of what I was trying to archive.
Screenshot of single lined data displayed in 3 columns
<style>
body {
background:#5F5F6B;
color:#fff;
width:800px;
height:550px;
border:2px solid #bbb;
padding:20px;
float:center;
}
input[type="text"] {
width: 30px;
}
.table {
width:180px;
margin:1px;
border:2px solid #bbb;
padding:10px;
float:left;
}
.header {
width:595px;
margin:1px;
border:2px solid #bbb;
padding:10px;
float:left;
}
</style>
<div class="header"><b>Price List for dates from <font color ="yellow"><?php echo "$SPA"; ?> to <?php echo "$EPA"; ?></font></div>
<?php
$dataprice = $_POST['database'];
$datesrange = $_POST['id'];
$result = mysql_query("SELECT id, pounds FROM $dataprice ORDER BY id");
echo '<form name="prices" method="POST" action="update.php">';
$i = 0;
while ($rows = mysql_fetch_array($result)) {
echo '<div class="table">Day <font color="yellow">'.$rows['id'].' </font> costs ';
echo "<input type='hidden' name='id[$i]' value='{$rows['id']}' >";
echo "<input type='text' name='pounds[$i]' value='{$rows['pounds']}' > Pounds";
echo '<br/></div>';
$i++;
}
echo "<input type='hidden' name='databases' value='$dataprice'>";
echo '<center><input type="submit" value="Update Prices"/>';
echo '<center><font color="yellow"><br><br><br>IF UPDATING PRICE BAND D, ONLY ENTER THE VALUE OF WHICH
PRICES YOU WANT TO INCREASE BY, <br>EXAMPLE: 7 DAYS, IF CURRENT PRICE IS 30, IF YOU WANT TO
CHARGE 34, ONLY ENTER 4 AND LEAVE EVERYTHING ELSE SET TO 0</b></center>';
echo "</form>";
?>
A bit of an idiot really, completely forgot about CSS styling.
I am currently working on a project which pulls in tables from a mySQL which are shown in a html table. Below you can see the code.
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 border=1 width=100%>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>"."<center>".$row[0]."</center>"."</td>";
echo "<td>" .$row[2]."</td>";
echo "<td>" .$row[1]."</td>";
echo "<td>" .$row[3]."</td>";
echo "</tr>";
}
echo "</table>";
}
I need to change it to
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=1 border=1 width=100%>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>"."<center>".$row[0]."</center>"."</td>";
echo "<td>" .$row[2]."</td>";
echo "<td>" .$row[1]."</td>";
echo "<td><font face="Georgia, Times New Roman, Times, serif">" .$row[3]."</font></td>";
echo "</tr>";
}
echo "</table>";
}
When I do this is throws back errors. I am doing this so the user sees a barcode which i have a font for.
Am I doing this wrong? or is there another method which will work better.
Thanks
Ryan
Change " to ' for face:
echo "<td><font face='Georgia, Times New Roman, Times, serif'>" .$row[3]."</font></td>";
Also instead of inline font you should use css class or id. (check this for example of css class Applying css to a php table)
Try
echo "<td><font face='Georgia, Times New Roman, Times, serif'>" .$row[3]."</font></td>";
OMG.. don't use font tag. It's deprecated since HTML 4.01 and not available in HTML 5. The error comes from not escaping properly.
echo "<td><font face=\"Georgia, 'Times New Roman', Times, serif\">" ...
But please use a class and do the styling with CSS.
echo "<td class=\"font-georgia\">...
And in css:
.font-georgia {
font-family: Georgia, 'Times New Roman', Times, serif;
}
Note the quotes for 'Times New Roman' because it has spaces.
The same for center tag. Use text-align:center CSS instead.