Passing hidden variables PLEASE HELP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by fionaom87 »

sorry again but i messed around with and that error came back again this is what i have now.

<div id="box33"><img src="FYP/img23.jpg" alt="Windows"/>
<label> Tilt and Turn Windows </label>
</div>
<div id="box34"><input type="text" name="quantity[]" size="1" selected="selected"><label> Quantity</label></div>

<!-- Passing of hidden variables-->
<INPUT TYPE="hidden" name="quantity[]" VALUE="<?php echo $quantity[]; ?>">

////my next page



<?php
$totalquantity = $_POST['quantity'];
$newtotal = array_sum($totalquantity);
?>
<!-- Passing of hidden variables-->
<INPUT TYPE="hidden" name="quantity[]" VALUE="<?php echo $quantity[]; ?>">


and my page after that

<?php
$totalquantity = $_POST['quantity'];
$newtotal = array_sum($totalquantity);


Quantity: <?php echo $newtotal; ?><BR>
?>
is it possible to do this or should i stick to just passing from one direct page to another
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by Skoalbasher »

Ok, your hidden type is the same name as your text boxes, which isn't what you should do.

It'd be something like this

Code: Select all

 
<div id="box33"><img src="FYP/img23.jpg" alt="Windows"/>
<label> Tilt and Turn Windows </label>
</div>
<div id="box34"><input type="text" name="quantity[]" size="1" selected="selected"><label> Quantity</label></div>
 
<!-- Passing of hidden variables-->
<INPUT TYPE="hidden" name="windowtype[]" VALUE="Tilt and Turn">
 


so if Tilt and Turn was the first type selected, you'd pull it like

Code: Select all

 
$type1 = $_POST['windowtype[0]'];
 
I hope this makes sense, i'm at work right now so don't have time to write something out completely.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by fionaom87 »

for some reason now my quantity values arent working anymore either.

page1////where i input my quantity


<div id="box33"><img src="FYP/img23.jpg" alt="Windows"/>
<label> Tilt and Turn Windows </label>
</div>
<div id="box34"><input type="text" name="quantity[]" size="1" selected="selected"><label> Quantity</label></div>


page2//
<?php
$totalquantity = $_POST['quantity'];
$newtotal = array_sum($totalquantity);
?>

page3//
<?php
$totalquantity = $_POST['quantity'];
$newtotal = array_sum($totalquantity);
?>

Quantity: <?php echo $newtotal; ?><BR>


i want to drag my quantity value from page 1 to page 3.

thank u for your patience and time.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by Skoalbasher »

If you have 1 hidden field named "windowtype[]" for each text named "quantity[]" you can do something like this

Code: Select all

 
$qt = $_POST['quantity'];
$wt = $_POST['windowtype'];
 
$i = 0;
foreach($qt as a$)
{
 if($a[$i])
 {
   echo "User selected ".$a[$i]." ".$wt[$i]."'s";
   $i ++;
  }
}
 
On a side note, I may be wrong.. but I don't think you can send $_POST data to 1 page from another without using a form. You could use a $_SESSION variable instead.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by fionaom87 »

ya sounds like what exactly what i want but this occured when i put

this in page 3
<?php
$qt = $_POST['quantity'];
$wt = $_POST['windowtype'];

$i = 0;
foreach($qt as a$) //line 89
{
if($a[$i])
{
echo "User selected ".$a[$i]." ".$wt[$i]."'s";
$i ++;
}
}

?>


Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in C:\wamp\www\inventory.php on line 89
(ive highlighted line 89).







page 2 has this now at the end

<INPUT TYPE="hidden" name="windowtype[]" VALUE="Tilt and Turn">

page 3 has this at the top

<?php
$totalquantity = $_POST['quantity'];
$newtotal = array_sum($totalquantity);
?>

<?php
$type1 = $_POST['windowtype[0]'];
?>
thanks
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by Skoalbasher »

I'm sorry, I know you're trying to work on this. I think I know what you need, I'll try and get something working on my lunch break. ugh.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by fionaom87 »

that would be brillant really appreciate it!

Thanks
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by watson516 »

Providing you are using a mysql db, this might work (untested)

items

Code: Select all

<div class="content">
        <form action="process.php" method="POST">
            <div class="item">
                <img src="item.jpg" alt="Sliding Windows" />
                <h3>Sliding Windows</h3>
                <p>
                    Quanity: <input type="text" name="quant[]" />
                </p>
                <input type="hidden" name="item[]" value="1" />
            </div>
            <div class="item">
                <img src="item.jpg" alt="Broken Windows" />
                <h3>Broken Windows</h3>
                <p>
                    Quanity: <input type="text" name="quant[]" />
                </p>
                <input type="hidden" name="item[]" value="2" />
            </div>
            <div class="item">
                <img src="item.jpg" alt="Tilt and Turn Windows" />
                <h3>Tilt and Turn Windows</h3>
                <p>
                    Quanity: <input type="text" name="quant[]" />
                </p>
                <input type="hidden" name="item[]" value="3" />
            </div>
            <div class="add">
                <input type="submit" value="Add To Cart" />
            </div>
        </form>
    </div>
