Page 1 of 1

$_POST variable not working?

Posted: Sat Feb 09, 2008 9:18 pm
by ashebrian
Hi,

I have a scripts thats doesn't seem to work at all. Please help...i'm trying to post the forms details to the next 3 pages. ere's the codes:

Code: Select all

<form name='availablevouchers2' action='voucherProcess.php' method='post'>
                                <input name="vouchertype" type="hidden" value="You Choose The Amount" />
                                <input type="hidden" name="price" value="<?php echo $_POST['price']; ?>"/>
                                <input type="hidden" name="quantity" value="<?php echo $_POST['quantity']; ?>"/>
                                <input type="hidden" name="deliverymethod" value="<?php echo $_POST['deliverymethod']; ?>"/>
voucherProcess.php

Code: Select all

<?php
if(isset($_POST['availablevouchers'])) {
 
    if (($_POST['quantity'] == '1') AND ($_POST['price'] == '50')) {
    header("Location: voucherLoaded1.php"); 
    }
    elseif (($_POST['quantity'] == '2') AND ($_POST['price'] == '50')) {
    header("Location: voucherLoaded2.php"); 
    }
    elseif (($_POST['quantity'] == '3') AND ($_POST['price'] == '50')) {
    header("Location: voucherLoaded3.php"); 
    }
    elseif (($_POST['quantity'] == '4') AND ($_POST['price'] == '50')) {
    header("Location: voucherLoaded4.php"); 
    }
    elseif (($_POST['quantity'] == '5') AND ($_POST['price'] == '50')) {
    header("Location: voucherLoaded5.php"); 
        }
        elseif (($_POST['quantity'] == '1') AND ($_POST['price'] == '100')) {
        header("Location: voucherLoaded6.php"); 
        }
        elseif (($_POST['quantity'] == '2') AND ($_POST['price'] == '100')) {
        header("Location: voucherLoaded7.php"); 
        }
        elseif (($_POST['quantity'] == '3') AND ($_POST['price'] == '100')) {
        header("Location: voucherLoaded8.php"); 
        }
        elseif (($_POST['quantity'] == '4') AND ($_POST['price'] == '100')) {
        header("Location: voucherLoaded9.php"); 
        }
        elseif (($_POST['quantity'] == '5') AND ($_POST['price'] == '100')) {
        header("Location: voucherLoaded10.php"); 
        }
            elseif (($_POST['quantity'] == '1') AND ($_POST['price'] == '150')) {
            header("Location: voucherLoaded11.php"); 
            }
            elseif (($_POST['quantity'] == '2') AND ($_POST['price'] == '150')) {
            header("Location: voucherLoaded12.php"); 
            }
            elseif (($_POST['quantity'] == '3') AND ($_POST['price'] == '150')) {
            header("Location: voucherLoaded13.php"); 
            }
            elseif (($_POST['quantity'] == '4') AND ($_POST['price'] == '150')) {
            header("Location: voucherLoaded14.php"); 
            }
            elseif (($_POST['quantity'] == '5') AND ($_POST['price'] == '150')) {
            header("Location: voucherLoaded15.php"); 
            }
                elseif (($_POST['quantity'] == '1') AND ($_POST['price'] == '200')) {
                header("Location: voucherLoaded16.php"); 
                }
                elseif (($_POST['quantity'] == '2') AND ($_POST['price'] == '200')) {
                header("Location: voucherLoaded17.php"); 
                }
                elseif (($_POST['quantity'] == '3') AND ($_POST['price'] == '200')) {
                header("Location: voucherLoaded18.php"); 
                }
                elseif (($_POST['quantity'] == '4') AND ($_POST['price'] == '200')) {
                header("Location: voucherLoaded19.php"); 
                }
                elseif (($_POST['quantity'] == '5') AND ($_POST['price'] == '200')) {
                header("Location: voucherLoaded20.php"); 
                }
                    elseif (($_POST['quantity'] == '1') AND ($_POST['price'] == '250')) {
                    header("Location: voucherLoaded21.php"); 
                    }
                    elseif (($_POST['quantity'] == '2') AND ($_POST['price'] == '250')) {
                    header("Location: voucherLoaded22.php"); 
                    }
                    elseif (($_POST['quantity'] == '3') AND ($_POST['price'] == '250')) {
                    header("Location: voucherLoaded23.php"); 
                    }
                    elseif (($_POST['quantity'] == '4') AND ($_POST['price'] == '250')) {
                    header("Location: voucherLoaded24.php"); 
                    }
                    elseif (($_POST['quantity'] == '5') AND ($_POST['price'] == '250')) {
                    header("Location: voucherLoaded25.php"); 
                    }
                        elseif (($_POST['quantity'] == '1') AND ($_POST['price'] == '300')) {
                        header("Location: voucherLoaded26.php"); 
                        }
                        elseif (($_POST['quantity'] == '2') AND ($_POST['price'] == '300')) {
                        header("Location: voucherLoaded27.php"); 
                        }
                        elseif (($_POST['quantity'] == '3') AND ($_POST['price'] == '300')) {
                        header("Location: voucherLoaded28.php"); 
                        }
                        elseif (($_POST['quantity'] == '4') AND ($_POST['price'] == '300')) {
                        header("Location: voucherLoaded29.php"); 
                        }
                        elseif (($_POST['quantity'] == '5') AND ($_POST['price'] == '300')) {
                        header("Location: voucherLoaded30.php"); 
                        }
                            elseif (($_POST['quantity'] == '1') AND ($_POST['price'] == '350')) {
                            header("Location: voucherLoaded31.php"); 
                            }
                            elseif (($_POST['quantity'] == '2') AND ($_POST['price'] == '350')) {
                            header("Location: voucherLoaded32.php"); 
                            }
                            elseif (($_POST['quantity'] == '3') AND ($_POST['price'] == '350')) {
                            header("Location: voucherLoaded33.php"); 
                            }
                            elseif (($_POST['quantity'] == '4') AND ($_POST['price'] == '350')) {
                            header("Location: voucherLoaded34.php"); 
                            }
                            elseif (($_POST['quantity'] == '5') AND ($_POST['price'] == '350')) {
                            header("Location: voucherLoaded35.php"); 
                            }
    else { die("Error"); }
    exit;
}
?>
voucherLoaded1.php

