Jump to content

Recommended Posts

Hi guys,

I'm having a hard time displaying the quantity the user selects in my index.php to reflect that same exact quantity in my invoice.php page.  Please help!  Any suggestions will be greatly appreciated:) 

 

my product information is stored in a file called products_info.inc and looks something like this:

$hulkhamburger = array('Product' => 'Hulk Hamburger', 'Description' => '<img src="hamburger.jpg" "height=100" "width=100">', 'Price' => '$1.00', 'Quantity' => "<input type='text' name='quantity[]'>");
$atomichotdog = array('Product' => 'Atomic Hot Dog', 'Description' => '<img src="hotdog.jpg" "height=100" "width=100">', 'Price' => '$2.00', 'Quantity' => "<input type='text' name='quantity2[]>");
$friedchicken = array('Product' => 'Fantastic 4 Fried Chicken', 'Description' => '<img src="friedchicken.jpg" "height=100" "width=100">', 'Price' => '$3.00', 'Quantity' => "<input type='text' name='quantity[]'>");
$psyonicpizza = array('Product' => 'Psyonic Pizza', 'Description' => '<img src="pizza.jpg" "height=100" "width=100">', 'Price' => '$4.00', 'Quantity' => "<input type='text' name='quantity[]'>");
$marvelmeatloaf = array('Product' => 'Marvel Meatloaf', 'Description' => '<img src="meatloaf.jpg" "height=100" "width=100">', 'Price' => '$5.00', 'Quantity' => "<input type='text' name='quantity[]'>");

//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);

Now my index.php looks like this:

<?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 the 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>";
            ?>

The problem arises when I enter any quantity in my index after pressing submit, it goes to this following invoice.php and does not display the quantity that was inputed from the index :(

<?php
//Include products info.inc (Which holds all our product arrays and info)
//Credit: Tracy & Mark (Thank you!)
require '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)
{
    $quantityErr = "<BR><span style='color:red'>Please go back and input a quantity.</spand></BR>";
}
 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>";

?>

I bolded the items that might be of question, but then again, I'm new to PHP:(  Thank you in advance folks!

Edited by phpnoob808

Don't supress errors:

 

$qty= @$_POST['quantity']['$i']; //<- you are supressing errors by using @.

 

You would then find out that your $_POST array is NOT set, because you need a form for that.  Add a form to index.php so that you can get the input values back to the page.

Don't supress errors:

$qty= @$_POST['quantity']['$i']; //<- you are supressing errors by using @.

You would then find out that your $_POST array is NOT set, because you need a form for that.  Add a form to index.php so that you can get the input values back to the page.

Hi jcbones,

thank you for the response, indeed there was an error after the inputing a quantity and it said:

Notice: Undefined index: quantity in C:\xampp\htdocs\assignment_2\invoice.php on line 47

Notice: Undefined index: quantity in C:\xampp\htdocs\assignment_2\invoice.php on line 47

Notice: Undefined index: quantity in C:\xampp\htdocs\assignment_2\invoice.php on line 47

Notice: Undefined index: quantity in C:\xampp\htdocs\assignment_2\invoice.php on line 47

Notice: Undefined index: quantity in C:\xampp\htdocs\assignment_2\invoice.php on line 47

and about the form, I don't know if it's because of this that's hindering the inputs too, but if you look at my products_info.inc code.  The form is within the array.  I did this because the form would line up nicely with the rest of the products in a table, and I'm really horrible at finding out how to line it up without the use of the array.

 

you can find the code at the very top of my post.  looks something like this:

$hulkhamburger = array('Product' => 'Hulk Hamburger', 'Description' => '<img src="hamburger.jpg" "height=100" "width=100">', 'Price' => '$1.00', 'Quantity' => "<input type='text' name='quantity[]'>");

the posted code only has the form field defined, not an actual form (an opening form tag with method and action attributes and a closing form tag.)

I see, I'll go back to the drawing board and come back, thanks guys!!!

Still getting the same errors, code looked like this

$hulkhamburger = array('Product' => 'Hulk Hamburger', 'Description' => '<img src="hamburger.jpg" "height=100" "width=100">', 'Price' => '$1.00', 'Quantity' => "<form name='quantity' method='post'><input type='text' name='quantity[]'></form>");

a form without a submit button is going to be a little hard to submit.

 

i recommend that you slow down and define what output you want to produce. what would the html be if you hand coded it for a couple of products without using php? then once you have that information, use php to dynamically produce the output from your products stored in the array.

 

also, i recommend that your file being included ends in a .php extension so that if you were doing this for real and someone learned of the file name they could not see the entire contents of it by browsing to it.

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.