proccess.php

Code: Select all

<?php
session_start();
if(isset($_POST['item'])
{
    foreach ($_POST['quant'] as $key => $value)
    {
        if($value)
        {
            //Add this item to the shopping cart table
            $item=htmlentities($_POST['item'][$key]);
            $quant=(int)$_POST['quant'][$key];
            $q="INSERT INTO cart(cart_user,cart_item,cart_quant) VALUES('{$_SESSION['user']}','$item',$quant)";
            if(mysql_query($q))
            {
                header('Location: index.html');
            }else{
                header('Location: index.html?err=1');
            }
        }
    }
}else{
    header('Location: index.html');      //This page was visited directly
}
?>
If you're not using a db, you could probably use sessions instead.

When you want to display the contents of the shopping cart, you could just query the cart table for every entry with the logged in user's username
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Passing hidden variables PLEASE HELP!!!!!!!!!!!!!!!!!!1

Post by jaoudestudios »

Download a shopping cart, there are loads of opensource ones out there.

I have not tried any myself, but I think you would make more progress. As you will still need to build in a lot more features. No offense, but I hope you are not planning on doing credit cards?
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Passing hidden variables PLEASE HELP!

Post by Skoalbasher »

So, keep page 1 the way it is

this is on page 2

Code: Select all

 
$qt = $_POST['quantity'];
$wt = $_POST['windowtype'];
 
$_SESSION['windowtype'] = array();
$_SESSION['quantity'] = array();
 
$i = 0;
foreach($qt as a$)
{
 if($a[$i])
 {
   $_SESSION['windowtype'][$i] = $wt[$i];
   $_SESSION['quantity'][$i] = $a[i];
   $i ++;
  }
}
 
Page 3

Code: Select all

 
echo "User has selected ".$_SESSION['quantity'][0]." ".$_SESSION['windowtype'][0]."'s"; 
 
Should print something like "User has selected 23 Bay Window's"

EDIT: I don't have anything to check this on while i'm here at work.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: Passing hidden variables PLEASE HELP!

Post by fionaom87 »

//////page 1


<div id="box31"><img src="FYP/img22.jpg" alt="Windows"/>
<label> Casement Windows </label>
</div>
<div id="box32"><input type="text" name="quantity[]" size="1" selected="selected"><label> Quantity</label></div>

<div id="box33"><img src="FYP/img23.jpg" alt="Windows"/>
<label> Tilt and Turn Windows </label>
</div>
<div id="box34"><input type="text" name="quantity[]" size="1" selected="selected"><label> Quantity</label></div>


<input type="submit" value="Submit Details"/>




//page 2


<?php

$qt = $_POST['quantity'];
$wt = $_POST['windowtype'];

$_SESSION['windowtype'] = array();
$_SESSION['quantity'] = array();

$i = 0;
foreach($qt as a$) // line 71
{
if($a[$i])
{
$_SESSION['windowtype'][$i] = $wt[$i];
$_SESSION['quantity'][$i] = $a;
$i ++;
}
}
?>
///page 3


<?PHP
echo "User has selected ".$_SESSION['quantity'][0]." ".$_SESSION['windowtype'][0]."'s";
?>


following error comes up

Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in C:\wamp\www\page2.php on line 71
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Passing hidden variables PLEASE HELP!

Post by Skoalbasher »

Code: Select all

 
<?php
  
$qt = $_POST['quantity'];
$wt = $_POST['windowtype'];
 
$_SESSION['windowtype'] = array();
$_SESSION['quantity'] = array();
 
$i = 0;
foreach($qt as $a) // <---- change here a$ should actually be $a (line 71)
{
 if($a[$i])
 {
   $_SESSION['windowtype'][$i] = $wt[$i];
   $_SESSION['quantity'][$i] = $a[i];
   $i ++;
  }
}
?>
 
 
Oops! Change a$ to $a on line 71.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: Passing hidden variables PLEASE HELP!

Post by fionaom87 »

<?PHP
echo "User has selected ".$_SESSION['quantity'][0]." ".$_SESSION['windowtype'][0]."'s";
?>


everything seems fine im not sure whats causing the problem

it is only printing User has selected 's

Thanks very much for your help
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Passing hidden variables PLEASE HELP!

Post by watson516 »

have you started the session?

Code: Select all

<?php
//The very top of the page, no output before this
session_start();
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Passing hidden variables PLEASE HELP!

Post by s.dot »

top of the script..

Code: Select all

ini_set('display_errors', 'On');
error_reporting(E_ALL);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply