collecting data from html template for php script

hello, my name is slade. i’m working with a shopping cart template that allows for the option to choose the product, choose the quantity, choose the earpiece. there are only three items that we offer so it seems like a pretty straight forward script. i was hoping to avoid sessions and use a php to convert the values of the options to variables then insert them into the mysql db. here is the back of the html code:

[code]











							  <th class="quantity">
										Quantity
								  </th>
									<th class="price">
										Price
									</th>
									<th class="subtotal">
										Subtotal
              </th>
									<th>&nbsp;
										
									</th>
								</tr>
							</thead>
						</table>
						<table class="table">
							<tbody>
								<tr class="warning">
									<td class="thumb" rowspan="2">
										<a href="javascript: void(0);" title="Link to product page">
											<img src="img/331.jpg" width="123" height="91" title="Product Image">
										</a>

Item Description

TEMPLE
The Carat
These Diamond Shape Spectacles Accentuate Anyone's Style and Grace.
MDL331
Size Buffalo Horn Gold Rose Gold White Gold Titanium Platinum Whale Bone
										<input type="hidden" id="prod2_color1" name="prod2_color1" value="no">
									</td>
							  <td class="quantity">
										<label class="inline">Qty:</label>
                                        <input id="prod2_qty1" name="prod2_qty1" type="text" class="number" value="1" class="inline input-mini">
								  </td>
									<td class="price">
										<span class="currency">$</span><span class="amount">3000.00</span>
									</td>
									<td class="subtotal">
										<span class="currency">$</span><span class="amount">3000.00</span>
									</td>
									<td class="remove">
                                        <button class="btn btn-danger btn-mini" type="submit" name="prod2_remove1" value="remove_item" rel="tooltip" data-placement="left" data-original-title="Be careful! This item will be entirely removed from the basket!">
                                            <i class="icon-remove icon-white"></i>
                                        </button>
									</td>
								</tr>
								<tr class="warning additional_empty">
									<td class="empty_td" colspan="8">
										
									</td>
								</tr>
						  </tbody>
				    </table>
                    
                    
                    <table class="table">
						  <tbody>
								<tr class="warning">
									<td class="thumb" rowspan="2">
										<a href="javascript: void(0);" title="Link to product page">
											<img src="img/332.jpg" width="123" height="91" title="Product Image">
										</a>
The Minimum

Timeless Design, A true work of art, High Quality Lenses, A Comfortable Fit

MDL332
Size Buffalo Horn Gold Rose Gold White Gold Titanium Platinum Whale Bone Qty: $4000.00 $4000.00
								  </td>
								</tr>
						  </tbody>
				    </table>
                    
                    
                    <table class="table">
							<tbody>
								<tr class="warning">
									<td class="thumb" rowspan="2">
										<a href="javascript: void(0);" title="Link to product page">
											<img src="img/333.jpg" width="123" height="91" title="Product Image">
										</a>
