Jump to content

PHP: Gather ALL Post Variables

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
So I am making my own shopping cart, and you can add items to it on a single page, and it will update the price. You add items via radio buttons.

Here is the issue, each radio button is named after an ID, and it can be anything. so.. How can I use PHP to get ALL post variables, and break it down?

Thanks
Checkout my new forum! http://adminreference.com/

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
After speaking with you on IM I think this is what you:

Add a prefix to your HTML:

<input type="radio" name="'. $prefix . $id .'">
Assume $prefix = 'abc';

foreach ($_POST as $key => $val) {
    if (substr($key,0,3) == "abc") {
           // We have found a key with prefix abc. This means
          // we want to store it in our cookie.
          // First, lets strip our prefix off
         $newKey = substr($key,3);

         // Add it to the cookie
         $_COOKIE['$newKey'] = $val;
    }
}
This will take abc123 with a value of 'computer' and create a cookie named 123 with a value of 'computer'.

Edited by Jordan, 02 January 2009 - 08:06 AM.


#3
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Yes sir! works perfect, thank you.

However, the cookie code sets an error.

Quote

Warning: Cannot modify header information - headers already sent by (output started at /home/imakeco/public_html/includes/functions.php:39) in /home/imakeco/public_html/pages/cart.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /home/imakeco/public_html/includes/functions.php:39) in /home/imakeco/public_html/pages/cart.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /home/imakeco/public_html/includes/functions.php:39) in /home/imakeco/public_html/pages/cart.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /home/imakeco/public_html/includes/functions.php:39) in /home/imakeco/public_html/pages/cart.php on line 5

Any idea why it would give an error? I never understand what this error is about. I googled it and there are like 30 reasons why, something about the echo command throwing it off, something about spaces between ? and >, i dont get it!

Edit:

Quote

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Checkout my new forum! http://adminreference.com/

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
If there is a space between the top of the file and <?php Apache will send headers so your cookies can't send the headers.
Basically, if your code looks like this:

     // <- Line Feed/Carriage Return here
<?php

That will cause the headers to be sent making it impossible for your COOKIE global variable to be set. If you use echo, print, or display anything to the output before sending your headers (cookies, sessions, location, etc) it will cause the above error.

#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I also had the code above screwed up. I put:

         $_COOKIE['$key'] = $val;

But I meant for it to be:

         $_COOKIE['$newKey'] = $val;

It depends if you want to store the prefix in your cookie or not.

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
How many time do I have to tell you how to fix that error...

#7
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts

John said:

How many time do I have to tell you how to fix that error...
First time, you said its "cause its not in the header", thats not exactly a how to fix, thats more of a why its broke

second time, you told me how to fix it, after I fixed it :).

Dont get cocky.

@Jordan, here is how I set it up.

computer[$computer_id]=$action;
product[$product_id]=$subproduct_id

computer_id = Obvious, ID of the computer.

product[$product_id] is the ID of the product (EG: R.A.M., HDD, Processor, DVD/CD Tray, Any product you would add to a computer)

Then the value of product[] is the subproduct_id, which is the specifics of the product (EG: 2 x 1gb ddr2 ram, 1 x WD 250gb HDD, dualcore processor, Brand of DVD) Anything thats specific about the product.

Check the attachment out :)

Here is the code:
if (isset($_COOKIE['computer'])) {

				    foreach ($_COOKIE['computer'] as $name => $value) {

				    	$sql = "SELECT * FROM `computers` WHERE `id`='$name'";

				    	$mysql = mysql_query($sql);

				    	$get = mysql_fetch_array($mysql);

				    	$computer_title = $get['title'];

				    	$computer_price = $get['price'];

				    	$computer_special_price = $get['special_price'];

				    	echo '<td><font size="2"><b>'.$computer_title.'</b></font></td><td><font size="2">'.$computer_price.'</font></td></tr><tr>';

				    	$checkout_price = $checkout_price + $computer_price;

				    }

				}

				if (isset($_COOKIE['product'])) {

				    foreach ($_COOKIE['product'] as $name => $value) {

				    	$sql = "SELECT * FROM `products` WHERE `id`='$name'";

				    	$mysql = mysql_query($sql);

				    	$get = mysql_fetch_array($mysql);

				    	$prod_name = $get['name']; 

				    	

				    	$sql = "SELECT * FROM `subproducts` WHERE `id`='$value'";

				    	$mysql = mysql_query($sql);

				    	$get = mysql_fetch_array($mysql);

				    	$subprod_price = $get['price'];

				    	$subprod_name = $get['name'];

				    	$subprod_desc = $get['desc'];

					echo '<td><font size="2"><b>'.$prod_name.'</b> - '.$subprod_name.'</font></td><td><font size="2">'.$subprod_price.'</font></td></tr><tr>';

					$checkout_price = $checkout_price + $subprod_price;

				    }

				}

Its not perfect, i need to modify it so it will show the computer and its products and subproducts on one table, right now if you order 2 computers, and multiple products, it will just throw them all on one table. Not all too neat.

Also, I found something thats kind of useful for self-made checkouts!
Calculating UPS Shipping Rate with PHP

I will use that, I wonder of fedex mades one, fedex rocks

Attached Files


Checkout my new forum! http://adminreference.com/

#8
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Now I am trying something similar. if you click on the attachment in the above post, you will see the cookie format. How do I get the name of each cookie, I want to delete all products and computers. I tried this:
foreach ($_COOKIE as $val) {


}

But no luck
Checkout my new forum! http://adminreference.com/