Code: Select all

<table width="100%">
                  <tr>
                    <td></td>
                    <td></td>
                  </tr>
                  <tr>
                    <td width="30%"><b>Voucher:</b></td>
                    <td><?php echo $_POST['vouchertype']; ?></td>
                  </tr>
                  <tr>
                    <td width="30%"><b>Total Amount:</b></td>
                    <td>&euro; 
                        <?php
                        $price = intval($_POST['price']);
                        $quantity= intval($_POST['quantity']);
                        echo $price * $quantity;
                        ?>
                    </td>
                  </tr>
                  <tr>
                    <td width="30%"><b>How Many?</b></td>
                    <td><?php echo $_POST['quantity']; ?></td>
                  </tr>
                  <tr>
                    <td width="30%"><b>Voucher Delivery:</b></td>
                    <td><?php echo $_POST['deliverymethod']; ?></td>
                  </tr>
                </table>
.
.
.
.
<form action="voucherLoadedPayment1.php" method="post" enctype="multipart/form-data" onsubmit="return check(frmRequest)" name="frmRequest" id="frmRequest">
                <input type="hidden" name="vouchertype" value="<?php echo $_POST['vouchertype']; ?>"/>
                <input type="hidden" name="price" value="<?php echo $_POST['price']; ?>"/>
                <input type="hidden" name="quantity" value="<?php echo $_POST['quantity']; ?>"/>
                <input type="hidden" name="deliverymethod" value="<?php echo $_POST['deliverymethod']; ?>"/>
I can't seem to post the first codes above to the last code. Please help as i'm just learning php

Re: $_POST variable not working?

Posted: Sat Feb 09, 2008 10:12 pm
by GuitarheadCA
Well, The information flow isn't very obvious from your post, so I can't tell you why the POST variables aren't working.

However, I think you're code has a few bigger problems.

-You're creating 35(!) different "voucherLoaded" pages to process the same two variables. I imagine you're doing something similar with the variables in all of those 35 pages, which means that you should be able to use a single page and have the page act on those values accordingly.

-The above change would eliminate the need for the overgrown string of "elseif"s that makes your code unwieldy.

-Should you decide that the above is impossible, your entire string of "elseif"s can be changed into two small loops:

Code: Select all

 
$k = 1;
for($i=50; $i<400; $i+= 50) {
 for ($j=1; $j<6; $j++ ) {
  if( ($_POST['quantity'] == $j) && ($_POST['price'] == $i) ) {
   header('location: voucherLoaded' . $k . '.php');
  }
  $k++
 }
}
 
- The upkeep on the above code would be worlds easier than the way it is listed. (No one wants to change 35 lines if you misspell something)

I hope that makes some sense, finding errors in code is much easier when the code is concise.

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 4:12 am
by JAM

Code: Select all

