Problem defining variable

Holy cow I think i’m getting this. (it’s easy to tell I have the learning capacity of a potato). So I have to go backward from the cart page and find where the session variables are and add this to them. So then I’m guessing i’ll be able to loop em out with a while statement. Or am I totally on another planet?

Ok I entered the new code in but am still getting an undefined variable throughout. If I suppress the errors with this code
[php]error_reporting(E_ALL ^ E_NOTICE);[/php]
and put something in the size column it prints it out just fine. it just wont echo the variable $size

<?php [php]if(isset($_POST['submit'])) { $size = $_POST['submit']; // this should = size not submit $size = $_POST['size'];[/php] } ?>

On the add item to cart page you need to set the variable before you go to the cart.php

So in the shop page say
/cart/product.php?id=9

you need to add the

[php]$_SESSON[size] = $_POST[size]; [/php]

This will add the size to the session

Then on the cart page the
[php]$size = $_SESSON[size][/php]

This is changing the size to the submit button value
which = add to cart
This is what I got confused about suddenly the size was changed from
xxxl to add to cart
So I was trying to find where it was getting changed but I could not see this code as it was added after.

So this should work if changed to the right value forget about my last post.
[php]
if(isset($_POST[‘submit’])) {
$size = $_POST[‘submit’];}
[/php]

[php]
if(isset($_POST[‘submit’])) {
$size = $_POST[‘size’];}
[/php]

LOL, Noodles… That’s what I said… He keeps forgetting to capture the posted value of SIZE…

So, Lost, you are almost there… Just a recap on how posting variables work:

You create a form. This form has whatever data you want the user to enter, such as number of items and
size and color or any other needed data.

You pass this info to a PHP page that “pulls” the data using “$a_variable=$_POST[‘some_variable_name’]”.
This new variable is useable in your PHP code for saving inside of a database or calculations or whatever you may need to do with the data.

At this point, you would “save” this information inside of your “cart” for use on other pages such as perhaps
the payment page where it would recap the “cart” info and show the totals and shipping costs. At this point,
you would save this “cart” into a database so the user and yourself could pull up a previous “cart” for archives
or for sending copies to the user in the future.

Then, you would switch to your payment page and display a recap of the entire “cart” for the user and ask
for payment options.

I just wanted you to see a short overview of the steps. I think you are nearly there, you just need to listen
to Noodles and others on the missing piece which is the capturing of the “size” from the posted form.

Hope this wasn’t annoying to recap it all, but, seems like you are very close… Good luck…

You guys are awesomely patient, I really do appreciate that. The question I guess that I have is can I merge the [php]if (isset($_POST[‘pid’])) {[
$pid = $_POST[‘pid’];
}
/php]

and the
[php]if (isset($_POST[‘submit’])) {
$size = $_POST[‘submit’];
}[/php]

with the “||” or do I have to keep them separated?
or am i missing the point alltogether? ;D

would you mind also looking at the setup of my form. I’ll post it here I just want to make sure I’m referencing it right.

<form id="form1" name="form1" method="post" action="cart.php">
	
		<p>          
          <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
        </p>
	
      <label>Size
      <select name="size">
        <option>Select</option>
        <option value="small">Small (petite)</option>
        <option value="medium">Medium (average)</option>
        <option value="large">Large (life size)</option>
        <option value="xl">X-Large (full figured)</option>
        <option value="xxl">XX-Large (our Hindenberg line)</option>
        <option value="xxxl">XXX-Large (DAMN!!)</option>
      </select>
      </label>
      
	  
	 
        
        
        <input type="submit" name="size" id="size" value="Add to Shopping Cart" />
        <p>   
         
      </form>

You want both so you would use && = AND
Because || = OR which would be one or the other

[php]if (isset($_POST[‘pid’])&&(isset($_POST[‘size’]))){
$pid = $_POST[‘pid’];
$size = $_POST[‘size’];
}[/php]

The form looks ok you do not need the

as that part is hidden
 <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /> 

huh, i’m still getting 2 errors:

[php]Notice: Undefined variable: size in /home/content/36/8631036/html/cart/cart.php on line 139

Notice: Undefined variable: size in /home/content/36/8631036/html/cart/cart.php on line 150
[/php]

Which 2 lines are these ?

have you printed the size variable to the screen ?
and does it give the right result now ?

You have this line around the 120 line area
maybe delete it
[php]$size = $_GET[‘size’];[/php]

I would try putting echo before the GET to see if the VAR was right, if its right then the GET is not needed as this might be changing it to GET=’’.
[php]
echo ’
before GET = ‘.$size.’
’;
$size = $_GET[‘size’];
echo ‘after GET = ‘.$size.’
’;
[/php]

or change it
[php] $size = $_POST[‘size’];[/php]

Hmmm, Lost, you need to use the $size = $_POST[‘size’]; line to retrieve the posted value of “size”.
$_GET would only be used if you are passing this value thru an argument in the HREF, but, you are
passing it thru a drop-down select clause. This is passed when you “POST” the form using a SUBMIT
button.

Also, the two errors are from you not using the $size = $_POST[‘size’]; line. You need the capture or
retrieve the “size” value from the select clause before your code will work.

Does that mean I need to define

[php]$size = $_POST[‘size’];[/php]

for each set for <?php ?> tags throughout the page?

It should work without doing it as it is set at the top of the page its just you changed it in the middle.

But you can add it if it becomes Undefined at any point you can set it again for that section.

Thats why I said just echo out $size, if you get the right result then you do not need to assign it again as it will be ok with just $size.

There is no point adding extra when its not needed.

But echo and var_dump print_r are your friends they will tell you most things.

because no matter where I put the [php]$size = $_POST[‘size’];[/php], that’s where I get the undefined index error. If I put it in every php block it gives me an error for every line of php block I place it in.

Do you get the right variable to start with

change this

[php]
if (isset($_POST[‘pid’])&&(isset($_POST[‘size’]))){$pid = $_POST[‘pid’];$size = $_POST[‘size’];}
[/php]

to this
[php]
if (isset($_POST[‘pid’])&&(isset($_POST[‘size’]))){
$pid = $_POST[‘pid’];
$size = $_POST[‘size’];
echo 'size = '.$size;
}[/php]

The first thing I need to know is the $size variable assigned a string.
Sorry change it to this so it returns if or if not set message so I can see at the very start of the script it has anything assigned

[php]
if (isset($_POST[‘pid’])&&(isset($_POST[‘size’]))){
$pid = $_POST[‘pid’];
$size = $_POST[‘size’];
echo 'size = '.$size;
}else{
echo ‘size is not set’;
}[/php]

Then we will have to go back to the start and maybe add the post size to the session.

If I put it in every php block it gives me an error for every line of php block I place it in.

Because we need to figure out if the post[‘size’] has been added to the session

So in the shop page say
product.php

you need to add the

This will add the size to the session
[php]$_SESSION[‘size’] = $_POST[‘size’]; [/php]

Then on the cart page the

[php]$size = $_SESSION[‘size’];[/php]

Ok I have the

[php]$_SESSION[‘size’] = $_POST[‘size’]; [/php]

on the product page and

[php]if (isset($_POST[‘pid’])&&(isset($_POST[‘size’]))){
$pid = $_POST[‘pid’];
$size = $_SESSION[‘size’];
echo 'size = '.$size;
}else{
echo ‘size is not set’;[/php]

on the cart page. Here are the errors that surface:

[php]Notice: Undefined index: size in /home/content/36/8631036/html/cart/product.php on line 7[/php]
shows up on the product page, and

[php]size = Add to Shopping CartAdd to Shopping Cart [/php]
shows up on the cart page. Also now when I try to empty the cart I get:

size is not set Notice: Undefined variable: pid in /home/content/36/8631036/html/cart/cart.php on line 31

Notice: Undefined variable: pid in /home/content/36/8631036/html/cart/cart.php on line 31

Notice: Undefined variable: pid in /home/content/36/8631036/html/cart/cart.php on line 33

Notice: Undefined variable: size in /home/content/36/8631036/html/cart/cart.php on line 33

Warning: Cannot modify header information - headers already sent by (output started at /home/content/36/8631036/html/cart/cart.php:18) in /home/content/36/8631036/html/cart/cart.php on line 43

you have jumped a step

if you get no result
[php]if (isset($_POST[‘pid’])&&(isset($_POST[‘size’]))){
$pid = $_POST[‘pid’];
$size = $_POST[‘size’];
echo 'size = '.$size;
}else{
echo ‘size is not set’;
}[/php]

then we will try the other

but the other has to be something else

[php]if (isset($_POST[‘pid’])){
$pid = $_POST[‘pid’];
$size = $_SESSION[‘size’];
echo 'size = '.$size;
}else{
echo ‘size is not set’;
}[/php]

I also printed the cart array and got this:

[php]size = Array ( [username] => lostnvegas [cart_array] => Array ( [0] => Array ( [item_id] => 9 [quantity] => 1 [size] => Add to Shopping Cart ) [1] => Array ( [item_id] => [quantity] => 10 [size] => ) ) [size] => ) Add to Shopping CartAdd to Shopping Cart [/php]

and was wondering why size = add to cart?

I just looked and the name of the input for submit is size should be submit

<input type="submit" name="size" id="size" value="Add to Shopping Cart" />

should be

<input type="submit" name="submit" id="size" value="Add to Shopping Cart" />

The name size is already the name of the size input option so it cant be the name of the submit input as well.
Something small can be overlooked because submit and size look close when written.

Sponsor our Newsletter | Privacy Policy | Terms of Service