</td>
									<td class="description" rowspan="2">
										<a class="item-name" href="javascript: void(0);" title="Link to product page">The Maximum</a>
										<div>
											<div class="item-availability"><em>Rimless frames</em>, <em> Sophistication &amp;  Elegance</em> by Design</div>
											<div class="item-sku">MDL333</div>
										</div>
									</td>
									<td class="size">
                                        <select class="span2" id="prod2_size1" name="prod2_size1">
											<option value="0">Size</option>
											<option value="Buffalo Horn" selected>Buffalo Horn</option>
											<option value="Gold">Gold</option>
											<option value="Rose Gold">Rose Gold</option>
											<option value="White Gold">White Gold</option>
											<option value="Titanium">Titanium</option>
											<option value="Platinum">Platinum</option>
											<option value="Whale Bone">Whale Bone</option>
                                        </select>
									</td>
									<td class="color"><input type="hidden" id="prod2_color1" name="prod2_color1" value="no">
								  </td>
									<td class="quantity">
										<label class="inline">Qty:</label>
                                        <input id="prod2_qty1" name="prod2_qty1" type="text" class="number" value="1" class="inline input-mini">
									</td>
									<td class="price">
										<span class="currency">$</span><span class="amount">5000.00</span>
									</td>
									<td class="subtotal">
										<span class="currency">$</span><span class="amount">5000.00</span>
									</td>
									<td class="remove">
                                        <button class="btn btn-danger btn-mini" type="submit" name="prod2_remove1" value="remove_item" rel="tooltip" data-placement="left" data-original-title="Be careful! This item will be entirely removed from the basket!">
                                            <i class="icon-remove icon-white"></i>
                                        </button>
									</td>
								</tr>
								<tr class="warning additional_empty">
									<td class="empty_td" colspan="8">
										
								  </td>
								</tr>
						  </tbody>
				    </table>
                   
                                          

                   
                   
                    <table class="table">
					  <tbody>
						    <tr class="warning">
						      <td class="thumb" rowspan="2"><a href="javascript: void(0);" title="Link to product page"> <img src="img/334.jpg" alt="" width="123" height="91" title="Product Image"> </a></td>
						      <td class="description" rowspan="2"><a class="item-name" href="javascript: void(0);" title="Link to product page">The Drop</a>
						        <div>
						          <div class="item-availability"><em>These Teardrop Cut Lenses with Lightweight Rimless frames are a work of art</em></div>
						          <div class="item-sku">MDL334</div>
					            </div></td>
						      <td class="size"><select class="span2" id="prod2_size3" name="prod2_size3">
						        <option value="0">Size</option>
						        <option value="Buffalo Horn" selected>Buffalo Horn</option>
						        <option value="Gold">Gold</option>
						        <option value="Rose Gold">Rose Gold</option>
						        <option value="White Gold">White Gold</option>
						        <option value="Titanium">Titanium</option>
						        <option value="Platinum">Platinum</option>
						        <option value="Whale Bone">Whale Bone</option>
						        </select></td>
						      <td class="color"><input type="hidden" id="prod2_color3" name="prod2_color3" value="no"></td>
						      <td class="quantity"><label class="inline">Qty:</label>
						        <input id="prod2_qty1" name="prod2_qty1" type="text" class="number" value="1" class="inline input-mini"></td>
						      <td class="price"><span class="currency">$</span><span class="amount">6000.00</span></td>
						      <td class="subtotal"><span class="currency">$</span><span class="amount">6000.00</span></td>
						      <td class="remove"><button class="btn btn-danger btn-mini" type="submit" name="prod2_remove1" value="remove_item" rel="tooltip" data-placement="left" data-original-title="Be careful! This item will be entirely removed from the basket!"> <i class="icon-remove icon-white"></i> </button></td>
					        </tr>
						    <tr class="warning additional_empty">
						      <td class="empty_td" colspan="8"></td>
					        </tr>
				      </tbody>
					  </table>
					</div>
					<div class="empty_basket">
						Your Shopping Cart is empty!
					</div>
					<div class="order_blocks clearfix">
						<div class="row">
							<div class="span6 shipping_block clearfix">
								<div class="delivery_block pull-left">
									<h4>Delivery Method</h4>
									<label class="radio">
										<input type="radio" name="deliveryOption" id="deliveryOption1" value="0" checked>
										STANDARD - 4-5 working days: <span class="currency">$</span>0.00
									</label>
									<label class="radio">
										<input type="radio" name="deliveryOption" id="deliveryOption2" value="9.95">
										EXPRESS DELIVERY - 48h: <span class="currency">$</span>9.95
									</label>
								</div>
								<div class="promotional_block pull-right">
									<h4>Promotional Code</h4>
									<div class="input-append">
										<input class="span2" id="appendedInputButton" name="promotional" value="" size="16" type="text" >
										<button class="btn" type="submit" formaction="demo_admin.html" id="promotionAdd" rel="tooltip" data-placement="bottom" data-original-title="If you have a promotional code - you could add it here.">Add</button>
									</div>
									<h6>(Use any text here and you'll receive 10% of discount!)</h6>
								</div>
							</div>
							<div class="span6 total_block">
								<h4>Summary Block</h4>
								<table id="basketTotalsList">
									<tbody>
										<tr>
											<td class="total_position">Goods Subtotal</td>
											<td class="basket_subtotal">
												<span class="currency">$</span><span class="amount">325.67</span>
											</td>
										</tr>
										<tr>
											<td class="total_position">Taxes (<span class="taxes_percent">0</span>%)</td>
											<td class="basket_taxes">
												<span class="currency">$</span><span class="amount">325.67</span>
											</td>
										</tr>
										<tr>
											<td class="total_position">Delivery cost</td>
											<td class="basket_delivery">
												<span class="currency">$</span><span class="amount">325.67</span>
											</td>
										</tr>
										<tr>
											<td class="total_position">Discount (<span class="discount_percent">0</span>%)</td>
											<td class="basket_discount">
												-<span class="currency">$</span><span class="amount">325.67</span>
											</td>
										</tr>
										<tr class="total">
											<td class="total_position">
												<h3>Order Total</h3>
											</td>
											<td class="basket_total">
												<h3>
													<span class="currency">$</span><span class="amount">325.67</span>
												</h3>
											</td>
										</tr>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
				<div class="navbar">
					<div class="navbar-inner">
						<ul class="nav custom_nav">
							<li class="pull-left">
								<div>
									<a href="javascript: void(0);" class="btn btn-info">
										<i class="icon-hand-left icon-white"></i>
										Continue Shopping
									</a>
								</div>
							</li>
						
<li class="pull-right">
								<a href="ajax_fakes/checkout.html">
								<button class="btn btn-success continue_to_checkout" type="submit" value="Checkout" name="cont_to_checkout2">
									Continue To Checkout
								</button>
		                </a></li>
							<li class="pull-right">&nbsp;&nbsp;</li>
							<li class="pull-right">
								<div>
									<button class="btn btn-danger remove_all" type="submit" name="remove_all2" value="remove_all" rel="tooltip" data-placement="top" data-original-title="Be careful! All products will be entirely removed from the basket!">
										<i class="icon-remove icon-white"></i>
										Remove All
									</button>
								</div>
							</li>
						</ul>
					</div>
				</div>
			</div>
		</form>

[/code]

here is my php:
[php]/*** mysql hostname ***/
$hostname = ‘localhost’;

/*** mysql username ***/
$username = ‘username’;

/*** mysql password ***/
$password = ‘password’;

try {
$dbh = new PDO(“mysql:host=$hostname;dbname=mysql”, $username, $password);
/*** echo a message saying we have connected ***/
echo ‘Connected to database’;

/*** close the db connection. always do this ***/
}
$dbh = null;
{

}

catch(PDOException $e)
{
echo $e->getMessage();
}

echo(‘Thank you for choosing our services. Your order is being processed.’)

[/php]

I’m looking for the db to produce a output similar to this:
[php]
$array = array();
$array[‘options4521’][] = array(‘A’,‘C’,‘D’);
$array[‘options4521’][] = array(‘B’,‘B’,‘A’);

echo print_r($array);

And the output is:

[options4521] => Array
(
[0] => Array
(
[0] => A
[1] => C
[2] => D
)

    [1] => Array
        (
            [0] => B
            [1] => B
            [2] => A
        )

)[/php]

thank you all. glad you all around.

Slade, welcome to the site! Hopefully, we can help…

First, never use spaces in your clauses. It just cause problems in your code later on.
So, Rose Gold should not have spaces in the value section.
Let’s explain this. What the user sees on the site means nothing to the programmer. The value of the item
can be anything. Normally it is just an index number, 1,2,3,etc that points to the database table of items
you have for sale. In this way, if you change the items you sell, they are updated in the options drop
downs. But, we can cover that more another time…

Next, I am not sure of the last part of your post. It talks about array in the database? This is not how it is
done. You create a database. You create a table for users, one for products and one for sales. The inputs
from your HTML form fields are pulled in from them with PHP and then entered into the database tables.
So, a row of data in the sales-cart table would be user-id, product-id, price, quantity, status. One of these
rows for each and every product item that user orders. So, if they order 10 items, 5 different ones, there
would be 5 new rows of entries into the database. You can create an array of products and current prices
and quantities and store it, but, it is slower that way. A simple query can load up all of the items in one
cart and is easy to display that way. ( SELECT * FROM sales_cart WHERE user=123 AND date=today )

Now, if this is really a “cart template”, it already has the database figured out for you and all of the other
items in place. Or, if you mean you were using sample code online for carts, the first step would be to do
a total define of your database and tables needed.

If you wish tutorials on how to do this step by step, we can send you some links. let us know what you
really need help with… Glad you joined us, hope we can help…

what a prompt response…thanks. i was under the impression that i could create the html code where a customer would choose and item, then choose the temple then submit that data to a php script named “orders” where the data would be converted to a variable then inserted into the db and tables i’ve created for me to retrieve and fill the order. i humbly ask why is this not feasible?

to take a look at the template please go to
http://solutionm3.com/ordertest.html

Thanks

It is feasible, just not the best way.
[php]<?php
$products = array (
1 =>array (
‘price’=>5,
‘desc’=>‘This item’,
‘item_num’ => 0001,
),
2 =>array (
‘price’=>10,
‘desc’=>‘That item’,
‘item_num’ => 0002,
),
3 =>array (
‘price’=>15,
‘desc’=>‘My item’,
‘item_num’ => 0003,
)
);

echo “

”;
print_r( $products );
echo “
”;

printf("%s is $%d", $products[2][‘desc’], number_format($products[2][‘price’], 2));[/php]

Yes, I agree with Astonecipher! You can do it, but… Look at his code…

forgive my newness but why is the other way better? there doesn’t seem to be any security risks. what is the better way? it appears that the demo you responded with is really straight forward…what could be better than that? please don’t think i’m a smart asss. i’m just trying to understand. Thanks a bunch.

to add: it would seem to work with the data already in the db. using the converge/elevon gateway processing package all that would need be done is that data extracted to the elevon gateway script. but i want to do this right. just needing to understand. :-[. I did look at the script that astonecipher posted but i also noticed i didn’t see any code for the temple options. thanks again.

Well, we are going by what you asked us:

i was hoping to avoid sessions and use a php to convert the values of the options to variables then insert them into the mysql db.
So, no sessions, you want to grab data on a form's drop-downs, you want to save that data into a DB. Well, that is just normal processing of forms with PHP. No need to drop that into an array and then have to reload it back from the db into an array when you want to use the data. Just save the data only.

Now, if you want to use the Elavon payment package, that is a card payment system and they mostly use
XML for sending to their system remotely. Do you want to create that from your data or are you hosting
your own version and want to use a PHP array template to store intermediate sales carts?

Either way, we can help you create an array. Astonecipher showed you how PHP would set up an array to
match your sample. You would have to replace the hard-coded numbers with the results from your query.
To create one query that creates an array would be extremely complicated. But, in PHP, it would be a series
of queries pulling out data and building the array. It can be done.

I personally have time tomorrow to test it out for you, it’s 1:40 AM here, up at 7… More on this tomorrow!

Slade, I have taken your HTML, form and tables and have a page working now displaying them.
I see you are using the BootStrap library, but, didn’t bother linking it to it as this is just for the array issue.
Looking at your HTML/form code, I see that there is no PHP code at all in it for handling data. I suspect
that the “template” you were given was just the base layout and no code was included with it.

Next, I looked at your PHP, it is just a DB connection, so it is useless for us. You have not shown any of your
code that takes the results from your HTML page and puts it anywhere. Are you looking for us to create
that for you? If so, we will send you to a great tutorial on how to use inputs from forms. Again, normally,
your cart would be handled inside your database, not in a PHP array. For example why, what happens if
the user gets disconnected from the internet while having a cart going. You would loose everything as the
session would drop. Normally, a cart would be held in a database linked to the user’s IP and would be
removed after a set number of minutes usually 20 to 60.

If you keep the cart in the browser and just keep posting it to itself, it can be done, but not the correct way,
again, in my humble opinion. I did further research on the link to the Elavon payment gateway package and
found in every single post I found that it is used in XML not PHP arrays. So, if you spend the time to create
a PHP array, you would still have to convert it to XML. You might want to think about creating XML code
instead to save in your database. In that way, you can just send the XML cart as-is to Elavon.

Well, steer me to what you want us to help you with. As Astonecipher showed you, an array of orders
would be just an array of an array.

For further info, here are two tutorials for you to study. The first is on PHP and form fields the second is on
creating a full cart system with user-base, products and sales using PHP and MySQL. Hope it helps some…

http://www.w3schools.com/php/php_forms.asp (Press green next-chapter to walk thru it)

http://code.tutsplus.com/tutorials/build-a-shopping-cart-with-php-and-mysql–net-5144

i will read the tutorials you sent for a few hours and get back with you all. thanks…time to study a little.

hello. for the sake of simplicity i like putting the data into the db but i’d rather do it the right way. It appears that with the template there is a set of tiles. i guess i need help changing the values on the backside of the html form so they can be passed to the db via php. is this the correct first step?

Well, still not understanding what you really need. I will recap how PHP works with a database.

You create HTML pages which the users will see.
You add Javascript or JQuery to it if you need to animate things on it.
You create a database and create a connection string to it.
You create tables in the database for various uses such as “users”, “products”, “orders”, “etc…”
You create fields in each of the tables to be used to store data, many fields for each table as needed.
You create HTML FORMs for the user to see and enter data and place them in your HTML pages
You create PHP code and place inside each HTML page to capture the form field data
You create PHP code and place inside each HTML page to validate the data captured from forms
You create PHP code and place inside each HTML page to store the data inside the database

Many of these are combined steps and many are very quick and easy.
Where are you in this process and which are you having problems with?

Also, you showed us this as your desired final output:

$array = array(); $array['options4521'][] = array('A','C','D'); $array['options4521'][] = array('B','B','A');
This is just an array of data. That makes no sense at all. If you want to be able to pull data from the form you showed us, I gave you a tutorial on how to do that. You would use PHP and capture the $_POST data form the forms. Then, you have the data and you can format it into arrays however you wish.

hello. i’m beginning to understand. so happens that i didn’t have the files needed for full execution of the plugin. i do now.
in your recap of how php works with a db if i look at the list as steps i am at step 7.
with this being a template there are several html files that have the following html names:

checkout, choose_color, choose_delivery, choose_qty, choose_size, remove_all, and remove_item

each of those files have the following html:

[code]

[/code]

I am presuming that the php code you mentioned would go into each of these files just after the closing of the head tag.

thank you so much for your time and patience.

Well, Slade, no problem on time or patience! That is what we are here for. To help…

Well, if you are using a template, then it should already have the PHP in place for you to get started with.
It sounds like it does not. Is this for a class project or are you planning to sell something yourself?

Normally, you would not have choose_color, choose_qty, choose_size, etc on different pages. Normally, it is
just one cart or sales page, you would just use PHP code in that one page. Loosely it would be something
like this:

<?PHP ... PHP code to connect to your database ... ... PHP code to acquire all of the $_POST[] entries from your forms ... ... PHP code to validate the inputs that were captured from the forms ... ... PHP code to run queries to the database to save new data or update old data ... ?> ... Here would go links to other files like CSS scripts or pointers to JS code ... ... Your HTML code which includes your FORMs and input FIELDS ... ( This HTML code in the body might also have PHP code that displays data from the database as it is needed such as product drop-downs for color, size, etc... )

As you see, you can place PHP code and HTML code together on one page. You can also place PHP code
into the HTML code to display things you want the user to see. Remember that PHP code is only on the
server and is never seen in the client side (browser). So, any outputs from PHP code is done on the server
and works with the database which is also server-side only. Did that make sense? Hope so…

Now, as I posted before, here is a link that walks you thru this process showing samples how to do it all.
[url]http://www.w3schools.com/php/php_forms.asp[url] (Press green next-chapter to walk thru it)
I would study that some and start your coding. Once you get part of it done, post it and we will look at it
for you. Remember to post the code inside the PHP tags.

hello, sirs. i’ve been trying to get it right. i once created a form using html with the php forwarding the data to the db. i never finished that site. it’s babiesnmovies.com/Register.html. i’m going to need to change the entire thing because when i created the site in 2010 this process was a bit different.

here is what i have so far:

[code]

Untitled Document

[php]<?php

/*** mysql hostname ***/
$hostname = ‘xxxx.co’;

/*** mysql username ***/
$username = ‘xxx’;

/*** mysql password ***/
$password = ‘xxxx’;

try {
$dbh = new PDO(“mysql:host=$hostname;dbname=orders0001”, $username, $password);
/*** echo a message saying we have connected ***/
echo ‘Connected to database
’;

?>

<?php // define variables and set to empty values $temple = $quantity = $price = $promotional = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $temple = ordertest01($_POST["temple"]); $quantity = ordertest01($_POST["quantity"]); $price = ordertest01($_POST["price"]); $promotional = ordertest01($_POST["promotional"]); } function ordertest01($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <?php $temple = $_POST['Temple']; $quantity = $_POST['Quantity']; $price = $_POST['Price']; $promotional = $_POST['Promotional']; mysql_select_db("xxxx", $con); $sql="INSERT INTO orders0001 (Temple, Quantity, Price, Promotional) values ('$temple', '$quantity', '$price', '$promotional')"; header( "Location: http://www..com/.....html" ) /*** close the database connection ***/ $dbh = null; } catch(PDOException $e) { echo $e->getMessage(); } ?>

[/php]

[/code]

its not much but i’m seriously working on it and i have read the tutorial from the last link sent to W3schools. it thew me a little because the form is created in php whereas mine is created in html. trying to execute the processing of form data is a bit rough for me because i first need to determine which $_POST[] entries are on the code side of my html form. once i get past this hurdle i believe i’ll be better on my way.

i’ve changed the code to include the htmlspecialchars().

to this:
[php]$temple = htmlspecialchars($_POST[‘Temple’]);
$quantity = htmlspecialchars($_POST[‘Quantity’]);
$price = htmlspecialchars($_POST[‘Price’]);
$promotional = htmlspecialchars( $_POST[‘Promotional’]);[/php]

from this:
$temple = $_POST[‘Temple’];
$quantity = $_POST[‘Quantity’];
$price = $_POST[‘Price’];
$promotional = $_POST[‘Promotional’];

this handles the validation issue, ya?

Well, yes. It is a base level validation for those four items. There are more robust code for validation, but,
I suggest getting your page working first. This will do for now. Let us know when you get further along or
if you have problems…

What kind of validation are you looking for exactly? That function really doesn’t DO anything other than convert characters into html equivalents.

One MAJOR issue I have with your last code, Use PDO. PDO and mysql_ are not the same nor are they compatible. So,

[php]
$stmt = $pdo->prepare( “SELECT name FROM users WHERE id = :id” );
$stmt->execute( array(’:id’ => $_POST[‘user_id’]) );
[/php]

is not the same as

[php]$result = mysql_query( “INSERT INTO orders0001 (Temple, Quantity, Price, Promotional) values (’$temple’, ‘$quantity’, ‘$price’, ‘$promotional’)”;[/php]

I understand it has been a while.

Sponsor our Newsletter | Privacy Policy | Terms of Service