if(isset($_POST['availablevouchers'])) {
Youre not using 'availablevouchers' anywhere?

Edit;
And as previous speaker thinks; your code is not well thought through. If it works for you, it's okay, but a heads up might be in order. But intead of using the loop I'd look into moving from $_POST to $_GET and use ?variables=in+the&URL=instead. Will make you use one or maybe two pages instead of multible.

Edit II;
Bad judgement, changed post abit.

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 10:02 am
by lucia
Hi, could you solve the problem?
I notice you didn't include the attribute 'id' in the input tags. If you don't include it the script might not work in internet explorer (in case you are testing there)
Mozilla uses the attribute name to recognize the variable, but explorer uses id. You should include both in your tags.

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 3:58 pm
by ashebrian
I tried using the above code in my voucherProccess.php in stead of the ifelse statements:

Code: Select all

$k = 1;
for($i=50; $i<550; $i+= 50) 
{
 for ($j=1; $j<6; $j++ ) 
 {
  if( ($_POST['quantity'] == $j) && ($_POST['price'] == $i) ) 
  {
   header('Location: voucherLoaded' . $k . '.php');
  }
  $k++
 }
}
but get the following error:

Code: Select all

Parse error: syntax error, unexpected '}' in voucherProcess.php on line 12
..................................................
For the next solution are you saying to use $_GET instead of $_POST in the pages after this voucherChooseAmt.php ?

..................................................

And the last solution regarding the id for ie explorer. your are right about this using the id and name together as i came accross this problem before and did this which solved my problem.....But what i did was used both id and name in the <form> tag and only name in the <input> tag as if you were using id in inputs AND using css then the id will be refered to css. Please correct me if i'm wrong?

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 5:11 pm
by Christopher
Missing semicolon:

Code: Select all

$k++;

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 6:01 pm
by ashebrian
yeah....pretty stupid i am. Its always the simple errors i don't see.

The code you gave me does exactly the same thing as the ifelse statement. If i was to use this code i'd get 35 different pages to design. Nobody wants that but i don't seem to know how to get this down to lets say 5 pages coz when the user inputs 5 vouchers i'd want i'd want 5 different voucher boxes to come up on the next page for them to input details. So to do this another way would be:

Code: Select all

$k = 1;
 for ($j=1; $j<6; $j++ ) 
 {
  if($_POST['quantity'] == $j)
  {
   header('Location: voucherLoaded' . $k . '.php');
  }
  $k++;
 }
 
However, i would also like to post the price on the next page and the page after which mails the info. Can you help with this solution? Do i have to add to all pages

Code: Select all

<input type="hidden" name="price" value="<?php echo $_POST['price']; ?>" />
and this to the next page and the mail page

Code: Select all

<td>&euro; 
                        <?php
                        $price = intval($_POST['price']);
                        $quantity= intval($_POST['quantity']);
                        echo $price * $quantity;
                        ?>
                    </td>

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 6:10 pm
by lucia
Hi,

When you want to "call" a css style from a stylesheet you have to use the attribute "class"
Example: <input type="text" id="example" name="example" class="exampleColors">
-the attribute id is not related to styles-
I advise you tu use id and name in all your input tags and call them with the same name.

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 6:23 pm
by Christopher
lucia wrote:-the attribute id is not related to styles-
Actually, id is related to styles. You can use either id or class:

Code: Select all

<style>
// style an id
#exampleColors {
}
// style a class
.exampleColors {
}
 
Many elements can all have the same class, but id is unique to one element.

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 6:31 pm
by ashebrian
Id is actually related to styles. Is this just for php....you always have to use that for php and have the css as class's not id's. understandable, if i'm not wrong. I got mixed up with using javascript in the <form> tag for both the id and name for both ie and firefox to work with that. Thanks. I implemented all this. But it didn't solve my $_POST problem. Same error as i'm testing it in firefox.

Any ideas on the post before your solution.

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 6:43 pm
by Christopher
You can simpify this code a lot. It uses two varaibles that count identically. And it does not need a loop:

Code: Select all

// alway filter inputs for security, now this must be an integer
$quantity = intval($_POST['quantity']);
// now check if it is in the range
if (($quantity >= 1) && ($quantity <= 6))
{
   header('Location: voucherLoaded' . $quantity . '.php');
} else {
   echo "Error: quantity=$quantity. ";
}

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 7:33 pm
by ashebrian
this code does the exact same job:

Code: Select all

# $k = 1;
#  for ($j=1; $j<6; $j++ )
#  {
#   if($_POST['quantity'] == $j)
#   {
#    header('Location: voucherLoaded' . $k . '.php');
#   }
#   $k++;
#  }
arborint wrote:You can simpify this code a lot. It uses two varaibles that count identically. And it does not need a loop:

Code: Select all

// alway filter inputs for security, now this must be an integer
$quantity = intval($_POST['quantity']);
// now check if it is in the range
if (($quantity >= 1) && ($quantity <= 6))
{
   header('Location: voucherLoaded' . $quantity . '.php');
} else {
   echo "Error: quantity=$quantity. ";
}
The above codes do not need to changed. What i need is to post the price to the next page and mail page to send the email with this info in it.

Re: $_POST variable not working?

Posted: Sun Feb 10, 2008 7:46 pm
by dayyanb
The above codes do not need to be changed.
If you want your page to be efficient, and the code to be legible they do.
I am new to this forum but according to:
viewtopic.php?f=6&t=30038
The purpose of this forum is to teach people to use php, not just solving specific problems.