Page 1 of 1

Problem pulling values from product form

Posted: Tue Sep 21, 2010 2:09 pm
by keepsmilen
I have been working on a catalog application. I am having trouble with transferring the information of a product (which are displayed from the database, each in it's own form). When I check to see if the form (from the product) has been submitted so I can transfer the variables to a cookie to send to the shopping cart the product name variable is not set.
I want to get the product name variable from the form to the bottom of the code in the if isset submitted section.
Any help would be greatly appreciated.

Here is the code from the section I am having problems with. It may be important to note that I have a form that creates product categories earlier in the code that also uses the POST array however I am still having issues even using the GET array.

Code: Select all

    $cxn = connectDatabase();
    $query = "SELECT * from ProductInformation WHERE product_category='$value'";
    //setting variable result to the results of the mysql query of the cxn and query information
    $result = mysqli_query($cxn,$query) or die("Can't execute query");
    $rows = mysqli_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysqli_fetch_row($result);
$productname = $row[0];
    echo '<form id="product" name="product" method="POST" action="ProductCatalog.php">';
    echo '<table width="400" border="1">';
    echo '<tr><td>' . $row[0] . '</td>';
    echo '<td>' . $row[1] . '</td></tr>';
    echo '<tr><td colspan="2">Price: $' . $row[2] . '<td></tr>';
    echo '<tr><td>' . $row[3] . '</td>';
    echo '<td><img src="' . $row[4] . '"/></td></tr>';
    echo '<tr><td colspan="2"><input type="hidden" name="submitted" value="yes">';
    echo '<input type="hidden" value="';
         $productname;
    echo '" name="product_name">';
    echo '<input type="submit" value="submit"></td></tr>';
    echo '</table></form>';
}//check if product is set
if(isset ($_POST['submitted']) and $_POST['submitted'] =="yes"){
    echo "It has been submitted";
    echo $_POST['product_name'];
    //if product is selected proccess information from the product form and transfer to shopping cart
}

Re: Problem pulling values from product form

Posted: Tue Sep 21, 2010 2:58 pm
by Jonah Bron
What doesn't work? Do you get an error?

Re: Problem pulling values from product form

Posted: Tue Sep 21, 2010 9:36 pm
by keepsmilen
I'm having problems getting the value of the product name from the submitted form
Here:

Code: Select all

if(isset ($_POST['submitted']) and $_POST['submitted'] =="yes"){
    echo "It has been submitted";
    echo $_POST['product_name'];
When I submit a product "it has been submitted" is printed. However, the product name is either ignored (and not printed) or I'm told that there is no value set for the 'product_name'.

Does that make sense?

Thanks!

Re: Problem pulling values from product form

Posted: Wed Sep 22, 2010 6:44 am
by instinct13
Hey.

You cannot ask for $_POST['product_name'] on the same page where you are having your form, at least not using the different .php file as form action (of course there are ways to do that but that's not the point). When you set ProdutCatalog.php as action of your form, that means that all values from that form are going to be accessible to file ProductCatalog.php in $_POST variable, NOT the same file where your form is. Go, try it out, ask for the $_POST['product_name'] in ProductCatalog.php page and you will get it. The reason why

Code: Select all

echo "It has been submitted."
is working and not the next line should be obvious to you by now. If, perhaps, you check whether $_POST['product_name'] has been set instead of $_POST['submitted'],

Code: Select all

echo "It has been submitted."
won't work either.